mirror of
https://github.com/ccmjga/zhilu-admin
synced 2026-03-14 05:33:42 +08:00
init
This commit is contained in:
50
frontend/src/utils/errorHandler.ts
Normal file
50
frontend/src/utils/errorHandler.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import type { ComponentPublicInstance } from "vue";
|
||||
import type { Router } from "vue-router";
|
||||
import { RoutePath } from "../router/constants";
|
||||
import {
|
||||
ForbiddenError,
|
||||
InternalServerError,
|
||||
SystemError,
|
||||
UnAuthError,
|
||||
} from "../types/error";
|
||||
|
||||
const makeErrorHandler =
|
||||
(
|
||||
router: Router,
|
||||
signOut: () => void,
|
||||
showAlert: ({
|
||||
content,
|
||||
level,
|
||||
}: {
|
||||
content: string;
|
||||
level: "info" | "success" | "warning" | "error";
|
||||
}) => void,
|
||||
) =>
|
||||
(err: unknown, instance: ComponentPublicInstance | null, info: string) => {
|
||||
console.error(err);
|
||||
if (err instanceof UnAuthError) {
|
||||
signOut();
|
||||
router.push(RoutePath.LOGIN);
|
||||
showAlert({
|
||||
level: "error",
|
||||
content: err.message,
|
||||
});
|
||||
} else if (err instanceof ForbiddenError) {
|
||||
showAlert({
|
||||
level: "error",
|
||||
content: err.message,
|
||||
});
|
||||
} else if (err instanceof SystemError) {
|
||||
showAlert({
|
||||
level: "error",
|
||||
content: err.message,
|
||||
});
|
||||
} else if (err instanceof InternalServerError) {
|
||||
showAlert({
|
||||
level: "error",
|
||||
content: err.detail ?? err.message,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export default makeErrorHandler;
|
||||
Reference in New Issue
Block a user