feat: better error messages
This commit is contained in:
22
app/utils/api.ts
Normal file
22
app/utils/api.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
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;
|
||||
};
|
||||
Reference in New Issue
Block a user