feat: 适配器异常处理成响应异常
parent
adb9dbe88f
commit
be17ba7e68
|
@ -19,7 +19,7 @@ lib/
|
||||||
*.test.js
|
*.test.js
|
||||||
*.test.d.ts
|
*.test.d.ts
|
||||||
*.test.js.map
|
*.test.js.map
|
||||||
coverage
|
.coverage
|
||||||
|
|
||||||
# eslint
|
# eslint
|
||||||
.eslintcache
|
.eslintcache
|
||||||
|
|
|
@ -2,6 +2,7 @@ import {
|
||||||
isEmptyArray,
|
isEmptyArray,
|
||||||
isFunction,
|
isFunction,
|
||||||
isPlainObject,
|
isPlainObject,
|
||||||
|
isString,
|
||||||
isUndefined,
|
isUndefined,
|
||||||
} from './helpers/isTypes';
|
} from './helpers/isTypes';
|
||||||
import { assert } from './helpers/error';
|
import { assert } from './helpers/error';
|
||||||
|
@ -57,6 +58,19 @@ export interface AxiosAdapterResponseError extends AnyObject {
|
||||||
* 响应头
|
* 响应头
|
||||||
*/
|
*/
|
||||||
headers: AnyObject;
|
headers: AnyObject;
|
||||||
|
/**
|
||||||
|
* 错误数据
|
||||||
|
*/
|
||||||
|
data?: {
|
||||||
|
/**
|
||||||
|
* 错误信息
|
||||||
|
*/
|
||||||
|
errMsg: string;
|
||||||
|
/**
|
||||||
|
* Errno错误码
|
||||||
|
*/
|
||||||
|
errno: number;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AxiosAdapterRequestConfig extends AnyObject {
|
export interface AxiosAdapterRequestConfig extends AnyObject {
|
||||||
|
@ -253,22 +267,25 @@ export function createAdapter(platform: AxiosPlatform) {
|
||||||
return download(options);
|
return download(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
function transformResult(result: AnyObject): void {
|
function transformResponse(response: AnyObject): void {
|
||||||
result.status =
|
response.status = response.status ?? response.statusCode;
|
||||||
result.status ??
|
response.statusText = 'OK';
|
||||||
result.statusCode ??
|
|
||||||
(isUndefined(result.data) ? 400 : 200);
|
|
||||||
result.statusText =
|
|
||||||
result.status === 200
|
|
||||||
? 'OK'
|
|
||||||
: result.status === 400
|
|
||||||
? 'Bad Adapter'
|
|
||||||
: result.errMsg;
|
|
||||||
result.headers = result.headers || result.header;
|
|
||||||
|
|
||||||
if (result.statusCode) delete result.statusCode;
|
if (isUndefined(response.status)) {
|
||||||
if (result.errMsg) delete result.errMsg;
|
response.status = 400;
|
||||||
if (result.header) delete result.header;
|
response.statusText = 'Fail Adapter';
|
||||||
|
}
|
||||||
|
|
||||||
|
response.headers = response.headers ?? response.header ?? {};
|
||||||
|
|
||||||
|
if (isUndefined(response.data) && isString(response.errMsg)) {
|
||||||
|
response.data = {
|
||||||
|
errMsg: response.errMsg,
|
||||||
|
errno: response.errno,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanResponse(response, ['statusCode', 'errMsg', 'errno', 'header']);
|
||||||
}
|
}
|
||||||
|
|
||||||
function transformOptions(
|
function transformOptions(
|
||||||
|
@ -278,11 +295,11 @@ export function createAdapter(platform: AxiosPlatform) {
|
||||||
...config,
|
...config,
|
||||||
header: config.headers,
|
header: config.headers,
|
||||||
success(response): void {
|
success(response): void {
|
||||||
transformResult(response);
|
transformResponse(response);
|
||||||
config.success(response);
|
config.success(response);
|
||||||
},
|
},
|
||||||
fail(error: AxiosAdapterResponseError): void {
|
fail(error: AxiosAdapterResponseError): void {
|
||||||
transformResult(error);
|
transformResponse(error);
|
||||||
config.fail(error);
|
config.fail(error);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
@ -297,9 +314,16 @@ export function createAdapter(platform: AxiosPlatform) {
|
||||||
response.apFilePath,
|
response.apFilePath,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (response.tempFilePath) delete response.tempFilePath;
|
cleanResponse(response, ['tempFilePath', 'apFilePath', 'filePath']);
|
||||||
if (response.apFilePath) delete response.apFilePath;
|
}
|
||||||
if (response.filePath) delete response.filePath;
|
|
||||||
|
/**
|
||||||
|
* 清理 response 上多余的 key
|
||||||
|
*/
|
||||||
|
function cleanResponse(response: AnyObject, keys: string[]) {
|
||||||
|
for (const key of keys) {
|
||||||
|
if (key in response) delete response[key];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return adapter;
|
return adapter;
|
||||||
|
|
|
@ -186,7 +186,7 @@ export interface AxiosResponse<
|
||||||
|
|
||||||
export interface AxiosResponseError extends AxiosAdapterResponseError {
|
export interface AxiosResponseError extends AxiosAdapterResponseError {
|
||||||
/**
|
/**
|
||||||
* 原生接口 fail 回调产生的响应错误
|
* 失败的请求,指没能够成功响应的请求
|
||||||
*/
|
*/
|
||||||
isFail: true;
|
isFail: true;
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -4,6 +4,7 @@ import {
|
||||||
AxiosAdapterRequestMethod,
|
AxiosAdapterRequestMethod,
|
||||||
AxiosAdapterResponse,
|
AxiosAdapterResponse,
|
||||||
AxiosAdapterResponseError,
|
AxiosAdapterResponseError,
|
||||||
|
AxiosAdapterTask,
|
||||||
} from '../adapter';
|
} from '../adapter';
|
||||||
import {
|
import {
|
||||||
AxiosProgressCallback,
|
AxiosProgressCallback,
|
||||||
|
@ -49,7 +50,16 @@ export function request(config: AxiosRequestConfig) {
|
||||||
fail,
|
fail,
|
||||||
};
|
};
|
||||||
|
|
||||||
const adapterTask = adapter!(adapterConfig);
|
let adapterTask: AxiosAdapterTask;
|
||||||
|
try {
|
||||||
|
adapterTask = adapter!(adapterConfig);
|
||||||
|
} catch {
|
||||||
|
fail({
|
||||||
|
status: 400,
|
||||||
|
statusText: 'Bad Adapter',
|
||||||
|
headers: {},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function success(_: AxiosAdapterResponse): void {
|
function success(_: AxiosAdapterResponse): void {
|
||||||
const response = _ as AxiosResponse;
|
const response = _ as AxiosResponse;
|
||||||
|
|
|
@ -253,12 +253,14 @@ describe('src/adapter.ts', () => {
|
||||||
};
|
};
|
||||||
const p2 = {
|
const p2 = {
|
||||||
...p1,
|
...p1,
|
||||||
request: vi.fn(({ fail }) => fail({ data: { result: null } })),
|
request: vi.fn(({ fail }) =>
|
||||||
|
fail({ status: 500, data: { result: null } }),
|
||||||
|
),
|
||||||
};
|
};
|
||||||
const p3 = {
|
const p3 = {
|
||||||
...p1,
|
...p1,
|
||||||
request: vi.fn(({ fail }) =>
|
request: vi.fn(({ fail }) =>
|
||||||
fail({ statusCode: 500, header: {}, errMsg: 'request:fail' }),
|
fail({ errMsg: 'request:fail', errno: 1000 }),
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
const c1 = {
|
const c1 = {
|
||||||
|
@ -269,9 +271,9 @@ describe('src/adapter.ts', () => {
|
||||||
fail: (response: any) => {
|
fail: (response: any) => {
|
||||||
expect(response).toMatchInlineSnapshot(`
|
expect(response).toMatchInlineSnapshot(`
|
||||||
{
|
{
|
||||||
"headers": undefined,
|
"headers": {},
|
||||||
"status": 400,
|
"status": 400,
|
||||||
"statusText": "Bad Adapter",
|
"statusText": "Fail Adapter",
|
||||||
}
|
}
|
||||||
`);
|
`);
|
||||||
},
|
},
|
||||||
|
@ -284,8 +286,8 @@ describe('src/adapter.ts', () => {
|
||||||
"data": {
|
"data": {
|
||||||
"result": null,
|
"result": null,
|
||||||
},
|
},
|
||||||
"headers": undefined,
|
"headers": {},
|
||||||
"status": 200,
|
"status": 500,
|
||||||
"statusText": "OK",
|
"statusText": "OK",
|
||||||
}
|
}
|
||||||
`);
|
`);
|
||||||
|
@ -296,9 +298,13 @@ describe('src/adapter.ts', () => {
|
||||||
fail: (response: any) => {
|
fail: (response: any) => {
|
||||||
expect(response).toMatchInlineSnapshot(`
|
expect(response).toMatchInlineSnapshot(`
|
||||||
{
|
{
|
||||||
|
"data": {
|
||||||
|
"errMsg": "request:fail",
|
||||||
|
"errno": 1000,
|
||||||
|
},
|
||||||
"headers": {},
|
"headers": {},
|
||||||
"status": 500,
|
"status": 400,
|
||||||
"statusText": "request:fail",
|
"statusText": "Fail Adapter",
|
||||||
}
|
}
|
||||||
`);
|
`);
|
||||||
},
|
},
|
||||||
|
|
|
@ -37,16 +37,42 @@ describe('src/core/dispatchRequest.ts', () => {
|
||||||
).not.toThrowError();
|
).not.toThrowError();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('坏的适配器', () => {
|
test('坏的适配器应该抛出异常', () => {
|
||||||
expect(() =>
|
expect(
|
||||||
dispatchRequest({
|
dispatchRequest({
|
||||||
adapter: () => {
|
adapter: () => {
|
||||||
throw 'bad adapter';
|
throw 'bad adapter';
|
||||||
},
|
},
|
||||||
url: '/',
|
url: '/',
|
||||||
method: 'get',
|
method: 'get',
|
||||||
}),
|
}).catch((e) => ({ ...e })),
|
||||||
).toMatchInlineSnapshot('[Function]');
|
).resolves.toMatchInlineSnapshot(`
|
||||||
|
{
|
||||||
|
"config": {
|
||||||
|
"adapter": [Function],
|
||||||
|
"data": undefined,
|
||||||
|
"headers": undefined,
|
||||||
|
"method": "get",
|
||||||
|
"url": "",
|
||||||
|
},
|
||||||
|
"request": undefined,
|
||||||
|
"response": {
|
||||||
|
"config": {
|
||||||
|
"adapter": [Function],
|
||||||
|
"data": undefined,
|
||||||
|
"headers": undefined,
|
||||||
|
"method": "get",
|
||||||
|
"url": "",
|
||||||
|
},
|
||||||
|
"data": undefined,
|
||||||
|
"headers": {},
|
||||||
|
"isFail": true,
|
||||||
|
"request": undefined,
|
||||||
|
"status": 400,
|
||||||
|
"statusText": "Bad Adapter",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
`);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('应该支持转换 URL', () => {
|
test('应该支持转换 URL', () => {
|
||||||
|
|
|
@ -10,7 +10,7 @@ export default defineConfig({
|
||||||
},
|
},
|
||||||
coverage: {
|
coverage: {
|
||||||
provider: 'istanbul',
|
provider: 'istanbul',
|
||||||
reportsDirectory: resolve('test/coverage'),
|
reportsDirectory: resolve('test/.coverage'),
|
||||||
enabled: false,
|
enabled: false,
|
||||||
include: ['src/**/*.ts'],
|
include: ['src/**/*.ts'],
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue