diff --git a/src/interceptors/response.ts b/src/interceptors/response.ts index f160c9aa..7e57d2bd 100644 --- a/src/interceptors/response.ts +++ b/src/interceptors/response.ts @@ -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; diff --git a/test/interceptors/response.test.ts b/test/interceptors/response.test.ts index 681f1f55..c5a0a05d 100644 --- a/test/interceptors/response.test.ts +++ b/test/interceptors/response.test.ts @@ -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); + }); });