Skip to content

Commit

Permalink
fix: pass error-like objects (#505)
Browse files Browse the repository at this point in the history
  • Loading branch information
aqemi committed Apr 24, 2024
1 parent 6f0ecf0 commit 09765c3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/pretty.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' })
Expand Down

0 comments on commit 09765c3

Please sign in to comment.