Skip to content

Commit

Permalink
feat: Add AbortError handling
Browse files Browse the repository at this point in the history
  • Loading branch information
卢俊升 committed Oct 18, 2022
1 parent 99e89c9 commit de50d00
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,18 @@ const getRequestMethod = () => {
error.message = errorInfo?.errorMessage || error.message;
error.data = error.data;
error.info = errorInfo;
} else if (error.name === 'AbortError'&& error.request) {
const ctx: Context = {
req: error.request,
res: error.response,
};
errorInfo = errorAdaptor({}, ctx);
error.info = errorInfo;
}

errorInfo = error.info;


if (errorInfo) {
const errorMessage = errorInfo?.errorMessage;
const errorCode = errorInfo?.errorCode;
Expand Down
41 changes: 41 additions & 0 deletions tests/normal.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ jest.mock(
res.testMiddlewares = 'middlewares works';
},
],
errorConfig: {
adaptor: (data, ctx) => {
if (ctx?.req.options.signal?.aborted) {
return {
...data,
errorMessage: 'abort error message'
}
}
return data
},
},
};
},
{ virtual: true },
Expand Down Expand Up @@ -65,6 +76,35 @@ describe('normal request', () => {
expect(response.data).toEqual(rawData);
});

test('abort failed', async () => {
const rawData = {
success: false,
errorMessage: 'test message',
showType: 1,
};
const abc = new AbortController();
server.get('/test/failed', (req, res) => {
res.send(rawData);
});
try {
const req = request(prefix('/test/failed'), {
signal: abc.signal
});

abc.abort('Abort request')

const response = await req

} catch (e) {
expect(e.name).toEqual('BizError');
expect(e.message).toEqual('abort error message');
expect(e.data).toEqual({
...rawData,
testMiddlewares: 'middlewares works',
});
}
});

test('failed', async () => {
const rawData = {
success: false,
Expand All @@ -86,6 +126,7 @@ describe('normal request', () => {
}
});


test('http failed', async () => {
const rawData = {
success: false,
Expand Down

0 comments on commit de50d00

Please sign in to comment.