Skip to content

Commit

Permalink
Merge pull request #19 from pinojs/handle-undefined-fields
Browse files Browse the repository at this point in the history
Do not crash with a property that has value undefined
  • Loading branch information
mcollina committed Aug 8, 2018
2 parents 034eaee + 186642e commit dc30eeb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ module.exports = function prettyFactory (options) {
return line

function joinLinesWithIndentation (value) {
if (!value) {
return
}

const lines = value.split(/\r?\n/)
for (var i = 1; i < lines.length; i++) {
lines[i] = IDENT + lines[i]
Expand Down
21 changes: 20 additions & 1 deletion test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ test('basic prettifier tests', (t) => {
t.afterEach((done) => {
Date.now = Date.originalNow
delete Date.originalNow

done()
})

Expand Down Expand Up @@ -433,5 +432,25 @@ test('basic prettifier tests', (t) => {
t.is(formatted, `[${epoch}] INFO: hello world\n`)
})

t.test('formats a line with an undefined field', (t) => {
t.plan(1)
const pretty = prettyFactory()
const log = pino({}, new Writable({
write (chunk, enc, cb) {
const obj = JSON.parse(chunk.toString())
// weird hack, but we should not crash
obj.a = undefined
const formatted = pretty(obj)
t.is(
formatted,
`[${epoch}] INFO (${pid} on ${hostname}): foo
a: undefined\n`
)
cb()
}
}))
log.info('foo')
})

t.end()
})

0 comments on commit dc30eeb

Please sign in to comment.