Proper API error handling
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import { Comment } from "$lib/models/comment";
|
||||
import type { Trail } from "$lib/models/trail";
|
||||
import { pb } from "$lib/pocketbase";
|
||||
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 comments: Writable<Comment[]> = writable([])
|
||||
@@ -15,7 +16,8 @@ export async function comments_index(trail: Trail) {
|
||||
})
|
||||
|
||||
if (!r.ok) {
|
||||
throw new ClientResponseError(await r.json())
|
||||
const response = await r.json();
|
||||
throw new APIError(r.status, response.message, response.detail)
|
||||
}
|
||||
|
||||
const fetchedComments: ListResult<Comment> = await r.json();
|
||||
@@ -38,7 +40,8 @@ export async function comments_create(comment: Comment) {
|
||||
})
|
||||
|
||||
if (!r.ok) {
|
||||
throw new ClientResponseError(await r.json())
|
||||
const response = await r.json();
|
||||
throw new APIError(r.status, response.message, response.detail)
|
||||
}
|
||||
|
||||
const model: Comment = await r.json();
|
||||
@@ -53,7 +56,8 @@ export async function comments_update(comment: Comment) {
|
||||
})
|
||||
|
||||
if (!r.ok) {
|
||||
throw new ClientResponseError(await r.json())
|
||||
const response = await r.json();
|
||||
throw new APIError(r.status, response.message, response.detail)
|
||||
}
|
||||
|
||||
const model: Comment = await r.json();
|
||||
@@ -67,6 +71,7 @@ export async function comments_delete(comment: Comment) {
|
||||
})
|
||||
|
||||
if (!r.ok) {
|
||||
throw new ClientResponseError(await r.json())
|
||||
const response = await r.json();
|
||||
throw new APIError(r.status, response.message, response.detail)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user