video-flow-b/api/errorHandle.ts

38 lines
1.3 KiB
TypeScript

import { message } from "antd";
import { debounce } from "lodash";
/**
* HTTP错误码对应的用户友好提示信息
*/
const HTTP_ERROR_MESSAGES: Record<number, string> = {
0: "Please try again if the network is abnormal. If it happens again, please contact us.",
400: "Request parameter error, please check your input.",
401: "Login expired, please log in again.",
403: "Insufficient permissions to access this resource.",
404: "Requested resource does not exist.",
408: "Request timeout, please try again.",
429: "Too many requests, please try again later.",
502: "Gateway error, please try again later.",
503: "Service temporarily unavailable, please try again later.",
504: "Gateway timeout, please try again later.",
};
/**
* 默认错误提示信息
*/
const DEFAULT_ERROR_MESSAGE =
"Please try again if the network is abnormal. If it happens again, please contact us.";
/**
* 根据错误码显示对应的提示信息
* @param code - HTTP错误码
* @param customMessage - 自定义错误信息(可选)
*/
export const errorHandle = debounce(
(code: number, customMessage?: string): void => {
const errorMessage =
customMessage || HTTP_ERROR_MESSAGES[code] || DEFAULT_ERROR_MESSAGE;
message.error(errorMessage);
},
100
);