Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
39 changes: 39 additions & 0 deletions test/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
Loading