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,11 +1,12 @@
import type { TrailShare } from "$lib/models/trail_share";
import { ClientResponseError, type ListResult } from "pocketbase";
import { APIError } from "$lib/util/api_util";
import { type ListResult } from "pocketbase";
import { writable, type Writable } from "svelte/store";
export const shares: Writable<TrailShare[]> = writable([])
export async function trail_share_index(data: {trail?: string, user?: string}, f: (url: RequestInfo | URL, config?: RequestInit) => Promise<Response> = fetch) {
export async function trail_share_index(data: { trail?: string, user?: string }, f: (url: RequestInfo | URL, config?: RequestInit) => Promise<Response> = fetch) {
const r = await f('/api/v1/trail-share?' + new URLSearchParams({
filter: data.trail ? `trail='${data.trail}'` : data.user ? `user='${data.user}'` : '',
}), {
@@ -13,7 +14,8 @@ export async function trail_share_index(data: {trail?: string, user?: string}, f
})
if (!r.ok) {
throw new ClientResponseError(await r.json())
const response = await r.json();
throw new APIError(r.status, response.message, response.detail)
}
const response: ListResult<TrailShare> = await r.json();
@@ -31,7 +33,8 @@ export async function trail_share_create(share: TrailShare) {
})
if (!r.ok) {
throw new ClientResponseError(await r.json())
const response = await r.json();
throw new APIError(r.status, response.message, response.detail)
}
}
@@ -42,7 +45,8 @@ export async function trail_share_update(share: TrailShare) {
})
if (!r.ok) {
throw new ClientResponseError(await r.json())
const response = await r.json();
throw new APIError(r.status, response.message, response.detail)
}
}
@@ -52,6 +56,7 @@ export async function trail_share_delete(share: TrailShare) {
})
if (!r.ok) {
throw new ClientResponseError(await r.json())
const response = await r.json();
throw new APIError(r.status, response.message, response.detail)
}
}