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

Conversation

KoltesDigital
Copy link

@KoltesDigital KoltesDigital commented Jun 13, 2022

Before this PR:

const cause = evaluateCause(err.cause)
return typeof cause === 'function'
    ? cause()
    : cause

err is not bound as this to the cause method when calling evaluateCause. The call to cause executes Error.prototype.cause (or its overload), but this refers to the global object, whereas it should be err. This leads to errors as soon as this is used in the cause method implementation.

This PR fixes this behavior.

Copy link
Member

@jsumners jsumners left a comment

Choose a reason for hiding this comment

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

Please add a unit test that exemplifies the issue.


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.

@KoltesDigital
Copy link
Author

Agreed for the test, here it is. It fails before the fix.

@KoltesDigital KoltesDigital changed the title Fix evaluateCause loosing access to this Fix evaluateCause loosing access to err Jun 13, 2022
test/err.test.js Outdated Show resolved Hide resolved
@KoltesDigital KoltesDigital force-pushed the fix-cause-this branch 2 times, most recently from 01cbb10 to 20c44ec Compare June 13, 2022 13:01
@jsumners
Copy link
Member

@voxpelli please take a look at this.

voxpelli added a commit to voxpelli/pino-std-serializers that referenced this pull request Jun 13, 2022
Instead rely on its content being appended to the message and the stack.

This is an alternative fix to pinojs#94, replacing pinojs#105 and pinojs#108 + makes pinojs#109 not needed
@voxpelli
Copy link
Contributor

I added an alternate solution to it, which removed the new competing serialization system and relies on the original summary system: #110

mcollina pushed a commit that referenced this pull request Jun 13, 2022
…ead (#110)

* Revert "Nested VError causes (#108)"

This reverts commit 5b88f7e.

* Revert "Serialize errors with cause (#105)"

This reverts commit ae83956.

* Never serialize `.cause` when an `Error`

Instead rely on its content being appended to the message and the stack.

This is an alternative fix to #94, replacing #105 and #108 + makes #109 not needed

* Add tests

* Add explainer comment
@voxpelli
Copy link
Contributor

This PR can be closed now that #110 has been merged

@jsumners jsumners closed this Jun 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants