From e94e60a7675bdd207c197cfe84268d3eefd3cbdc Mon Sep 17 00:00:00 2001 From: Gus Narea Date: Tue, 17 Nov 2020 21:35:25 +0000 Subject: [PATCH] fix: Remove Axios config from connection errors To avoid filling the logs with unnecessary information --- src/lib/client.spec.ts | 5 +++-- src/lib/client.ts | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/lib/client.spec.ts b/src/lib/client.spec.ts index 17a439e..ea1f5d2 100644 --- a/src/lib/client.spec.ts +++ b/src/lib/client.spec.ts @@ -250,13 +250,14 @@ describe('deliverParcel', () => { ); }); - test('Unexpected axios exceptions should be wrapped rethrown', async () => { + test('Connection error should be replaced with a simpler error', async () => { + // .. That doesn't leak the request or response. // @ts-ignore stubAxiosPost.mockReset(); const axiosError = new Error('Haha, in thy face'); stubAxiosPost.mockRejectedValueOnce(axiosError); - const expectedError = new PoHTTPError(axiosError, 'Failed to deliver parcel'); + const expectedError = new PoHTTPError(`Connection error: ${axiosError.message}`); await expectPromiseToReject(deliverParcel(url, body), expectedError); }); diff --git a/src/lib/client.ts b/src/lib/client.ts index 262dc01..bd11e4b 100644 --- a/src/lib/client.ts +++ b/src/lib/client.ts @@ -66,11 +66,11 @@ async function postRequest( }); } catch (error) { if (!error.response) { - throw new PoHTTPError(error, 'Failed to deliver parcel'); + throw new PoHTTPError(`Connection error: ${error.message}`); } const responseStatus = error.response.status; - const reason = error.response?.data?.message; + const reason = error.response.data?.message; if (responseStatus === 403) { throw new PoHTTPInvalidParcelError( reason ? `Server refused to accept parcel: ${reason}` : 'Server refused to accept parcel',