23 lines
565 B
TypeScript
23 lines
565 B
TypeScript
import { z } from "zod";
|
|
|
|
const apiErrorSchema = z.object({
|
|
data: z.object({
|
|
statusCode: z.number(),
|
|
message: z.string(),
|
|
}),
|
|
});
|
|
|
|
export const getApiError = (
|
|
error: unknown,
|
|
defaultMessage: string = "Something went wrong",
|
|
): string => {
|
|
const parsedError = apiErrorSchema.safeParse(error);
|
|
if (parsedError.success) {
|
|
if (parsedError.data.data.statusCode === 500) {
|
|
return `ERR-500: Internal server error`;
|
|
}
|
|
return `ERR-${parsedError.data.data.statusCode}: ${parsedError.data.data.message}`;
|
|
}
|
|
return defaultMessage;
|
|
};
|