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,5 +1,6 @@
import type { Notification } from "$lib/models/notification";
import { ClientResponseError, type ListResult } from "pocketbase";
import { APIError } from "$lib/util/api_util";
import { type ListResult } from "pocketbase";
let notifications: Notification[] = [];
@@ -14,7 +15,8 @@ export async function notifications_index(data: { recipient: string, seen?: bool
})
if (!r.ok) {
throw new ClientResponseError(await r.json())
const response = await r.json();
throw new APIError(r.status, response.message, response.detail)
}
const fetchedNotifications: ListResult<Notification> = await r.json();
@@ -29,10 +31,11 @@ export async function notifications_index(data: { recipient: string, seen?: bool
export async function notifications_mark_as_seen(notification: Notification) {
let r = await fetch('/api/v1/notification/' + notification.id, {
method: 'POST',
body: JSON.stringify(notification),
body: JSON.stringify({ seen: true }),
})
if (!r.ok) {
throw new ClientResponseError(await r.json())
const response = await r.json();
throw new APIError(r.status, response.message, response.detail)
}
}