From 186642ebdb7939b36809825fb2ef0fa71e5f6efa Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Wed, 8 Aug 2018 14:44:28 +0200 Subject: [PATCH] Do not crash with a property that has value undefined --- index.js | 4 ++++ test/basic.test.js | 21 ++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 53a07e84..0d6d1afd 100644 --- a/index.js +++ b/index.js @@ -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] diff --git a/test/basic.test.js b/test/basic.test.js index 6112d279..c297a608 100644 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -22,7 +22,6 @@ test('basic prettifier tests', (t) => { t.afterEach((done) => { Date.now = Date.originalNow delete Date.originalNow - done() }) @@ -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() })