feat: enhance error handling and improve API response management across components
Some checks failed
Build and Push Docker Images / Build and Push Docker Images (push) Failing after 1m39s

This commit is contained in:
2025-04-14 01:05:07 +05:00
parent 0d578c9add
commit 6365a701ba
11 changed files with 111 additions and 66 deletions

View File

@ -3,6 +3,7 @@
import { authOptions } from "@/app/auth";
import type { ApiError, ApiResponse, Device } from "@/lib/backend-types";
import { checkSession } from "@/utils/session";
import { handleApiResponse } from "@/utils/tryCatch";
import { getServerSession } from "next-auth";
import { revalidatePath } from "next/cache";
@ -27,17 +28,7 @@ export async function getDevices({ query, offset, limit }: GetDevicesProps) {
},
);
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<Device>;
return data;
return handleApiResponse<ApiResponse<Device>>(response, "getDevices");
}
export async function getDevice({ deviceId }: { deviceId: string }) {
@ -52,16 +43,7 @@ export async function getDevice({ deviceId }: { deviceId: string }) {
},
},
);
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 device = (await response.json()) as Device;
return device;
return handleApiResponse<Device>(response, "getDevice");
}
export async function addDevice({
@ -88,15 +70,35 @@ export async function addDevice({
}),
},
);
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 SingleDevice;
revalidatePath("/devices");
return data;
return handleApiResponse<SingleDevice>(response, "addDevice");
}
export async function blockDevice({
deviceId,
reason_for_blocking,
blocked_by,
}: {
deviceId: string;
reason_for_blocking: string;
blocked_by: "ADMIN" | "PARENT";
}) {
const session = await getServerSession(authOptions);
const response = await fetch(
`${process.env.SARLINK_API_BASE_URL}/api/devices/${deviceId}/block/`,
{
method: "PUT",
headers: {
"Content-Type": "application/json",
Authorization: `Token ${session?.apiToken}`,
},
body: JSON.stringify({
blocked: true,
reason_for_blocking: session?.user?.is_superuser
? reason_for_blocking
: "Blocked by parent",
blocked_by: session?.user?.is_superuser ? "ADMIN" : "PARENT",
}),
},
);
return handleApiResponse<Device>(response, "blockDevice");
}