mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-11-25 19:31: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 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;
|
||||
}
|
||||
|
|
|
@ -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');
|
||||
|
|
Loading…
Reference in a new issue