Skip to content

Commit 1a23045

Browse files
authored
fix: pass error-like objects (#505) (#506)
1 parent 4f992c2 commit 1a23045

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lib/pretty.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ function pretty (inputData) {
138138
}
139139

140140
// pino@7+ does not log this anymore
141-
if (log.type === 'Error' && log.stack) {
141+
if (log.type === 'Error' && typeof log.stack === 'string') {
142142
const prettifiedErrorLog = prettifyErrorLog({ log, context: this.context })
143143
if (this.singleLine) line += this.EOL
144144
line += prettifiedErrorLog

test/basic.test.js

+26
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,32 @@ test('basic prettifier tests', (t) => {
922922
t.equal(arst, 'INFO: hello world\n')
923923
})
924924

925+
t.test('log error-like object', (t) => {
926+
t.plan(7)
927+
const expectedLines = [
928+
' type: "Error"',
929+
' message: "m"',
930+
' stack: [',
931+
' "line1",',
932+
' "line2"',
933+
' ]'
934+
]
935+
const pretty = prettyFactory()
936+
const log = pino({}, new Writable({
937+
write (chunk, enc, cb) {
938+
const formatted = pretty(chunk.toString())
939+
const lines = formatted.split('\n')
940+
t.equal(lines.length, expectedLines.length + 2)
941+
lines.shift(); lines.pop()
942+
for (let i = 0; i < lines.length; i += 1) {
943+
t.equal(lines[i], expectedLines[i])
944+
}
945+
cb()
946+
}
947+
}))
948+
log.error({ type: 'Error', message: 'm', stack: ['line1', 'line2'] })
949+
})
950+
925951
t.test('include should override ignore', (t) => {
926952
t.plan(1)
927953
const pretty = prettyFactory({ ignore: 'time,level', include: 'time,level' })

0 commit comments

Comments
 (0)