pino-std-serializers
Advanced tools
Comparing version 5.5.0 to 5.6.0
@@ -15,18 +15,19 @@ 'use strict' | ||
const cause = evaluateCause(err.cause) | ||
/** @type {unknown} */ | ||
// @ts-ignore | ||
const cause = err.cause | ||
return cause instanceof Error | ||
? cause | ||
: undefined | ||
} | ||
// VError / NError style causes | ||
if (typeof cause === 'function') { | ||
// @ts-ignore | ||
const causeResult = err.cause() | ||
/** | ||
* @param {unknown|(()=>err)} cause | ||
* @returns {Error|undefined} | ||
*/ | ||
const evaluateCause = (cause) => { | ||
// VError / NError style causes are functions | ||
return typeof cause === 'function' | ||
? cause() | ||
: cause | ||
return causeResult instanceof Error | ||
? causeResult | ||
: undefined | ||
} else { | ||
return cause instanceof Error | ||
? cause | ||
: undefined | ||
} | ||
} | ||
@@ -111,5 +112,4 @@ | ||
getErrorCause, | ||
evaluateCause, | ||
stackWithCauses, | ||
messageWithCauses | ||
} |
@@ -5,3 +5,3 @@ 'use strict' | ||
const { messageWithCauses, stackWithCauses, evaluateCause } = require('./err-helpers') | ||
const { messageWithCauses, stackWithCauses } = require('./err-helpers') | ||
@@ -32,7 +32,2 @@ const { toString } = Object.prototype | ||
}, | ||
cause: { | ||
enumerable: true, | ||
writable: true, | ||
value: undefined | ||
}, | ||
raw: { | ||
@@ -70,13 +65,8 @@ enumerable: false, | ||
if (err.cause) { | ||
const cause = evaluateCause(err.cause) | ||
_err.cause = errSerializer(cause) | ||
} | ||
for (const key in err) { | ||
if (_err[key] === undefined) { | ||
const val = err[key] | ||
if (val instanceof Error && key !== 'cause') { | ||
/* eslint-disable no-prototype-builtins */ | ||
if (!val.hasOwnProperty(seen)) { | ||
if (val instanceof Error) { | ||
// We append cause messages and stacks to _err, therefore skipping causes here | ||
if (key !== 'cause' && !Object.prototype.hasOwnProperty.call(val, seen)) { | ||
_err[key] = errSerializer(val) | ||
@@ -83,0 +73,0 @@ } |
{ | ||
"name": "pino-std-serializers", | ||
"version": "5.5.0", | ||
"version": "5.6.0", | ||
"description": "A collection of standard object serializers for Pino", | ||
@@ -40,3 +40,3 @@ "main": "index.js", | ||
"tap": "^15.0.10", | ||
"tsd": "^0.20.0", | ||
"tsd": "^0.21.0", | ||
"typescript": "^4.2.4" | ||
@@ -43,0 +43,0 @@ }, |
@@ -50,3 +50,3 @@ 'use strict' | ||
test('serializes error causes', function (t) { | ||
t.plan(6) | ||
t.plan(7) | ||
const err = Error('foo') | ||
@@ -62,2 +62,3 @@ err.cause = Error('bar') | ||
t.match(serialized.stack, /Error: abc/) | ||
t.notOk(serialized.cause) | ||
}) | ||
@@ -69,5 +70,6 @@ | ||
const err = Error('foo: bar') | ||
err.cause = () => { | ||
err.foo = 'abc' | ||
err.cause = function () { | ||
const err = Error('bar') | ||
err.cause = Error('abc') | ||
err.cause = Error(this.foo) | ||
return err | ||
@@ -84,2 +86,12 @@ } | ||
test('keeps non-error cause', function (t) { | ||
t.plan(3) | ||
const err = Error('foo') | ||
err.cause = 'abc' | ||
const serialized = serializer(err) | ||
t.equal(serialized.type, 'Error') | ||
t.equal(serialized.message, 'foo') | ||
t.equal(serialized.cause, 'abc') | ||
}) | ||
test('prevents infinite recursion', function (t) { | ||
@@ -201,53 +213,1 @@ t.plan(4) | ||
}) | ||
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:/) | ||
}) | ||
test('serializes causes with VError support', function (t) { | ||
t.plan(11) | ||
// Fake VError-style setup | ||
const err = Error('foo: bar') | ||
err.cause = () => { | ||
const err = Error('bar') | ||
err.cause = Error('abc') | ||
return err | ||
} | ||
const serialized = serializer(err) | ||
t.equal(serialized.type, 'Error') | ||
t.equal(serialized.message, 'foo: bar: abc') // 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, 'bar: abc') | ||
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, 'abc') | ||
t.match(serialized.cause.cause.stack, /err\.test\.js:/) | ||
}) |
40685
1119