增加请求错误信息格式化函数

This commit is contained in:
2025-11-12 10:23:33 +08:00
parent 16b106e475
commit 7ef27a4181

View File

@@ -12,6 +12,30 @@ const service = axios.create({
paramsSerializer: (params) => qs.stringify(params),
});
/**
* 格式化错误信息msg
*/
function formatErrorMsg(error: string) {
// 1. 获取原始提示文本(兜底空字符串避免报错)
const originMsg = error
// 2. 定义要匹配的前缀
const exceptionPrefix = "Exception:";
// 3. 判断是否包含目标前缀
if (originMsg.includes(exceptionPrefix)) {
// 截取前缀后的内容 → 去除首尾空格 → 限制最大20个字符
return originMsg
.slice(
originMsg.indexOf(exceptionPrefix) +
exceptionPrefix.length
)
.trim()
.slice(0, 20);
} else {
// 不包含则按原逻辑截取前20个字符
return originMsg.slice(0, 20);
}
}
// 请求拦截器
service.interceptors.request.use(
(config: InternalAxiosRequestConfig) => {
@@ -66,10 +90,11 @@ service.interceptors.response.use(
});
return;
}
ElMessage.error(msg || "系统出错");
return Promise.reject(new Error(msg || "Error"));
ElMessage.error(formatErrorMsg(msg || "Error"));
return Promise.reject(new Error(formatErrorMsg(msg || "Error")));
},
async (error: any) => {
// 非 2xx 状态码处理 401、403、500 等
const { config, response } = error;
if (response) {