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

Ensure error objects are propagated without modification #1920

Merged
merged 6 commits into from Nov 3, 2023

Conversation

nwalters512
Copy link
Contributor

This PR specifically addresses the case of an AggregateError being thrown, though it'll also handle the case of any Error object with an empty message.

When an AggregateError is constructed with only a single argument, its message property is the empty string. This means that the err.message check fails, and the AggregateError is passed to the Error constructor, losing information about it.

I believe the instanceof Error check is most robust here, though it would be a change in behavior if someone was relying on being able to throw something like { message: 'foo' }, i.e., something that's not actually an Error instance. An alternative would be to check for the presence of err.errors, which would specifically handle the case of err being an AggregateError.

I wrote my test such that it doesn't rely on the AggregateError global, which isn't available in some of the versions of Node that this repo is tested against. Instead, it checks that an Error with an empty message is propagated back to the caller without being wrapped in another instance of Error.

Copy link
Collaborator

@aearly aearly left a comment

Choose a reason for hiding this comment

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

Can we keep the old behavior as well? I'm sure there are cases where people are throwing error-like objects that aren't actual Errors.

lib/asyncify.js Outdated Show resolved Hide resolved
Co-authored-by: Alex Early <alexander.early@gmail.com>
@nwalters512
Copy link
Contributor Author

@aearly done! I agree it's best to preserve compatibility with things that users may have been doing.

@aearly
Copy link
Collaborator

aearly commented Nov 1, 2023

Finally ran the CI pipeline, and noticed a lint error. Apparently AggregateError is a relatively recent addition (node 15), and we're testing on node 12. I update the pipeline to run on node 20, can you rebase your PR?

@nwalters512
Copy link
Contributor Author

nwalters512 commented Nov 1, 2023

@aearly done! Could you approve the CI workflow again? That said, I think the following line might have to change to es2021 for ESLint to be happy with the AggregateError global:

"es6": true

If you'd like to avoid that, I can rewrite this test to use a plain Error object. That is, I'd throw an error with an empty message and check for strict object equality. That should fail with the old code and succeed with my changes.

@xescuder
Copy link

xescuder commented Nov 3, 2023

Hi, any maintainer can approve this? I'm really interested. We're using external libraries that generate errors without message field (they have errorMessages, ...) and then we get a String that says [Object object] error, without information of the real cause.

For us it's very urgent. Thanks a lot.

@aearly
Copy link
Collaborator

aearly commented Nov 3, 2023

Can you rebase again?

@nwalters512
Copy link
Contributor Author

nwalters512 commented Nov 3, 2023

@aearly sorry for not providing more precise instructions: the key es6 needs to change to es2021, not the value. That is, your .eslintrc.json should start with the following:

{
    "env": {
        "browser": true,
        "node": true,
        "es2021": true
    },

Let me know if you'd like me to open a PR with that or incorporate it into this PR, I'd be more than happy to do so.

@aearly
Copy link
Collaborator

aearly commented Nov 3, 2023

Yes, at this point you're better off adding it to your PR.

@nwalters512
Copy link
Contributor Author

@aearly done in 8521e9d, could you re-approve the workflow run?

@aearly aearly merged commit 0412f8d into caolan:master Nov 3, 2023
7 checks passed
@nwalters512 nwalters512 deleted the propagate-aggregate-error branch November 3, 2023 21:49
@aearly
Copy link
Collaborator

aearly commented Nov 3, 2023

Published in v3.2.5

@nwalters512
Copy link
Contributor Author

Many thanks for the quick release!

@xescuder
Copy link

xescuder commented Nov 6, 2023

Many thanks, great work!

@xescuder
Copy link

xescuder commented Nov 7, 2023

Hi, @nwalters512. I don't know why but if you check main branch the problem is still in there. Was the merge correct?
Now I see still:
invokeCallback(callback, err && (err instanceof Error || err.message) ? err : new Error(err));

@nwalters512
Copy link
Contributor Author

@xescuder I'd recommend opening a new issue instead of continuing conversation on this merged PR. When you open one, can you be more specific with the problem you're seeing, and maybe provide a failing test?

@xescuder
Copy link

xescuder commented Nov 7, 2023

I've opened a new one. Many thanks.

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.

None yet

3 participants