Skip to content

Commit

Permalink
stringify: add support for nested errors with multiple properties
Browse files Browse the repository at this point in the history
  • Loading branch information
zignd committed Aug 21, 2020
1 parent 5520941 commit ccc3ffc
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 11 deletions.
23 changes: 19 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"iron-mask": "1.0.2",
"moment-timezone": "^0.5.13",
"pokeprop": "^1.0.0",
"ramda": "^0.23.0"
"ramda": "^0.23.0",
"serialize-error": "^7.0.1"
},
"devDependencies": {
"ava": "^2.4.0",
Expand Down
7 changes: 3 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
const R = require('ramda')
const pokeprop = require('pokeprop')
const circularJSON = require('circular-json')
const { serializeError } = require('serialize-error')

const parseStringToJSON = chunk => (
Promise.resolve(chunk)
.then(JSON.parse)
.catch(err => chunk)
.catch(_ => chunk)
)

const buildErrorObject = R.pick(['message', 'stack'])

const stringify = log => {
if (R.is(String, log)) return log
if (R.is(Error, log.message)) log.message = buildErrorObject(log.message)
if (R.is(Error, log.message)) log.message = serializeError(log.message)
return circularJSON.stringify(log)
}

Expand Down
33 changes: 31 additions & 2 deletions test/unit/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,47 @@ test('stringify: with a invalid JSON object', t => {
})

test('stringify: with a valid error object', t => {
const err = new Error('Error Message')

const logStringified = utils.stringify({
id: 1,
message: new Error('Error Message')
message: err
})

const logObject = JSON.parse(logStringified)
const logError = logObject.message

t.is(logError.message, 'Error Message')
t.is(logError.message, err.message)
t.true(logError.stack.includes('Error: Error Message'))
})

test('stringify: with a valid nested error object with multiple properties', t => {
const innerErr = new Error('Inner error message')
innerErr.username = 'foo'
innerErr.isValidUsername = true

const err = new Error('Error message')
err.cause = innerErr
err.correlationId = '4ac1-b431-cab1-4c1a'

const logStringified = utils.stringify({
id: 1,
message: err
})

const logObject = JSON.parse(logStringified)
const logError = logObject.message

t.is(logError.message, err.message)
t.is(logError.correlationId, err.correlationId)
t.true(logError.stack.includes('Error: Error message'))

t.is(logError.cause.message, innerErr.message)
t.is(logError.cause.username, innerErr.username)
t.is(logError.cause.isValidUsername, innerErr.isValidUsername)
t.true(logError.cause.stack.includes('Error: Inner error message'))
})

test('generateLogLevel: with status code 400', t => {
t.is(utils.generateLogLevel(400), 'warn')
})
Expand Down

0 comments on commit ccc3ffc

Please sign in to comment.