feat: handle response interceptor errors

This commit is contained in:
arthurfiorette 2023-03-19 17:30:02 -03:00
parent a9e8998026
commit 360ba57348
2 changed files with 28 additions and 0 deletions

View File

@ -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;

View File

@ -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);
});
});