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(console): prevent duplicate error printing when the cause is assi… #25327

Conversation

MujahedSafaa
Copy link
Contributor

@MujahedSafaa MujahedSafaa commented Aug 31, 2024

This commit fixes the error format when the cause is assigned separately, ensuring that the cause is only printed once instead of twice.

The fix addresses issue #21651.

Use Case
When an error instance has a cause assigned separately, it produces the same output as when the cause is passed directly to the Error constructor.
constructed-with-cause.js

const x = new SyntaxError("improper syntax")
    const y = new TypeError("type incorrect", { cause: x })
    console.log(y)

assigned-cause.js

const x = new SyntaxError("improper syntax")
    const y = new TypeError("type incorrect")
    y.cause = x

When running the command deno run constructed-with-cause.js, the cause is printed twice. This occurs because the inspectError function checks for a cause property in the error and appends it to the final message, leading to the first print. The second print happens due to the direct assignment of the cause, which makes it an enumerable property. When the getKeys function retrieves the enumerable keys from the error, it includes cause, resulting in it being appended again to the final message after calling formatProperty, which invokes inspectError once more. as shown in this code:
image
image

Code changes

A) Skip adding the cause as a key since it is already handeled in the inspectError method

@MujahedSafaa
Copy link
Contributor Author

Hi @dsherret @crowlKats
Could you please review this PR? I'm also unsure how to run the test that's failing in the job locally. Do you have any tips on how I can reproduce the failure on my machine?

@MujahedSafaa
Copy link
Contributor Author

Could you please review this PR @crowlKats, and merge it if it's ready?

Copy link
Member

@crowlKats crowlKats left a comment

Choose a reason for hiding this comment

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

LGTM

@crowlKats crowlKats merged commit 4983f76 into denoland:main Sep 12, 2024
17 checks passed
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.

2 participants