mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-09-25 05:00:05 +00:00
Show error returned from API (#3980)
This commit is contained in:
parent
6c9469f610
commit
b0fe208399
2 changed files with 15 additions and 8 deletions
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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');
|
||||||
|
|
Loading…
Reference in a new issue