From 576c970e2f71554a1f748b9208ff7a3f0596e822 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Sun, 29 Jul 2018 09:48:16 -0700 Subject: [PATCH] Do not cast to string Error#stack. (#464) --- pino.js | 4 +++- test/error.test.js | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/pino.js b/pino.js index ff34c3b30..43b68b6a5 100644 --- a/pino.js +++ b/pino.js @@ -147,7 +147,9 @@ function asJson (obj, msg, num, time) { var notHasOwnProperty = obj.hasOwnProperty === undefined if (objError) { data += ',"type":"Error"' - data += obj.stack ? ',"stack":' + this.stringify(obj.stack) : '' + if (obj.stack !== undefined) { + data += ',"stack":' + this.stringify(obj.stack) + } } // if global serializer is set, call it first if (this.serializers[Symbol.for('pino.*')]) { diff --git a/test/error.test.js b/test/error.test.js index dbdf1bc84..5bdb73d5a 100644 --- a/test/error.test.js +++ b/test/error.test.js @@ -143,3 +143,19 @@ test('stack is omitted if it is not set on err', t => { instance.level = name instance[name](err) }) + +test('stack is rendered as any other property if it\'s not a string', t => { + t.plan(3) + var err = new Error('myerror') + err.stack = null + var instance = pino(sink(function (chunk, enc, cb) { + t.ok(new Date(chunk.time) <= new Date(), 'time is greater than Date.now()') + delete chunk.time + t.equal(chunk.hasOwnProperty('stack'), true) + t.equal(chunk.stack, null) + cb() + })) + + instance.level = name + instance[name](err) +})