diff --git a/lib/err.js b/lib/err.js index ccd81c9..d92fce4 100644 --- a/lib/err.js +++ b/lib/err.js @@ -28,6 +28,11 @@ const pinoErrProto = Object.create({}, { writable: true, value: undefined }, + cause: { + enumerable: true, + writable: true, + value: undefined + }, raw: { enumerable: false, get: function () { @@ -60,6 +65,10 @@ function errSerializer (err) { _err.aggregateErrors = err.errors.map(err => errSerializer(err)) } + if (err.cause) { + _err.cause = errSerializer(err.cause) + } + for (const key in err) { if (_err[key] === undefined) { const val = err[key] diff --git a/test/err.test.js b/test/err.test.js index 746ed7a..d073c06 100644 --- a/test/err.test.js +++ b/test/err.test.js @@ -194,3 +194,27 @@ test('serializes aggregate errors', { skip: !global.AggregateError }, function ( t.match(serialized.aggregateErrors[1].stack, /^Error: bar/) t.match(serialized.stack, /err\.test\.js:/) }) + +test('serializes causes', function (t) { + t.plan(11) + + const bar = new Error('bar') + bar.cause = new Error('foo') + bar.cause.cause = new Error('baz') + + const serialized = serializer(bar) + + t.equal(serialized.type, 'Error') + t.equal(serialized.message, 'bar: foo: baz') // message serialization already walks cause-chain + t.match(serialized.stack, /err\.test\.js:/) + + t.ok(serialized.cause) + t.equal(serialized.cause.type, 'Error') + t.equal(serialized.cause.message, 'foo: baz') + t.match(serialized.cause.stack, /err\.test\.js:/) + + t.ok(serialized.cause.cause) + t.equal(serialized.cause.cause.type, 'Error') + t.equal(serialized.cause.cause.message, 'baz') + t.match(serialized.cause.cause.stack, /err\.test\.js:/) +})