forked from 77media/video-flow
69 lines
2.0 KiB
TypeScript
69 lines
2.0 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.",
|
|
4001: "Insufficient points, please recharge.",
|
|
};
|
|
|
|
/**
|
|
* 默认错误提示信息
|
|
*/
|
|
const DEFAULT_ERROR_MESSAGE = "Network error, please try again. If the problem persists, please contact us.";
|
|
|
|
/**
|
|
* 特殊错误码的处理函数
|
|
*/
|
|
const ERROR_HANDLERS: Record<number, () => void> = {
|
|
401: () => {
|
|
// 清除本地存储的 token
|
|
localStorage.removeItem('token');
|
|
// 跳转到登录页面
|
|
window.location.href = '/login';
|
|
},
|
|
4001: () => {
|
|
// 显示积分不足通知
|
|
import('../utils/notifications').then(({ showInsufficientPointsNotification }) => {
|
|
showInsufficientPointsNotification();
|
|
});
|
|
}
|
|
};
|
|
|
|
/**
|
|
* 根据错误码显示对应的提示信息并执行相应处理
|
|
* @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({
|
|
content: errorMessage,
|
|
duration: 3,
|
|
className: 'custom-error-message'
|
|
});
|
|
|
|
// 执行特殊错误码的处理函数
|
|
const handler = ERROR_HANDLERS[code];
|
|
if (handler) {
|
|
handler();
|
|
}
|
|
},
|
|
100
|
|
);
|