From 2963773ba3face99bd459a5b02ce621a0150479d Mon Sep 17 00:00:00 2001 From: aqemi <2773683+aqemi@users.noreply.github.com> Date: Wed, 24 Apr 2024 21:16:31 +0300 Subject: [PATCH] fix: pass error-like objects (#505) --- lib/pretty.js | 2 +- test/basic.test.js | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/pretty.js b/lib/pretty.js index ebb43800..5284d7bc 100644 --- a/lib/pretty.js +++ b/lib/pretty.js @@ -138,7 +138,7 @@ function pretty (inputData) { } // pino@7+ does not log this anymore - if (log.type === 'Error' && log.stack) { + if (log.type === 'Error' && typeof log.stack === 'string') { const prettifiedErrorLog = prettifyErrorLog({ log, context: this.context }) if (this.singleLine) line += this.EOL line += prettifiedErrorLog diff --git a/test/basic.test.js b/test/basic.test.js index 0201e69a..150f1e20 100644 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -918,6 +918,32 @@ test('basic prettifier tests', (t) => { t.equal(arst, 'INFO: hello world\n') }) + t.test('log error-like object', (t) => { + t.plan(7) + const expectedLines = [ + ' type: "Error"', + ' message: "m"', + ' stack: [', + ' "line1",', + ' "line2"', + ' ]' + ] + const pretty = prettyFactory() + const log = pino({}, new Writable({ + write (chunk, enc, cb) { + const formatted = pretty(chunk.toString()) + const lines = formatted.split('\n') + t.equal(lines.length, expectedLines.length + 2) + lines.shift(); lines.pop() + for (let i = 0; i < lines.length; i += 1) { + t.equal(lines[i], expectedLines[i]) + } + cb() + } + })) + log.error({ type: 'Error', message: 'm', stack: ['line1', 'line2'] }) + }) + t.test('include should override ignore', (t) => { t.plan(1) const pretty = prettyFactory({ ignore: 'time,level', include: 'time,level' })