pino-std-serializers
Advanced tools
Comparing version 6.0.0 to 6.1.0
@@ -8,5 +8,9 @@ 'use strict' | ||
const isErrorLike = (err) => { | ||
return err && typeof err.message === 'string' | ||
} | ||
/** | ||
* @param {Error|{ cause?: unknown|(()=>err)}} err | ||
* @returns {Error|undefined} | ||
* @returns {Error|Object|undefined} | ||
*/ | ||
@@ -25,7 +29,7 @@ const getErrorCause = (err) => { | ||
return causeResult instanceof Error | ||
return isErrorLike(causeResult) | ||
? causeResult | ||
: undefined | ||
} else { | ||
return cause instanceof Error | ||
return isErrorLike(cause) | ||
? cause | ||
@@ -45,3 +49,3 @@ : undefined | ||
const _stackWithCauses = (err, seen) => { | ||
if (!(err instanceof Error)) return '' | ||
if (!isErrorLike(err)) return '' | ||
@@ -81,3 +85,3 @@ const stack = err.stack || '' | ||
const _messageWithCauses = (err, seen, skip) => { | ||
if (!(err instanceof Error)) return '' | ||
if (!isErrorLike(err)) return '' | ||
@@ -114,2 +118,3 @@ const message = skip ? '' : (err.message || '') | ||
module.exports = { | ||
isErrorLike, | ||
getErrorCause, | ||
@@ -116,0 +121,0 @@ stackWithCauses, |
@@ -5,3 +5,3 @@ 'use strict' | ||
const { messageWithCauses, stackWithCauses } = require('./err-helpers') | ||
const { messageWithCauses, stackWithCauses, isErrorLike } = require('./err-helpers') | ||
@@ -48,3 +48,3 @@ const { toString } = Object.prototype | ||
function errSerializer (err) { | ||
if (!(err instanceof Error)) { | ||
if (!isErrorLike(err)) { | ||
return err | ||
@@ -61,3 +61,3 @@ } | ||
if (global.AggregateError !== undefined && err instanceof global.AggregateError && Array.isArray(err.errors)) { | ||
if (Array.isArray(err.errors)) { | ||
_err.aggregateErrors = err.errors.map(err => errSerializer(err)) | ||
@@ -69,3 +69,3 @@ } | ||
const val = err[key] | ||
if (val instanceof Error) { | ||
if (isErrorLike(val)) { | ||
// We append cause messages and stacks to _err, therefore skipping causes here | ||
@@ -72,0 +72,0 @@ if (key !== 'cause' && !Object.prototype.hasOwnProperty.call(val, seen)) { |
{ | ||
"name": "pino-std-serializers", | ||
"version": "6.0.0", | ||
"version": "6.1.0", | ||
"description": "A collection of standard object serializers for Pino", | ||
@@ -35,3 +35,3 @@ "main": "index.js", | ||
"devDependencies": { | ||
"@types/node": "^17.0.0", | ||
"@types/node": "^18.0.0", | ||
"pre-commit": "^1.2.2", | ||
@@ -41,3 +41,3 @@ "snazzy": "^9.0.0", | ||
"tap": "^15.0.10", | ||
"tsd": "^0.21.0", | ||
"tsd": "^0.25.0", | ||
"typescript": "^4.2.4" | ||
@@ -44,0 +44,0 @@ }, |
@@ -50,14 +50,19 @@ 'use strict' | ||
test('serializes error causes', function (t) { | ||
t.plan(7) | ||
const err = Error('foo') | ||
err.cause = Error('bar') | ||
err.cause.cause = Error('abc') | ||
const serialized = serializer(err) | ||
t.equal(serialized.type, 'Error') | ||
t.equal(serialized.message, 'foo: bar: abc') | ||
t.match(serialized.stack, /err\.test\.js:/) | ||
t.match(serialized.stack, /Error: foo/) | ||
t.match(serialized.stack, /Error: bar/) | ||
t.match(serialized.stack, /Error: abc/) | ||
t.notOk(serialized.cause) | ||
t.plan(14) | ||
for (const cause of [ | ||
Error('bar'), | ||
{ message: 'bar', stack: 'Error: bar: err.test.js:' } | ||
]) { | ||
const err = Error('foo') | ||
err.cause = cause | ||
err.cause.cause = Error('abc') | ||
const serialized = serializer(err) | ||
t.equal(serialized.type, 'Error') | ||
t.equal(serialized.message, 'foo: bar: abc') | ||
t.match(serialized.stack, /err\.test\.js:/) | ||
t.match(serialized.stack, /Error: foo/) | ||
t.match(serialized.stack, /Error: bar/) | ||
t.match(serialized.stack, /Error: abc/) | ||
t.notOk(serialized.cause) | ||
} | ||
}) | ||
@@ -165,3 +170,3 @@ | ||
test('pass through anything that is not an Error', function (t) { | ||
test('pass through anything that does not look like an Error', function (t) { | ||
t.plan(3) | ||
@@ -196,16 +201,18 @@ | ||
test('serializes aggregate errors', { skip: !global.AggregateError }, function (t) { | ||
t.plan(8) | ||
t.plan(14) | ||
const foo = new Error('foo') | ||
const bar = new Error('bar') | ||
const aggregate = new AggregateError([foo, bar], 'aggregated message') // eslint-disable-line no-undef | ||
const serialized = serializer(aggregate) | ||
t.equal(serialized.type, 'AggregateError') | ||
t.equal(serialized.message, 'aggregated message') | ||
t.equal(serialized.aggregateErrors.length, 2) | ||
t.equal(serialized.aggregateErrors[0].message, 'foo') | ||
t.equal(serialized.aggregateErrors[1].message, 'bar') | ||
t.match(serialized.aggregateErrors[0].stack, /^Error: foo/) | ||
t.match(serialized.aggregateErrors[1].stack, /^Error: bar/) | ||
t.match(serialized.stack, /err\.test\.js:/) | ||
for (const aggregate of [ | ||
new AggregateError([foo, bar], 'aggregated message'), // eslint-disable-line no-undef | ||
{ errors: [foo, bar], message: 'aggregated message', stack: 'err.test.js:' } | ||
]) { | ||
const serialized = serializer(aggregate) | ||
t.equal(serialized.message, 'aggregated message') | ||
t.equal(serialized.aggregateErrors.length, 2) | ||
t.equal(serialized.aggregateErrors[0].message, 'foo') | ||
t.equal(serialized.aggregateErrors[1].message, 'bar') | ||
t.match(serialized.aggregateErrors[0].stack, /^Error: foo/) | ||
t.match(serialized.aggregateErrors[1].stack, /^Error: bar/) | ||
t.match(serialized.stack, /err\.test\.js:/) | ||
} | ||
}) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
41792
1131