@cainiaofe/cn-utils
Version:
菜鸟前端基础工具库
53 lines (52 loc) • 1.45 kB
JavaScript
import { isError, isObject } from "../../common/type";
/**
* 兼容3种数据结构
* error对象,axios原始返回值,菜鸟标准数据结构
*/
export var getErrorMsg = function (error, defaultErrorMsg) {
var _a;
if (!error)
return defaultErrorMsg;
// 场景1,error对象
if (isError(error)) {
return error.message || defaultErrorMsg;
}
if (typeof error !== 'object')
return defaultErrorMsg;
// 场景2 axios原始返回值
// {
// "data": {
// "msg": "",
// "traceId": "213e1e0c16787088879467040e5cf9",
// "success": false,
// "errorCode": "0000",
// "errorMsg": "mock 抛错",
// },
// "status": 200,
// "statusText": "",
// "headers": {
// ...
// },
// "config": {
// ...
// },
// "request": {
// ...
// },
// }
if (isObject(error) && 'request' in error && 'config' in error) {
return ((_a = error.data) === null || _a === void 0 ? void 0 : _a.errorMsg) || defaultErrorMsg;
}
// 场景3 菜鸟标准数据结构
// {
// "msg": "",
// "traceId": "213e1e0c16787088879467040e5cf9",
// "success": false,
// "errorCode": "0000",
// "errorMsg": "mock 抛错",
// }
if (isObject(error) && 'errorMsg' in error) {
return error.errorMsg || defaultErrorMsg;
}
return defaultErrorMsg;
};