1
0
Fork 0
mirror of https://github.com/morpheus65535/bazarr synced 2024-12-27 10:07:22 +00:00

Improve error handling on UI

This commit is contained in:
LASER-Yi 2022-06-14 22:13:28 +08:00
parent 4640badd4b
commit c3645c9024
3 changed files with 48 additions and 12 deletions

View file

@ -1,8 +1,19 @@
import SocketIO from "@/modules/socketio"; import SocketIO from "@/modules/socketio";
import { notification } from "@/modules/task";
import { LOG } from "@/utilities/console"; import { LOG } from "@/utilities/console";
import { setLoginRequired } from "@/utilities/event"; import { setLoginRequired } from "@/utilities/event";
import { showNotification } from "@mantine/notifications";
import Axios, { AxiosError, AxiosInstance, CancelTokenSource } from "axios"; import Axios, { AxiosError, AxiosInstance, CancelTokenSource } from "axios";
import { Environment } from "../../utilities"; import { Environment } from "../../utilities";
function GetErrorMessage(data: unknown, defaultMsg = "Unknown error"): string {
if (typeof data === "string") {
return data;
} else {
return defaultMsg;
}
}
class BazarrClient { class BazarrClient {
axios!: AxiosInstance; axios!: AxiosInstance;
source!: CancelTokenSource; source!: CancelTokenSource;
@ -36,32 +47,43 @@ class BazarrClient {
if (resp.status >= 200 && resp.status < 300) { if (resp.status >= 200 && resp.status < 300) {
return Promise.resolve(resp); return Promise.resolve(resp);
} else { } else {
this.handleError(resp.status); const error: BackendError = {
code: resp.status,
message: GetErrorMessage(resp.data),
};
this.handleError(error);
return Promise.reject(resp); return Promise.reject(resp);
} }
}, },
(error: AxiosError) => { (error: AxiosError) => {
if (error.response) { const message = GetErrorMessage(
const response = error.response; error.response?.data,
this.handleError(response.status); "You have disconnected from the server"
} else { );
error.message = "You have disconnected to Bazarr backend";
} const backendError: BackendError = {
code: error.response?.status ?? 500,
message,
};
error.message = backendError.message;
this.handleError(backendError);
return Promise.reject(error); return Promise.reject(error);
} }
); );
} }
handleError(code: number) { handleError(error: BackendError) {
const { code, message } = error;
switch (code) { switch (code) {
case 401: case 401:
setLoginRequired(); setLoginRequired();
break; break;
case 500:
break;
default:
break;
} }
LOG("error", "A error has occurred", code);
showNotification(notification.error(`Error ${code}`, message));
} }
} }

View file

@ -18,6 +18,15 @@ export const notification = {
}; };
}, },
error: (title: string, message: string): NotificationProps => {
return {
title,
message,
color: "red",
autoClose: 7 * 1000,
};
},
PROGRESS_TIMEOUT: 10 * 1000, PROGRESS_TIMEOUT: 10 * 1000,
progress: { progress: {

View file

@ -275,3 +275,8 @@ type ItemSearchResult = Partial<SeriesIdType> &
title: string; title: string;
year: string; year: string;
}; };
type BackendError = {
code: number;
message: string;
};