mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-01 15:33:12 +00:00
* chore: upgrade frontend application dependencies * chore: upgrade frontend infrastructure dependencies * fix: align lockfiles with CI npm * fix: preserve ky error semantics * fix: sanitize timeout error message
45 lines
1.2 KiB
TypeScript
45 lines
1.2 KiB
TypeScript
import { isHTTPError, isNetworkError, isTimeoutError } from "ky";
|
|
|
|
export function mapHttpStatusToMessage(status: number, data?: unknown): string {
|
|
if (status === 401) {
|
|
return "You are not authorized to access this page.";
|
|
}
|
|
if (status === 403) {
|
|
return "You do not have permission to perform this action.";
|
|
}
|
|
if (status === 404) {
|
|
return "The requested resource was not found.";
|
|
}
|
|
if (status === 500) {
|
|
return "A server error occurred. Please try again later.";
|
|
}
|
|
|
|
if (data && typeof data === "object") {
|
|
const record = data as Record<string, unknown>;
|
|
const message = record.message ?? record.Message ?? record.error;
|
|
if (typeof message === "string" && message.length > 0) {
|
|
return message;
|
|
}
|
|
}
|
|
|
|
return "An error occurred";
|
|
}
|
|
|
|
export function normalizeApiRequestError(error: Error): Error {
|
|
if (isHTTPError(error)) {
|
|
error.message = mapHttpStatusToMessage(error.response.status, error.data);
|
|
return error;
|
|
}
|
|
|
|
if (isNetworkError(error)) {
|
|
error.message = "No response from server. Please check your connection.";
|
|
return error;
|
|
}
|
|
|
|
if (isTimeoutError(error)) {
|
|
error.message = "The request timed out. Please try again.";
|
|
return error;
|
|
}
|
|
|
|
return error;
|
|
}
|