From ddcac5f58f57c5ca74f996b45d8492d132bf75ae Mon Sep 17 00:00:00 2001 From: arthurfiorette Date: Sun, 30 Jul 2023 00:05:15 -0300 Subject: [PATCH] fix: correct config re throw --- src/interceptors/response.ts | 5 +++-- test/interceptors/stale-if-error.test.ts | 3 ++- test/mocks/axios.ts | 15 ++++++++------- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/interceptors/response.ts b/src/interceptors/response.ts index 7e57d2bd..6c30dced 100644 --- a/src/interceptors/response.ts +++ b/src/interceptors/response.ts @@ -39,9 +39,10 @@ export function defaultResponseInterceptor( msg: 'Response interceptor received an unknown response.', data: response }); - - throw response; } + + // Re-throws the error + throw response; } const id = (response.id = response.config.id ??= axios.generateKey(response.config)); diff --git a/test/interceptors/stale-if-error.test.ts b/test/interceptors/stale-if-error.test.ts index c8796194..8eee9983 100644 --- a/test/interceptors/stale-if-error.test.ts +++ b/test/interceptors/stale-if-error.test.ts @@ -268,6 +268,7 @@ describe('Last-Modified handling', () => { id, adapter: (config) => Promise.reject({ + isAxiosError: true, data: false, headers: {}, config, @@ -397,6 +398,6 @@ describe('Last-Modified handling', () => { cache: { staleIfError: true }, validateStatus: () => false }) - ).rejects.toMatchObject({ id }); + ).rejects.toMatchObject({ config: { id } }); }); }); diff --git a/test/mocks/axios.ts b/test/mocks/axios.ts index c1207a28..4a6e9e34 100644 --- a/test/mocks/axios.ts +++ b/test/mocks/axios.ts @@ -1,5 +1,5 @@ -import Axios from 'axios'; -import type { AxiosCacheInstance, CacheRequestConfig } from '../../src/cache/axios'; +import Axios, { AxiosError } from 'axios'; +import type { AxiosCacheInstance } from '../../src/cache/axios'; import { CacheOptions, setupCache } from '../../src/cache/create'; import { Header } from '../../src/header/headers'; @@ -22,11 +22,12 @@ export function mockAxios( const statusText = should304 ? '304 Not Modified' : '200 OK'; if (config.validateStatus?.(status) === false) { - throw { - id: (config as CacheRequestConfig).id, + throw new AxiosError( + 'request failed', + status.toString(), config, - request: { config }, - response: { + { config }, + { data: true, status, statusText, @@ -38,7 +39,7 @@ export function mockAxios( config, request: { config } } - }; + ); } return {