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 reporter bug when rejecting non-errors #1281

Merged
merged 2 commits into from
Mar 3, 2017
Merged

Conversation

jakwuh
Copy link
Contributor

@jakwuh jakwuh commented Feb 22, 2017

To reproduce try ava with the following test:

import test from 'ava';

test(t => {
    Promise.reject(1);
});

@novemberborn
Copy link
Member

Interesting find. I think the error comes from this line, really:

stack: err

Unhandled rejections are normalized:

err = normalizeError(err);

Perhaps the message should be set to String(err) and stack shouldn't be set at all. Further, error objects that have a non-string message, name or stack should have those properties deleted. That would be in

if (err.stack) {
.

Long story short, this means errors have the expected shape in the reporters and we don't have to keep checking if certain properties are strings etc. @avajs/core thoughts?

@sindresorhus
Copy link
Member

Long story short, this means errors have the expected shape in the reporters and we don't have to keep checking if certain properties are strings etc. @avajs/core thoughts?

👍

@jakwuh
Copy link
Contributor Author

jakwuh commented Feb 23, 2017

@novemberborn thank you for the response. I think having message: String(err) + removing stack property is the best solution here. Isn't it?

@novemberborn
Copy link
Member

@jakwuh yea. Though that's just for rejection reasons that aren't objects. We also need to sanitize objects with unexpected properties.

@jakwuh
Copy link
Contributor Author

jakwuh commented Feb 26, 2017

@novemberborn here we go, now regardfully listening to any feedback :)

if (isObj(err) && 'message' in err && typeof err.message === 'string') {
['name', 'stack'].forEach(field => {
if (field in err && typeof err[field] !== 'string') {
delete err[field];
Copy link
Member

Choose a reason for hiding this comment

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

I think this really does need to go here:

if (err.stack) {

We don't want to modify err since it comes from user code. There may be side-effects. But we can't easily copy error objects since name and message aren't always enumerable. After the call to cleanYamlObject in serialize-error.js we have a copy we can safely modify.

Also, in this case, I'd prefer a little bit of duplication over your forEach trick. IMHO this is a lot easier to follow:

if ('name' in err && typeof err.name !== 'string') {
  delete err.name
}
if ('stack' in err && typeof err.stack !== 'string') {
  delete err.stack
}

Even better, delete only deletes own-properties, so the 'name' in err guard is unnecessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this totally makes sense, still I find fairly to say that errors coming to normalizeError are already serialized (as I can see from the test-worker code).
Also thank you a lot for your fast and detailed feedback.

@jakwuh
Copy link
Contributor Author

jakwuh commented Feb 26, 2017

@novemberborn here we go 🙂

Copy link
Member

@novemberborn novemberborn left a comment

Choose a reason for hiding this comment

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

Great!

@@ -20,6 +20,11 @@ function filter(propertyName, isRoot, source, target) {
return true;
}

if ((propertyName === 'name' || propertyName === 'stack') &&
typeof source[propertyName] !== 'string') {
Copy link
Member

Choose a reason for hiding this comment

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

@sindresorhus any thoughts on this? XO doesn't complain 😉

Copy link
Member

Choose a reason for hiding this comment

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

Why should it have complained?

Copy link
Member

Choose a reason for hiding this comment

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

Figured it may have opinions on if condition indenting.

@sindresorhus sindresorhus changed the title Fix reporter bug Fix reporter bug when rejecting non-errors Mar 3, 2017
@sindresorhus sindresorhus merged commit 157ef25 into avajs:master Mar 3, 2017
@sindresorhus
Copy link
Member

Woot! Very good work @jakwuh :)

@jakwuh
Copy link
Contributor Author

jakwuh commented Mar 3, 2017

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.

3 participants