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

View file

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