Skip to content

Commit

Permalink
Refactor prettify-error-log to use context
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners committed Aug 30, 2023
1 parent 448253a commit b4190c6
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 36 deletions.
8 changes: 1 addition & 7 deletions lib/pretty.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,7 @@ function pretty (inputData) {

// pino@7+ does not log this anymore
if (log.type === 'Error' && log.stack) {
const prettifiedErrorLog = prettifyErrorLog({
log,
errorLikeKeys: this.errorLikeObjectKeys,
errorProperties: this.errorProps,
ident: this.IDENT,
eol: this.EOL
})
const prettifiedErrorLog = prettifyErrorLog({ log, context: this.context })
if (this.singleLine) line += this.EOL
line += prettifiedErrorLog
} else if (!this.hideObject) {
Expand Down
33 changes: 11 additions & 22 deletions lib/utils/prettify-error-log.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
module.exports = prettifyErrorLog

const {
ERROR_LIKE_KEYS,
LOGGER_KEYS,
MESSAGE_KEY
LOGGER_KEYS
} = require('../constants')

const isObject = require('./is-object')
Expand All @@ -15,17 +13,8 @@ const prettifyObject = require('./prettify-object')
/**
* @typedef {object} PrettifyErrorLogParams
* @property {object} log The error log to prettify.
* @property {string} [messageKey] The name of the key that contains a
* general log message. This is not the error's message property but the logger
* messsage property. Default: `MESSAGE_KEY` constant.
* @property {string} [ident] The sequence to use for indentation. Default: `' '`.
* @property {string} [eol] The sequence to use for EOL. Default: `'\n'`.
* @property {string[]} [errorLikeKeys] A set of keys that should be considered
* to have error objects as values. Default: `ERROR_LIKE_KEYS` constant.
* @property {string[]} [errorProperties] A set of specific error object
* properties, that are not the value of `messageKey`, `type`, or `stack`, to
* include in the prettified result. The first entry in the list may be `'*'`
* to indicate that all sibling properties should be prettified. Default: `[]`.
* @property {PrettyContext} context The context object built from parsing
* the options.
*/

/**
Expand All @@ -36,14 +25,14 @@ const prettifyObject = require('./prettify-object')
*
* @returns {string} A string that represents the prettified error log.
*/
function prettifyErrorLog ({
log,
messageKey = MESSAGE_KEY,
ident = ' ',
eol = '\n',
errorLikeKeys = ERROR_LIKE_KEYS,
errorProperties = []
}) {
function prettifyErrorLog ({ log, context }) {
const {
EOL: eol,
IDENT: ident,
errorLikeKeys,
errorProps: errorProperties,
messageKey
} = context
const stack = log.stack
const joinedLines = joinLinesWithIndentation({ input: stack, ident, eol })
let result = `${ident}${joinedLines}${eol}`
Expand Down
62 changes: 55 additions & 7 deletions lib/utils/prettify-error-log.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,88 @@

const tap = require('tap')
const prettifyErrorLog = require('./prettify-error-log')
const {
ERROR_LIKE_KEYS,
MESSAGE_KEY
} = require('../constants')

const context = {
EOL: '\n',
IDENT: ' ',
errorLikeKeys: ERROR_LIKE_KEYS,
errorProps: [],
messageKey: MESSAGE_KEY
}

tap.test('returns string with default settings', async t => {
const err = Error('Something went wrong')
const str = prettifyErrorLog({ log: err })
const str = prettifyErrorLog({ log: err, context })
t.ok(str.startsWith(' Error: Something went wrong'))
})

tap.test('returns string with custom ident', async t => {
const err = Error('Something went wrong')
const str = prettifyErrorLog({ log: err, ident: ' ' })
const str = prettifyErrorLog({
log: err,
context: {
...context,
IDENT: ' '
}
})
t.ok(str.startsWith(' Error: Something went wrong'))
})

tap.test('returns string with custom eol', async t => {
const err = Error('Something went wrong')
const str = prettifyErrorLog({ log: err, eol: '\r\n' })
const str = prettifyErrorLog({
log: err,
context: {
...context,
EOL: '\r\n'
}
})
t.ok(str.startsWith(' Error: Something went wrong\r\n'))
})

tap.test('errorProperties', t => {
t.test('excludes all for wildcard', async t => {
const err = Error('boom')
err.foo = 'foo'
const str = prettifyErrorLog({ log: err, errorProperties: ['*'] })
const str = prettifyErrorLog({
log: err,
context: {
...context,
errorProps: ['*']
}
})
t.ok(str.startsWith(' Error: boom'))
t.equal(str.includes('foo: "foo"'), false)
})

t.test('excludes only selected properties', async t => {
const err = Error('boom')
err.foo = 'foo'
const str = prettifyErrorLog({ log: err, errorProperties: ['foo'] })
const str = prettifyErrorLog({
log: err,
context: {
...context,
errorProps: ['foo']
}
})
t.ok(str.startsWith(' Error: boom'))
t.equal(str.includes('foo: foo'), true)
})

t.test('ignores specified properties if not present', async t => {
const err = Error('boom')
err.foo = 'foo'
const str = prettifyErrorLog({ log: err, errorProperties: ['foo', 'bar'] })
const str = prettifyErrorLog({
log: err,
context: {
...context,
errorProps: ['foo', 'bar']
}
})
t.ok(str.startsWith(' Error: boom'))
t.equal(str.includes('foo: foo'), true)
t.equal(str.includes('bar'), false)
Expand All @@ -50,7 +92,13 @@ tap.test('errorProperties', t => {
t.test('processes nested objects', async t => {
const err = Error('boom')
err.foo = { bar: 'bar', message: 'included' }
const str = prettifyErrorLog({ log: err, errorProperties: ['foo'] })
const str = prettifyErrorLog({
log: err,
context: {
...context,
errorProps: ['foo']
}
})
t.ok(str.startsWith(' Error: boom'))
t.equal(str.includes('foo: {'), true)
t.equal(str.includes('bar: "bar"'), true)
Expand Down

0 comments on commit b4190c6

Please sign in to comment.