Skip to content

Commit

Permalink
Support for aggregate errors (#87)
Browse files Browse the repository at this point in the history
* feat: support for aggregate errors

* chore: using global aggregate error

* chore: minor refactor

* chore: explicitly send arguments to serializer

* chore: check for infinite recursion

* chore: package.json change reverted

* chore: remove recursion check
  • Loading branch information
sameer-coder authored Jan 19, 2022
1 parent 3025c21 commit 86a6ac9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/err.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ const pinoErrProto = Object.create({}, {
writable: true,
value: undefined
},
aggregateErrors: {
enumerable: true,
writable: true,
value: undefined
},
raw: {
enumerable: false,
get: function () {
Expand Down Expand Up @@ -50,6 +55,11 @@ function errSerializer (err) {
: err.name
_err.message = messageWithCauses(err)
_err.stack = stackWithCauses(err)

if (global.AggregateError !== undefined && err instanceof global.AggregateError && Array.isArray(err.errors)) {
_err.aggregateErrors = err.errors.map(err => errSerializer(err))
}

for (const key in err) {
if (_err[key] === undefined) {
const val = err[key]
Expand Down
17 changes: 17 additions & 0 deletions test/err.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,20 @@ test('can wrap err serializers', function (t) {
t.notOk(serialized.foo)
t.is(serialized.bar, 'bar')
})

test('serializes aggregate errors', { skip: !global.AggregateError }, function (t) {
t.plan(8)
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:/)
})

0 comments on commit 86a6ac9

Please sign in to comment.