Skip to content

Commit

Permalink
Merge pull request #78 from lifeomic/fix-exceptions
Browse files Browse the repository at this point in the history
Throw original error on network error
  • Loading branch information
Jeffrey Jagoda authored Jun 29, 2021
2 parents f07cda7 + 7236f65 commit 61bc070
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ async function axiosFetch (
try {
result = await axios.request(config);
} catch (err) {
result = err.response;
if (err.response) {
result = err.response;
} else {
throw err;
}
}

const fetchHeaders = new FetchHeaders(result.headers);
Expand Down
10 changes: 9 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ test.before(() => {
.get('/failure')
.reply(501)
.get('/failureBody')
.reply(400, { test: true });
.reply(400, { test: true })
.get('/error')
.replyWithError('simulated failure');
});

async function dualFetch (input: string, init?: FetchInit) {
Expand Down Expand Up @@ -199,6 +201,12 @@ test('returns the expected response body on a failure', async (test) => {
test.deepEqual(axiosResponse.headers, expectedResponse.headers);
});

test('throws the original error on a network error', async (test) => {
const axiosFetch = buildAxiosFetch(axios);
const error = await test.throwsAsync(axiosFetch(`${TEST_URL_ROOT}/error`));
test.is(error.message, 'simulated failure');
});

test('allows transforming request options', async (test) => {
const originalUrl = `${TEST_URL_ROOT}/success/text`;
const transformedUrl = `${TEST_URL_ROOT}/success/json`;
Expand Down

0 comments on commit 61bc070

Please sign in to comment.