mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-07-03 00:38:23 +00:00
refactor: enhance error handling and add pagination to device queries
Some checks failed
Build and Push Docker Images / Build and Push Docker Images (push) Failing after 1m37s
Some checks failed
Build and Push Docker Images / Build and Push Docker Images (push) Failing after 1m37s
This commit is contained in:
@ -1,7 +1,12 @@
|
||||
"use server";
|
||||
|
||||
import { authOptions } from "@/app/auth";
|
||||
import type { ApiResponse, NewPayment, Payment } from "@/lib/backend-types";
|
||||
import type {
|
||||
ApiError,
|
||||
ApiResponse,
|
||||
NewPayment,
|
||||
Payment,
|
||||
} from "@/lib/backend-types";
|
||||
import type { User } from "@/lib/types/user";
|
||||
import { tryCatch } from "@/utils/tryCatch";
|
||||
import { getServerSession } from "next-auth";
|
||||
@ -25,9 +30,12 @@ export async function createPayment(data: NewPayment) {
|
||||
},
|
||||
);
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json();
|
||||
// Throw an error with the message from the API
|
||||
throw new Error(errorData.message || "Something went wrong.");
|
||||
const errorData = (await response.json()) as ApiError;
|
||||
const errorMessage =
|
||||
errorData.message || errorData.detail || "An error occurred.";
|
||||
const error = new Error(errorMessage);
|
||||
(error as ApiError & { details?: ApiError }).details = errorData; // Attach the errorData to the error object
|
||||
throw error;
|
||||
}
|
||||
const payment = (await response.json()) as Payment;
|
||||
revalidatePath("/devices");
|
||||
@ -47,18 +55,16 @@ export async function getPayment({ id }: { id: string }) {
|
||||
},
|
||||
);
|
||||
|
||||
if (response.status === 404) {
|
||||
throw new Error("Payment not found");
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json();
|
||||
console.log(errorData);
|
||||
// Throw an error with the message from the API
|
||||
throw new Error(errorData.message || "Something went wrong.");
|
||||
const errorData = (await response.json()) as ApiError;
|
||||
const errorMessage =
|
||||
errorData.message || errorData.detail || "An error occurred.";
|
||||
const error = new Error(errorMessage);
|
||||
(error as ApiError & { details?: ApiError }).details = errorData; // Attach the errorData to the error object
|
||||
throw error;
|
||||
}
|
||||
const payment = (await response.json()) as Payment;
|
||||
return payment;
|
||||
const data = (await response.json()) as Payment;
|
||||
return data;
|
||||
}
|
||||
|
||||
export async function getPayments() {
|
||||
@ -73,10 +79,13 @@ export async function getPayments() {
|
||||
},
|
||||
},
|
||||
);
|
||||
console.log("response statys", response.status);
|
||||
if (response.status === 401) {
|
||||
// Redirect to the signin page if the user is unauthorized
|
||||
throw new Error("Unauthorized; redirect to /auth/signin");
|
||||
if (!response.ok) {
|
||||
const errorData = (await response.json()) as ApiError;
|
||||
const errorMessage =
|
||||
errorData.message || errorData.detail || "An error occurred.";
|
||||
const error = new Error(errorMessage);
|
||||
(error as ApiError & { details?: ApiError }).details = errorData; // Attach the errorData to the error object
|
||||
throw error;
|
||||
}
|
||||
const data = (await response.json()) as ApiResponse<Payment>;
|
||||
return data;
|
||||
@ -94,11 +103,14 @@ export async function cancelPayment({ id }: { id: string }) {
|
||||
},
|
||||
},
|
||||
);
|
||||
if (response.status === 401) {
|
||||
// Redirect to the signin page if the user is unauthorized
|
||||
throw new Error("Unauthorized; redirect to /auth/signin");
|
||||
if (!response.ok) {
|
||||
const errorData = (await response.json()) as ApiError;
|
||||
const errorMessage =
|
||||
errorData.message || errorData.detail || "An error occurred.";
|
||||
const error = new Error(errorMessage);
|
||||
(error as ApiError & { details?: ApiError }).details = errorData; // Attach the errorData to the error object
|
||||
throw error;
|
||||
}
|
||||
// Since the response is 204 No Content, there's no JSON to parse
|
||||
return { message: "Payment successfully canceled." };
|
||||
}
|
||||
|
||||
@ -132,9 +144,12 @@ export async function updatePayment({
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json();
|
||||
// Throw an error with the message from the API
|
||||
throw new Error(errorData.message || "Something went wrong.");
|
||||
const errorData = (await response.json()) as ApiError;
|
||||
const errorMessage =
|
||||
errorData.message || errorData.detail || "An error occurred.";
|
||||
const error = new Error(errorMessage);
|
||||
(error as ApiError & { details?: ApiError }).details = errorData; // Attach the errorData to the error object
|
||||
throw error;
|
||||
}
|
||||
const payment = (await response.json()) as Payment;
|
||||
return payment;
|
||||
@ -162,9 +177,12 @@ export async function updateWalletBalance({
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json();
|
||||
// Throw an error with the message from the API
|
||||
throw new Error(errorData.message || "Something went wrong.");
|
||||
const errorData = (await response.json()) as ApiError;
|
||||
const errorMessage =
|
||||
errorData.message || errorData.detail || "An error occurred.";
|
||||
const error = new Error(errorMessage);
|
||||
(error as ApiError & { details?: ApiError }).details = errorData; // Attach the errorData to the error object
|
||||
throw error;
|
||||
}
|
||||
const message = (await response.json()) as {
|
||||
message: "Wallet balance updated successfully.";
|
||||
|
Reference in New Issue
Block a user