From dbc3053113d32ab3e87ccf07ae0d00e01fe236ce Mon Sep 17 00:00:00 2001 From: Gus Narea Date: Tue, 17 Nov 2020 14:41:36 +0000 Subject: [PATCH] fix: Remove Axios config from errors (#22) To avoid filling the logs with unnecessary information --- src/lib/client.spec.ts | 13 ++++++++++++- src/lib/client.ts | 7 +++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/lib/client.spec.ts b/src/lib/client.spec.ts index 1f9414c..1ac4b0e 100644 --- a/src/lib/client.spec.ts +++ b/src/lib/client.spec.ts @@ -16,7 +16,10 @@ describe('deliverParcel', () => { stubAxiosPost.mockResolvedValueOnce(stubResponse); // @ts-ignore - jest.spyOn(axios, 'create').mockReturnValueOnce({ post: stubAxiosPost }); + jest.spyOn(axios, 'create').mockReturnValueOnce({ + interceptors: { response: { use: jest.fn() } } as any, + post: stubAxiosPost, + }); }); afterEach(() => { @@ -82,6 +85,14 @@ describe('deliverParcel', () => { expect(agent).toHaveProperty('keepAlive', true); }); + test('An interceptor that removes the request and response should be registered', async () => { + ((axios.create as any) as jest.MockInstance).mockRestore(); + + await expect(deliverParcel('https://httpstat.us/500', body)).rejects.not.toHaveProperty( + 'jse_cause.config', + ); + }); + describe('POHTTP_TLS_REQUIRED', () => { test('Non-TLS URLs should be refused if POHTTP_TLS_REQUIRED is undefined', async () => { mockEnvVars({}); diff --git a/src/lib/client.ts b/src/lib/client.ts index c55cfda..a31d9b2 100644 --- a/src/lib/client.ts +++ b/src/lib/client.ts @@ -33,6 +33,13 @@ export async function deliverParcel( headers: { 'Content-Type': 'application/vnd.relaynet.parcel' }, httpsAgent: new https.Agent({ keepAlive: true }), }); + + // Remove the request and response from the error cause to avoid filling the logs with + // unnecessary information. See: https://github.com/axios/axios/issues/2602 + axiosInstance.interceptors.response.use(undefined, async error => + Promise.reject(new Error(error.message)), + ); + const response = await postRequest(targetNodeUrl, parcelSerialized, axiosInstance, axiosOptions); if (response.status === 307 || response.status === 308) { throw new PoHTTPError(`Reached maximum number of redirects (${axiosOptions.maxRedirects})`);