mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-07-02 03:38:22 +00:00
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
Some checks failed
Build and Push Docker Images / Build and Push Docker Images (push) Failing after 1m39s
This commit is contained in:
@ -6,3 +6,33 @@ export async function tryCatch<T, E = Error>(promise: T | Promise<T>) {
|
||||
return [error as E, null] as const;
|
||||
}
|
||||
}
|
||||
|
||||
export async function handleApiResponse<T>(
|
||||
response: Response,
|
||||
fnName?: string,
|
||||
) {
|
||||
const responseData = await response.json();
|
||||
if (response.status === 401) {
|
||||
throw new Error("UNAUTHORIZED");
|
||||
}
|
||||
|
||||
if (response.status === 403) {
|
||||
throw new Error(
|
||||
responseData.message ||
|
||||
"Forbidden; you do not have permission to access this resource.",
|
||||
);
|
||||
}
|
||||
|
||||
if (response.status === 429) {
|
||||
throw new Error(
|
||||
responseData.message || "Too many requests; please try again later.",
|
||||
);
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
console.log(`API Error Response from ${fnName}:`, responseData);
|
||||
throw new Error(responseData.message || "Something went wrong.");
|
||||
}
|
||||
|
||||
return responseData as T;
|
||||
}
|
||||
|
Reference in New Issue
Block a user