mirror of
https://github.com/arthurfiorette/axios-cache-interceptor.git
synced 2025-12-08 17:36:16 +00:00
feat: handle response interceptor errors
This commit is contained in:
parent
a9e8998026
commit
360ba57348
@ -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;
|
||||
|
||||
|
||||
@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user