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

This commit is contained in:
2025-04-12 14:35:23 +05:00
parent d60dd3af14
commit aff9d26e0e
8 changed files with 134 additions and 63 deletions

View File

@ -1,22 +1,23 @@
"use server";
import { authOptions } from "@/app/auth";
import type { Api400Error, ApiResponse, Device } from "@/lib/backend-types";
import { AxiosClient } from "@/utils/axios-client";
import type { ApiError, ApiResponse, Device } from "@/lib/backend-types";
import { checkSession } from "@/utils/session";
import { getServerSession } from "next-auth";
import { revalidatePath } from "next/cache";
type GetDevicesProps = {
query?: string;
offset?: number;
limit?: number;
page?: number;
sortBy?: string;
status?: string;
};
export async function getDevices({ query }: GetDevicesProps) {
export async function getDevices({ query, offset, limit }: GetDevicesProps) {
const session = await checkSession();
const response = await fetch(
`${process.env.SARLINK_API_BASE_URL}/api/devices/?name=${query}`,
`${process.env.SARLINK_API_BASE_URL}/api/devices/?name=${query}&offset=${offset}&limit=${limit}`,
{
method: "GET",
headers: {
@ -25,16 +26,23 @@ export async function getDevices({ query }: GetDevicesProps) {
},
},
);
if (response.status === 401) {
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<Device>;
return data;
}
export async function getDevice({ deviceId }: { deviceId: string }) {
const session = await checkSession();
const respose = await fetch(
const response = await fetch(
`${process.env.SARLINK_API_BASE_URL}/api/devices/${deviceId}/`,
{
method: "GET",
@ -44,7 +52,15 @@ export async function getDevice({ deviceId }: { deviceId: string }) {
},
},
);
const device = (await respose.json()) as Device;
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;
}
@ -68,13 +84,17 @@ export async function addDevice({
body: JSON.stringify({
name: name,
mac: mac,
registered: true,
}),
},
);
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 data = (await response.json()) as SingleDevice;
revalidatePath("/devices");