Skip to content

Commit

Permalink
perf: 优化网络请求错误抛出
Browse files Browse the repository at this point in the history
  • Loading branch information
moonrailgun committed Jan 21, 2023
1 parent c473a70 commit c0ecd5e
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions client/shared/api/request.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios, { AxiosRequestConfig } from 'axios';
import _get from 'lodash/get';
import _isFunction from 'lodash/isFunction';
import { t } from '../i18n';
import { getErrorHook, tokenGetter } from '../manager/request';
import { getServiceUrl, onServiceUrlChange } from '../manager/service';

Expand Down Expand Up @@ -52,8 +53,23 @@ function createRequest() {
},
(err) => {
// 尝试获取错误信息
const errorMsg: string = _get(err, 'response.data.message');
const code: number = _get(err, 'response.data.code');
const responseData = _get(err, 'response.data') ?? {};
let errorMsg: string = responseData.message;
const code: number = responseData.code;

if (responseData.type === 'VALIDATION_ERROR') {
// 校验失败
errorMsg = t('请求参数校验失败');

if (Array.isArray(responseData.data)) {
try {
errorMsg += `: ${responseData.data
.map((item: any) => item.field)
.join(', ')}`;
} catch (e) {}
}
}

if (_isFunction(getErrorHook)) {
const isContinue = getErrorHook(err);
if (isContinue === false) {
Expand Down

0 comments on commit c0ecd5e

Please sign in to comment.