Proper API error handling

This commit is contained in:
Christian Beutel
2025-01-03 18:05:25 +01:00
parent 9887874ad7
commit 46b5b94219
25 changed files with 366 additions and 251 deletions

View File

@@ -4,6 +4,20 @@ import { pb } from "$lib/pocketbase";
import { ZodError, type ZodSchema } from "zod";
import { RecordListOptionsSchema, RecordIdSchema, RecordOptionsSchema } from "$lib/models/api/base_schema";
export class APIError extends Error {
status: number;
message: string;
detail: any;
constructor(status: number, message: string, detail?: any) {
super();
this.status = status;
this.message = message;
this.detail = detail
}
}
export enum Collection {
users = "users",
categories = "categories",
@@ -108,7 +122,7 @@ export async function remove(event: RequestEvent, collection: Collection) {
export function handleError(e: any) {
if (e instanceof ZodError) {
return error(400, { message: "invalid_params", details: e.issues } as any)
return error(400, { message: "invalid_params", detail: e.issues } as any)
} else if (e instanceof ClientResponseError && e.status > 0) {
return error(e.status as NumericRange<400, 599>, { message: e.message, detail: e.originalError.data } as any)
} else if (e instanceof SyntaxError) {