diff --git a/src/helpers.js b/src/helpers.js index f27b091..a37de19 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -24,10 +24,23 @@ 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)})` + const allowedHeaders = ['x-request-id'] // update as needed + let msg = `${response.status} - ${response.statusText} (${JSON.stringify(response.body)})` + 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 570948d..b318524 100644 --- a/test/helpers.test.js +++ b/test/helpers.test.js @@ -40,6 +40,45 @@ 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-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."})') + + 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', () => {