From b4190c652b7e71cbf2ae9463ac3c5ca8b11a63d2 Mon Sep 17 00:00:00 2001 From: James Sumners Date: Wed, 30 Aug 2023 16:40:55 -0400 Subject: [PATCH] Refactor prettify-error-log to use context --- lib/pretty.js | 8 +--- lib/utils/prettify-error-log.js | 33 +++++---------- lib/utils/prettify-error-log.test.js | 62 ++++++++++++++++++++++++---- 3 files changed, 67 insertions(+), 36 deletions(-) diff --git a/lib/pretty.js b/lib/pretty.js index 70b098fc..cb329a87 100644 --- a/lib/pretty.js +++ b/lib/pretty.js @@ -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) { diff --git a/lib/utils/prettify-error-log.js b/lib/utils/prettify-error-log.js index 21a4089b..bed68908 100644 --- a/lib/utils/prettify-error-log.js +++ b/lib/utils/prettify-error-log.js @@ -3,9 +3,7 @@ module.exports = prettifyErrorLog const { - ERROR_LIKE_KEYS, - LOGGER_KEYS, - MESSAGE_KEY + LOGGER_KEYS } = require('../constants') const isObject = require('./is-object') @@ -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. */ /** @@ -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}` diff --git a/lib/utils/prettify-error-log.test.js b/lib/utils/prettify-error-log.test.js index 5d0f7db1..2d022389 100644 --- a/lib/utils/prettify-error-log.test.js +++ b/lib/utils/prettify-error-log.test.js @@ -2,22 +2,46 @@ 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')) }) @@ -25,7 +49,13 @@ 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) }) @@ -33,7 +63,13 @@ tap.test('errorProperties', t => { 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) }) @@ -41,7 +77,13 @@ tap.test('errorProperties', t => { 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) @@ -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)