refactor: 添加英文的异常信息
parent
21e7058a75
commit
a02a4c7e4f
|
@ -252,31 +252,21 @@ export function createAdapter(platform: AxiosPlatform): AxiosAdapter {
|
||||||
}
|
}
|
||||||
|
|
||||||
function transformResult(result: AnyObject): void {
|
function transformResult(result: AnyObject): void {
|
||||||
result.status = result.status || isUndefined(result.data) ? 400 : 200;
|
result.status =
|
||||||
if (result.statusCode) {
|
result.status ?? result.statusCode ?? isUndefined(result.data)
|
||||||
result.status = result.statusCode;
|
? 400
|
||||||
delete result.statusCode;
|
: 200;
|
||||||
}
|
result.statusText =
|
||||||
|
result.status === 200
|
||||||
|
? 'OK'
|
||||||
|
: result.status === 400
|
||||||
|
? 'Bad Adapter'
|
||||||
|
: result.errMsg;
|
||||||
|
result.headers = result.headers || result.header;
|
||||||
|
|
||||||
if (isUndefined(result.statusText)) {
|
if (result.statusCode) delete result.statusCode;
|
||||||
result.statusText =
|
if (result.errMsg) delete result.errMsg;
|
||||||
result.status === 200
|
if (result.header) delete result.header;
|
||||||
? 'OK'
|
|
||||||
: result.status === 400
|
|
||||||
? 'Bad Adapter'
|
|
||||||
: '';
|
|
||||||
}
|
|
||||||
|
|
||||||
result.headers = result.headers || {};
|
|
||||||
if (result.header) {
|
|
||||||
result.headers = result.header;
|
|
||||||
delete result.header;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (result.errMsg) {
|
|
||||||
result.statusText = result.errMsg;
|
|
||||||
delete result.errMsg;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function transformOptions(
|
function transformOptions(
|
||||||
|
|
|
@ -60,13 +60,10 @@ export function request<TData = unknown>(
|
||||||
const response = _ as AxiosResponse<TData>;
|
const response = _ as AxiosResponse<TData>;
|
||||||
response.config = config;
|
response.config = config;
|
||||||
response.request = adapterTask;
|
response.request = adapterTask;
|
||||||
if (
|
if (config.validateStatus?.(response.status) ?? true) {
|
||||||
!isFunction(config.validateStatus) ||
|
|
||||||
config.validateStatus(response.status)
|
|
||||||
) {
|
|
||||||
resolve(response);
|
resolve(response);
|
||||||
} else {
|
} else {
|
||||||
catchError('请求失败', response);
|
catchError('validate status fail', response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,7 +72,7 @@ export function request<TData = unknown>(
|
||||||
responseError.isFail = true;
|
responseError.isFail = true;
|
||||||
responseError.config = config;
|
responseError.config = config;
|
||||||
responseError.request = adapterTask;
|
responseError.request = adapterTask;
|
||||||
catchError('网络错误', responseError);
|
catchError(responseError.statusText, responseError);
|
||||||
}
|
}
|
||||||
|
|
||||||
function catchError(
|
function catchError(
|
||||||
|
@ -95,9 +92,7 @@ export function request<TData = unknown>(
|
||||||
if (isPlainObject(adapterTask)) {
|
if (isPlainObject(adapterTask)) {
|
||||||
tryToggleProgressUpdate(adapterConfig, adapterTask.offProgressUpdate);
|
tryToggleProgressUpdate(adapterConfig, adapterTask.offProgressUpdate);
|
||||||
|
|
||||||
if (isFunction(adapterTask.abort)) {
|
adapterTask?.abort?.();
|
||||||
adapterTask.abort();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
reject(reason);
|
reject(reason);
|
||||||
|
|
|
@ -65,6 +65,6 @@ describe('src/core/request.ts', () => {
|
||||||
url: '/test',
|
url: '/test',
|
||||||
method: 'get',
|
method: 'get',
|
||||||
}),
|
}),
|
||||||
).rejects.toThrowErrorMatchingInlineSnapshot('"网络错误"');
|
).rejects.toThrowErrorMatchingInlineSnapshot('"FAIL"');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue