From df678793680e881ac47fd1e7528be2708f38f03c Mon Sep 17 00:00:00 2001 From: taub Date: Wed, 21 Jan 2026 16:38:53 -0500 Subject: [PATCH 1/3] Append all headers to error if present --- src/helpers.js | 6 +++++- test/helpers.test.js | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/helpers.js b/src/helpers.js index f27b091..9f69a8e 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -24,7 +24,11 @@ function reduceError (error = {}) { const response = error.response if (response) { if (response.status && response.statusText && response.body) { - return `${response.status} - ${response.statusText} (${JSON.stringify(response.body)})` + let msg = `${response.status} - ${response.statusText} (${JSON.stringify(response.body)})` + if (response.headers) { + msg += ` - Headers: ${JSON.stringify(response.headers)}` + } + return msg } } diff --git a/test/helpers.test.js b/test/helpers.test.js index 570948d..a23d021 100644 --- a/test/helpers.test.js +++ b/test/helpers.test.js @@ -40,6 +40,19 @@ test('reduceError', () => { } } expect(helpers.reduceError(expectedError)).toEqual("500 - Something went gang aft agley. ({\"error_code\":500101,\"message\":\"I'm giving it all I got, cap'n\"})") + + const expectedErrorWithHeaders = { + response: { + status: 418, + statusText: 'Tried making coffee in a teapot!', + body: { + error_code: 418001, + message: 'Reinstall beans and try again.' + }, + headers: { 'x-coffee-error': 'true', 'x-request-id': 'uuid-abc-def-ghi-123' } + } + } + expect(helpers.reduceError(expectedErrorWithHeaders)).toEqual('418 - Tried making coffee in a teapot! ({"error_code":418001,"message":"Reinstall beans and try again."}) - Headers: {"x-coffee-error":"true","x-request-id":"uuid-abc-def-ghi-123"}') }) describe('createRequestOptions', () => { From 0aa4c1c99f5bf219d16d38a9622199ceab4e3de4 Mon Sep 17 00:00:00 2001 From: taub Date: Thu, 22 Jan 2026 10:56:39 -0500 Subject: [PATCH 2/3] Filter for specific headers when returning errors --- src/helpers.js | 15 ++++++++++++--- test/helpers.test.js | 15 ++++++++++++++- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/helpers.js b/src/helpers.js index 9f69a8e..a37de19 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -24,14 +24,23 @@ function reduceError (error = {}) { const response = error.response if (response) { if (response.status && response.statusText && response.body) { + const allowedHeaders = ['x-request-id'] // update as needed let msg = `${response.status} - ${response.statusText} (${JSON.stringify(response.body)})` - if (response.headers) { - msg += ` - Headers: ${JSON.stringify(response.headers)}` + let headersMsg = ' - Headers: [ ' + if (response.headers && Object.keys(response.headers).length > 0) { + const headerParts = allowedHeaders + .filter(header => response.headers[header]) + .map(header => `${header}=${response.headers[header]}`) + headersMsg += headerParts.join('; ') } + headersMsg += ' ]' + if (headersMsg !== ' - Headers: [ ]') { + msg += headersMsg + } + return msg } } - return error } diff --git a/test/helpers.test.js b/test/helpers.test.js index a23d021..2b0a8ca 100644 --- a/test/helpers.test.js +++ b/test/helpers.test.js @@ -52,7 +52,20 @@ test('reduceError', () => { headers: { 'x-coffee-error': 'true', 'x-request-id': 'uuid-abc-def-ghi-123' } } } - expect(helpers.reduceError(expectedErrorWithHeaders)).toEqual('418 - Tried making coffee in a teapot! ({"error_code":418001,"message":"Reinstall beans and try again."}) - Headers: {"x-coffee-error":"true","x-request-id":"uuid-abc-def-ghi-123"}') + expect(helpers.reduceError(expectedErrorWithHeaders)).toEqual('418 - Tried making coffee in a teapot! ({"error_code":418001,"message":"Reinstall beans and try again."}) - Headers: [ x-request-id=uuid-abc-def-ghi-123 ]') + + const expectedErrorWithNoRelevantHeaders = { + response: { + status: 418, + statusText: 'Tried making coffee in a teapot!', + body: { + error_code: 418001, + message: 'Reinstall beans and try again.' + }, + headers: { 'x-coffee-error': 'true' } + } + } + expect(helpers.reduceError(expectedErrorWithNoRelevantHeaders)).toEqual('418 - Tried making coffee in a teapot! ({"error_code":418001,"message":"Reinstall beans and try again."})') }) describe('createRequestOptions', () => { From 6cb4ca9aea183fe704795afeefa3b9c469341a71 Mon Sep 17 00:00:00 2001 From: taub Date: Thu, 22 Jan 2026 13:47:27 -0500 Subject: [PATCH 3/3] add empty headers case test for errors --- test/helpers.test.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/helpers.test.js b/test/helpers.test.js index 2b0a8ca..b318524 100644 --- a/test/helpers.test.js +++ b/test/helpers.test.js @@ -66,6 +66,19 @@ test('reduceError', () => { } } expect(helpers.reduceError(expectedErrorWithNoRelevantHeaders)).toEqual('418 - Tried making coffee in a teapot! ({"error_code":418001,"message":"Reinstall beans and try again."})') + + const expectedErrorWithEmptyHeaders = { + response: { + status: 418, + statusText: 'Tried making coffee in a teapot!', + body: { + error_code: 418001, + message: 'Reinstall beans and try again.' + }, + headers: { } + } + } + expect(helpers.reduceError(expectedErrorWithEmptyHeaders)).toEqual('418 - Tried making coffee in a teapot! ({"error_code":418001,"message":"Reinstall beans and try again."})') }) describe('createRequestOptions', () => {