Proper API error handling
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import { SummitLog, type SummitLogFilter } from "$lib/models/summit_log";
|
||||
import { pb } from "$lib/pocketbase";
|
||||
import { ClientResponseError, type ListResult } from "pocketbase";
|
||||
import { type ListResult } from "pocketbase";
|
||||
import { writable, type Writable } from "svelte/store";
|
||||
import { fetchGPX } from "./trail_store";
|
||||
import { APIError } from "$lib/util/api_util";
|
||||
|
||||
export const summitLog: Writable<SummitLog> = writable(new SummitLog(new Date().toISOString().substring(0, 10)));
|
||||
export const summitLogs: Writable<SummitLog[]> = writable([]);
|
||||
@@ -21,7 +22,8 @@ export async function summit_logs_index(author: string, filter?: SummitLogFilter
|
||||
})
|
||||
|
||||
if (!r.ok) {
|
||||
throw new ClientResponseError(await r.json())
|
||||
const response = await r.json();
|
||||
throw new APIError(r.status, response.message, response.detail)
|
||||
}
|
||||
|
||||
const fetchedSummitLogs: ListResult<SummitLog> = await r.json();
|
||||
@@ -52,7 +54,8 @@ export async function summit_logs_create(summitLog: SummitLog, f: (url: RequestI
|
||||
})
|
||||
|
||||
if (!r.ok) {
|
||||
throw new ClientResponseError(await r.json())
|
||||
const response = await r.json();
|
||||
throw new APIError(r.status, response.message, response.detail)
|
||||
}
|
||||
|
||||
let model: SummitLog = await r.json();
|
||||
@@ -67,6 +70,11 @@ export async function summit_logs_create(summitLog: SummitLog, f: (url: RequestI
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
})
|
||||
|
||||
if (!r.ok) {
|
||||
const response = await r.json();
|
||||
throw new APIError(r.status, response.message, response.detail)
|
||||
}
|
||||
}
|
||||
|
||||
if (summitLog._photos && summitLog._photos.length) {
|
||||
@@ -81,13 +89,15 @@ export async function summit_logs_create(summitLog: SummitLog, f: (url: RequestI
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
})
|
||||
|
||||
if (!r.ok) {
|
||||
const response = await r.json();
|
||||
throw new APIError(r.status, response.message, response.detail)
|
||||
}
|
||||
}
|
||||
|
||||
if (r.ok) {
|
||||
return model;
|
||||
} else {
|
||||
throw new ClientResponseError(await r.json())
|
||||
}
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
export async function summit_logs_update(oldSummitLog: SummitLog, newSummitLog: SummitLog) {
|
||||
@@ -99,7 +109,8 @@ export async function summit_logs_update(oldSummitLog: SummitLog, newSummitLog:
|
||||
})
|
||||
|
||||
if (!r.ok) {
|
||||
throw new ClientResponseError(await r.json())
|
||||
const response = await r.json();
|
||||
throw new APIError(r.status, response.message, response.detail)
|
||||
}
|
||||
|
||||
const formData = new FormData()
|
||||
@@ -123,23 +134,25 @@ export async function summit_logs_update(oldSummitLog: SummitLog, newSummitLog:
|
||||
body: formData,
|
||||
})
|
||||
|
||||
if (r.ok) {
|
||||
return await r.json();
|
||||
} else {
|
||||
throw new ClientResponseError(await r.json())
|
||||
if (!r.ok) {
|
||||
const response = await r.json();
|
||||
throw new APIError(r.status, response.message, response.detail)
|
||||
}
|
||||
|
||||
return await r.json();
|
||||
}
|
||||
|
||||
export async function summit_logs_delete(summitLog: SummitLog) {
|
||||
const r = await fetch('/api/v1/summit-log/' + summitLog.id, {
|
||||
method: 'DELETE',
|
||||
})
|
||||
|
||||
if (r.ok) {
|
||||
return await r.json();
|
||||
} else {
|
||||
throw new ClientResponseError(await r.json())
|
||||
if (!r.ok) {
|
||||
const response = await r.json();
|
||||
throw new APIError(r.status, response.message, response.detail)
|
||||
}
|
||||
|
||||
return await r.json();
|
||||
|
||||
}
|
||||
|
||||
function buildFilterText(filter: SummitLogFilter,): string {
|
||||
|
||||
Reference in New Issue
Block a user