Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix evaluateCause loosing access to err #109

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lib/err-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@
const getErrorCause = (err) => {
if (!err) return

const cause = evaluateCause(err.cause)
const cause = evaluateCause(err)

return cause instanceof Error
? cause
: undefined
}

/**
* @param {unknown|(()=>err)} cause
* @param {Error|{ cause?: unknown|(()=>err)}} err
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this change mean?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

evaluateCause now takes the same argument as getErrorCause

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I completely missed we have that jsdoc. I don't think it is even valid.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't tell, haven't used JSDoc for a long time.

* @returns {Error|undefined}
*/
const evaluateCause = (cause) => {
const evaluateCause = (err) => {
// VError / NError style causes are functions
return typeof cause === 'function'
? cause()
: cause
return typeof err.cause === 'function'
? err.cause()
: err.cause
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/err.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function errSerializer (err) {
}

if (err.cause) {
const cause = evaluateCause(err.cause)
const cause = evaluateCause(err)
_err.cause = errSerializer(cause)
}

Expand Down
22 changes: 16 additions & 6 deletions test/err.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,26 @@ 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
class VError extends Error {
constructor (message, cause) {
super(message + ': ' + cause.message)
this._cause = cause
}

// Ensure `cause` references `this` (see https://github.com/pinojs/pino-std-serializers/pull/109)
cause () {
return this._cause
}
}

const causeErr = Error('bar')
causeErr.cause = Error('abc')

const err = new VError('foo', causeErr)

const serialized = serializer(err)

t.equal(serialized.type, 'Error')
t.equal(serialized.type, 'VError')
t.equal(serialized.message, 'foo: bar: abc') // message serialization already walks cause-chain
t.match(serialized.stack, /err\.test\.js:/)

Expand Down