mirror of
https://github.com/warmbly/warmbly.git
synced 2026-09-11 00:05:01 +00:00
46 lines
1.0 KiB
TypeScript
46 lines
1.0 KiB
TypeScript
import axios from "axios";
|
|
import { AuthError } from "@/lib/errors/auth";
|
|
|
|
export interface AppError {
|
|
error: string;
|
|
message: string;
|
|
status?: number;
|
|
redirect?: boolean;
|
|
}
|
|
|
|
export function normalizeError(error: unknown): AppError {
|
|
if (error instanceof AuthError) {
|
|
return {
|
|
error: "Authentication Required",
|
|
message: error.message,
|
|
status: 401,
|
|
redirect: true,
|
|
};
|
|
}
|
|
|
|
if (axios.isAxiosError(error)) {
|
|
if (!error.response) {
|
|
// network, CORS, or timeout
|
|
return {
|
|
error: "Network Error",
|
|
message: "Please check your connection.",
|
|
};
|
|
}
|
|
|
|
const status = error.response.status;
|
|
const data = error.response.data;
|
|
|
|
return {
|
|
error: data.error || "Unknown Error",
|
|
message: data.message || "Unexpected error occured.",
|
|
status,
|
|
}
|
|
}
|
|
|
|
return {
|
|
error: "Unknown Error",
|
|
message: "Unexpected error occurred.",
|
|
};
|
|
}
|
|
|