🔀 合并 fix/import-error-message-blob
修复导入失败时前端误报「系统错误」,根因有两层: - axios 的 validateStatus 允许 200-500,HTTP 400 走的是成功分支而不是错误分支; - 导入请求用 responseType='blob',res.data 是 Blob,读不到 code/msg, 于是状态码退回 res.status、文案落到「系统错误」兜底。 改法:按 Content-Type 判断,声明为 JSON 的响应体才把 Blob 读成文本解析回 对象;真·文件流(Excel 明细)保持 Blob 原样走下载分支。 另统一 notifiedError() 给 Error 打 _notified 标记,业务层据此避免同一条 错误被弹两次。
This commit is contained in:
@@ -28,6 +28,34 @@ const parseBlobJson = async blob => {
|
||||
return text ? JSON.parse(text) : {};
|
||||
};
|
||||
|
||||
/**
|
||||
* 解析导入失败时的错误提示文案。
|
||||
*
|
||||
* 背景:导入接口统一用 `responseType: 'blob'` 请求,因此**非 2xx 响应体也是 Blob**。
|
||||
* axios 的错误分支不做解析,直接 reject,`error.message` 只有
|
||||
* 「Request failed with status code 400」这类通用文案(或全局兜底的「系统错误」),
|
||||
* 用户会误以为系统崩了——而后端其实已返回了明确的中文业务提示(如「导入数据不能为空」)。
|
||||
*
|
||||
* 这里补上这一步:从错误响应的 Blob 里取出后端 `msg`,取不到再退回通用文案。
|
||||
*/
|
||||
const resolveErrorMessage = async error => {
|
||||
const blob = error?.response?.data ?? error?.data;
|
||||
if (blob instanceof Blob) {
|
||||
try {
|
||||
const result = await parseBlobJson(blob);
|
||||
if (result?.msg) return result.msg;
|
||||
} catch (ignored) {
|
||||
// 响应体不是 JSON(如网关返回的 HTML 错误页),退回通用文案
|
||||
}
|
||||
}
|
||||
const fallback = error?.response?.data?.msg || error?.message;
|
||||
// axios 的英文通用文案对用户没有意义,统一换成中文提示
|
||||
if (!fallback || /Request failed with status code/i.test(fallback)) {
|
||||
return '导入失败,请检查导入文件后重试';
|
||||
}
|
||||
return fallback;
|
||||
};
|
||||
|
||||
/**
|
||||
* 加工后端返回的导入失败明细 Excel。
|
||||
* 若传入 failDetailDecorator(函数),则动态加载 exceljs 读取 blob 流,
|
||||
@@ -125,7 +153,11 @@ export const handleImportExcel = async (vm, option, column, businessName, onSucc
|
||||
option.onSuccess(res);
|
||||
}
|
||||
} catch (error) {
|
||||
vm.$message.error(error.message || '导入失败');
|
||||
// 拦截器已提示过的错误(如后端 400 返回的业务提示)不再重复弹一次,
|
||||
// 只保留一次清晰的中文提示。
|
||||
if (!error?._notified) {
|
||||
vm.$message.error(await resolveErrorMessage(error));
|
||||
}
|
||||
if (typeof option.onError === 'function') {
|
||||
option.onError(error);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user