Skip to content

Commit

Permalink
Merge pull request #126 from sj-distributor/enhance-error-message
Browse files Browse the repository at this point in the history
Enhance error message
  • Loading branch information
mindy0-0 authored Sep 6, 2024
2 parents 6b066e3 + ea0cdbf commit a72874f
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 4 deletions.
7 changes: 7 additions & 0 deletions web/src/i18n/ i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import operationLog from "./language/pages/operation-log";
import portraitList from "./language/pages/portrait-list";
import userList from "./language/pages/user-list";
import userPermissions from "./language/pages/user-permissions";
import errorMessage from "./language/pages/error-message";

i18n.use(initReactI18next).init({
resources: {
Expand Down Expand Up @@ -53,6 +54,9 @@ i18n.use(initReactI18next).init({
homeMenu: {
...homeMenu.en,
},
errorMessage: {
...errorMessage.en,
},
},
ch: {
operationLog: {
Expand Down Expand Up @@ -91,6 +95,9 @@ i18n.use(initReactI18next).init({
licensePlateManagement: {
...licensePlateManagement.ch,
},
errorMessage: {
...errorMessage.ch,
},
},
},
lng: "en",
Expand Down
6 changes: 6 additions & 0 deletions web/src/i18n/language/keys/error-message-keys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
DUPLICATE_CAMERA: "DuplicateCamera",
DATABASE_ERROR: "DatabaseError",
ERROR_CAMERA: "ErrorCamera",
INSUFFICIENT_RESOURCES: "InsufficientResources",
};
17 changes: 17 additions & 0 deletions web/src/i18n/language/pages/error-message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import KEYS from "../keys/error-message-keys";

export default {
en: {
[KEYS.DUPLICATE_CAMERA]: "Reduplicative camera,please check and resubmit",
[KEYS.DATABASE_ERROR]: "gateway database error,please try again later",
[KEYS.ERROR_CAMERA]:
"camera not found or not online,please try again later",
[KEYS.INSUFFICIENT_RESOURCES]: "No node free,please try again later",
},
ch: {
[KEYS.DUPLICATE_CAMERA]: "存在重复camera,請檢查後重新提交",
[KEYS.DATABASE_ERROR]: "當前数据库异常,請稍候重試",
[KEYS.ERROR_CAMERA]: "當前摄像头信息错误或离线,請稍候重試",
[KEYS.INSUFFICIENT_RESOURCES]: "當前资源不够,請稍候重試",
},
};
5 changes: 3 additions & 2 deletions web/src/pages/equipment/equipment-list/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
import { IPageDto } from "@/services/dtos/public";

import { IBondOrNot, IOnlineOrNot, IOptionDto } from "./props";
import { getErrorMessage } from "@/utils/error-message";

export const useAction = () => {
const { t, language, myPermissions } = useAuth();
Expand Down Expand Up @@ -152,7 +153,7 @@ export const useAction = () => {
initGetEquipmentList();
form.setFieldsValue(initialEquipmentData);
})
.catch((err) => message.error(`更新失敗:${(err as Error).message}`))
.catch((err) => message.error(getErrorMessage((err as Error).message)))
.finally(() => setConfirmLoading(false));
};

Expand All @@ -167,7 +168,7 @@ export const useAction = () => {
setPageDto((prev) => ({ ...prev, PageIndex: 1 }));
form.setFieldsValue(initialEquipmentData);
})
.catch((err) => message.error(`新增失敗:${(err as Error).message}`))
.catch((err) => message.error(getErrorMessage((err as Error).message)))
.finally(() => setConfirmLoading(false));
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
} from "@/services/api/monitor";
import { GetEquipmentPage } from "@/services/api/equipment/list";
import dayjs from "dayjs";
import { getErrorMessage } from "@/utils/error-message";

export const useAction = () => {
const { t } = useAuth();
Expand Down Expand Up @@ -422,7 +423,7 @@ export const useAction = () => {
navigate("/monitor");
})
.catch((err) => {
message.error(`创建失败:${(err as Error).message}`);
message.error(getErrorMessage((err as Error).message));
})
.finally(() => setSubmitLoadin(false))
: MonitorSettingUpdate(data)
Expand All @@ -431,7 +432,7 @@ export const useAction = () => {
navigate("/monitor");
})
.catch((err) => {
message.error(`编辑失败:${(err as Error).message}`);
message.error(getErrorMessage((err as Error).message));
})
.finally(() => setSubmitLoadin(false));
});
Expand Down
29 changes: 29 additions & 0 deletions web/src/utils/error-message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import KEYS from "@/i18n/language/keys/error-message-keys";
import i18n from "i18next";

const ERROR_KEYWORDS = {
DUPLICATE_CAMERA: "Reduplicative camera",
DATABASE_ERROR: "gateway database error",
ERROR_CAMERA: "camera not found or not online",
INSUFFICIENT_RESOURCES: "insufficient resource",
};

export const getErrorMessage = (error: string) => {
const { t } = i18n;

const ERROR_MESSAGES = {
DUPLICATE_CAMERA: t(KEYS.DUPLICATE_CAMERA, { ns: "errorMessage" }),
DATABASE_ERROR: t(KEYS.DATABASE_ERROR, { ns: "errorMessage" }),
ERROR_CAMERA: t(KEYS.ERROR_CAMERA, { ns: "errorMessage" }),
INSUFFICIENT_RESOURCES: t(KEYS.INSUFFICIENT_RESOURCES, {
ns: "errorMessage",
}),
};

for (const [key, keyword] of Object.entries(ERROR_KEYWORDS)) {
if (error.includes(keyword)) {
return ERROR_MESSAGES[key as keyof typeof ERROR_MESSAGES];
}
}
return error;
};

0 comments on commit a72874f

Please sign in to comment.