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

@@ -1,7 +1,8 @@
import type { List, ListFilter } from "$lib/models/list";
import { lists_index, lists_show } from "$lib/stores/list_store";
import { error, type Load } from "@sveltejs/kit";
import { ClientResponseError, type ListResult } from "pocketbase";
import { APIError } from "$lib/util/api_util";
import { error, type Load, type NumericRange } from "@sveltejs/kit";
import { type ListResult } from "pocketbase";
export const load: Load = async ({ params, fetch, url }) => {
const filter: ListFilter = {
@@ -20,9 +21,9 @@ export const load: Load = async ({ params, fetch, url }) => {
lists = { items: [list], page: 1, perPage: 1, totalItems: 1, totalPages: 1 }
} catch (e) {
if (e instanceof ClientResponseError && e.status == 404) {
error(404, {
message: 'Not found'
if (e instanceof APIError) {
error(e.status as NumericRange<400, 599>, {
message: e.status == 404 ? 'Not found' : e.message
});
}
throw e

View File

@@ -1,8 +1,8 @@
import { List } from "$lib/models/list";
import { lists_show } from "$lib/stores/list_store";
import { APIError } from "$lib/util/api_util";
import { getFileURL } from "$lib/util/file_util";
import { error, type Load } from "@sveltejs/kit";
import { ClientResponseError } from "pocketbase";
import { error, type Load, type NumericRange } from "@sveltejs/kit";
export const load: Load = async ({ params, fetch, data }) => {
if (!params.id) {
@@ -20,9 +20,9 @@ export const load: Load = async ({ params, fetch, data }) => {
return { list: list, previewUrl: previewURL }
} catch (e) {
if (e instanceof ClientResponseError) {
throw error(e.status as any, e.message)
} catch (e) {
if (e instanceof APIError) {
throw error(e.status as NumericRange<400, 599>, e.message)
}
throw e
}