Files
pihka-al/app/utils/api.ts
Pihkaal 90a81f353d
All checks were successful
ci / ci (22, ubuntu-latest) (push) Successful in 9m44s
Build and Push Docker Image / build (push) Successful in 1m29s
chore: fix eslint and make nuxt typecheck happpy
2026-03-25 21:42:20 +01:00

26 lines
679 B
TypeScript

import { z } from "zod";
import type { InternalApi } from "nitropack/types";
export type Link = InternalApi["/api/links"]["get"][number];
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;
};