Skip to content

Commit

Permalink
feat: handle non axios errors rejections (#609)
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Jul 28, 2023
1 parent c7e3af0 commit 929054e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/interceptors/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,21 @@ export function defaultResponseInterceptor(
};

const onRejected: ResponseInterceptor['onRejected'] = async (error) => {
// When response.config is not present, the response is indeed a error.
if (!error.isAxiosError) {
if (__ACI_DEV__) {
axios.debug?.({
msg: 'Received an non axios error in the rejected response interceptor, ignoring.',
data: error
});
}

// We should probably re-request the response to avoid an infinite loading state here
// but, since this is an unknown error, we cannot figure out what request ID to use.
// And the only solution is to let the storage actively reject the current loading state.
throw error;
}

const config = error.config as CacheRequestConfig & { headers: AxiosResponseHeaders };
const id = config.id;
const cacheConfig = config.cache as CacheProperties;
Expand Down
17 changes: 17 additions & 0 deletions test/interceptors/response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,4 +280,21 @@ describe('test response interceptor', () => {
expect(normal.data).toBe(true);
expect(transformed.data).toStrictEqual([true]);
});

it('works even when modifying the error response', async () => {
const axios = mockAxios();

const error = new Error();

const promise = axios.get('url', {
transformResponse: () => {
throw error;
}
});

await expect(promise).rejects.not.toThrow(
"Cannot read properties of undefined (reading 'id')"
);
await expect(promise).rejects.toBe(error);
});
});

0 comments on commit 929054e

Please sign in to comment.