Skip to content

Commit

Permalink
feat: handle response interceptor errors
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Mar 19, 2023
1 parent a9e8998 commit 360ba57
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/interceptors/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ export function defaultResponseInterceptor(
};

const onFulfilled: ResponseInterceptor['onFulfilled'] = async (response) => {
// When response.config is not present, the response is indeed a error.
if (!response.config) {
if (__ACI_DEV__) {
axios.debug?.({
msg: 'Response interceptor received an unknown response.',
data: response
});

throw response;
}
}

const id = (response.id = response.config.id ??= axios.generateKey(response.config));
response.cached ??= false;

Expand Down
16 changes: 16 additions & 0 deletions test/interceptors/response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,20 @@ describe('test response interceptor', () => {
expect(cache.state).not.toBe('loading');
expect(cache.state).toBe('empty');
});

it('expects response interceptor handles non response errors', async () => {
const instance = Axios.create();

const NOT_RESPONSE = { notAResponse: true };

//@ts-expect-error - this is indeed wrongly behavior
instance.interceptors.response.use(() => NOT_RESPONSE);

const axios = mockAxios(undefined, undefined, instance);

await expect(
// just calls the response interceptor
axios.get('url')
).rejects.toBe(NOT_RESPONSE);
});
});

0 comments on commit 360ba57

Please sign in to comment.