Show error returned from API (#3980)

This commit is contained in:
qwerty287 2024-08-07 18:47:13 +02:00 committed by GitHub
parent 6c9469f610
commit b0fe208399
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 15 additions and 8 deletions

View file

@ -1,10 +1,9 @@
import { computed, ref } from 'vue'; import { computed, ref } from 'vue';
import useNotifications from '~/compositions/useNotifications'; export function useAsyncAction<T extends unknown[]>(
action: (...a: T) => void | Promise<void>,
const notifications = useNotifications(); onerror: ((error: any) => void) | undefined = undefined,
) {
export function useAsyncAction<T extends unknown[]>(action: (...a: T) => void | Promise<void>) {
const isLoading = ref(false); const isLoading = ref(false);
async function doSubmit(...a: T) { async function doSubmit(...a: T) {
@ -16,7 +15,10 @@ export function useAsyncAction<T extends unknown[]>(action: (...a: T) => void |
try { try {
await action(...a); await action(...a);
} catch (error) { } catch (error) {
notifications.notify({ title: (error as Error).message, type: 'error' }); console.error(error);
if (onerror) {
onerror(error);
}
} }
isLoading.value = false; isLoading.value = false;
} }

View file

@ -52,14 +52,19 @@ export default class ApiClient {
}); });
if (!res.ok) { if (!res.ok) {
let message = res.statusText;
const resText = await res.text();
if (resText) {
message = `${res.statusText}: ${resText}`;
}
const error: ApiError = { const error: ApiError = {
status: res.status, status: res.status,
message: res.statusText, message,
}; };
if (this.onerror) { if (this.onerror) {
this.onerror(error); this.onerror(error);
} }
throw new Error(res.statusText); throw new Error(message);
} }
const contentType = res.headers.get('Content-Type'); const contentType = res.headers.get('Content-Type');