mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-03 08:03:22 +00:00
Gate CompDoc on selected service, use real pm titles only, make Address editable via location update, and wire media to the board API with categories instead of blob state.
55 lines
1.4 KiB
TypeScript
55 lines
1.4 KiB
TypeScript
import { isHTTPError, isNetworkError, isTimeoutError } from "ky";
|
|
|
|
export class ApiError extends Error {
|
|
readonly status: number;
|
|
|
|
constructor(message: string, status: number) {
|
|
super(message);
|
|
this.name = "ApiError";
|
|
this.status = status;
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|