Skip to content

Commit

Permalink
fix: Remove Axios config from errors (#22)
Browse files Browse the repository at this point in the history
To avoid filling the logs with unnecessary information
  • Loading branch information
gnarea authored Nov 17, 2020
1 parent a25fbaf commit dbc3053
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/lib/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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<any, any>).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({});
Expand Down
7 changes: 7 additions & 0 deletions src/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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})`);
Expand Down

0 comments on commit dbc3053

Please sign in to comment.