-
Notifications
You must be signed in to change notification settings - Fork 29.9k
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
assert: improve ifError #18247
assert: improve ifError #18247
Conversation
assert.doesNotThrow(() => { assert.ifError(null); }); | ||
assert.doesNotThrow(() => { assert.ifError(); }); | ||
assert.doesNotThrow(() => { assert.ifError(undefined); }); | ||
assert.doesNotThrow(() => { assert.ifError(false); }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do others think about actually throwing in such a case? I personally think it would be best to only accept null
and undefined
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, it's already semver-major so let's do it. Can you also update the PR description with this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm thinking this might not be a good idea.
If an error we didn't want is passed, I think we prefer to see the original stacktrace and not the new one.
// Calling test will use `stdout` to assert value of `err.message` | ||
console.log(err.message); | ||
}); | ||
|
||
async function fn() { | ||
return await Promise.reject(sentinel); | ||
return Promise.reject(sentinel); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you remove await
there is no need for this to be an async function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, true. I am not sure why it was a async function in the first place. I guess a oversight.
@mcollina about the changed stack trace: you should have both. You can access the original stack trace by checking the I could also combine the stack traces (I thought about it before but I rejected it again) if that feels better to others. |
If you put it in |
@mcollina I rewrote this to extend the actual error with the necessary frames. So now you get a combined stack that is way more informative than before. |
} | ||
} | ||
newErr.stack = `${tmp1.join('\n')}\n${tmp2.join('\n')}`; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the goal of this code is not really clear, can you add some comments?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
assert(threw); | ||
})(); | ||
})(); | ||
})(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer a test showing a full stack trace, rather than this check. It would mean future edits are easier to understand and fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a separate test to check for the stack trace and minimized this test to the necessary parts.
assert.doesNotThrow(() => { assert.ifError(null); }); | ||
assert.doesNotThrow(() => { assert.ifError(); }); | ||
assert.doesNotThrow(() => { assert.ifError(undefined); }); | ||
assert.doesNotThrow(() => { assert.ifError(false); }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, it's already semver-major so let's do it. Can you also update the PR description with this change?
It is hard to know where ifError is actually triggered due to the original error being thrown. This changes it by wrapping the original error in a AssertionError. This has the positive effect of also making clear that it is indeed a assertion function that triggered that error. The original stack can still be accessed by checking the `actual` property.
Destructure the necessary Error classes from internal/errors. This improves the readability of the error creation.
This makes `assert.ifError` stricter by only accepting `null` and `undefined` from now on. Before any truthy value was accepted.
58d658c
to
34f02d9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
It is hard to know where ifError is actually triggered due to the original error being thrown. This changes it by wrapping the original error in a AssertionError. This has the positive effect of also making clear that it is indeed a assertion function that triggered that error. The original stack can still be accessed by checking the `actual` property. PR-URL: nodejs#18247 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
Destructure the necessary Error classes from internal/errors. This improves the readability of the error creation. PR-URL: nodejs#18247 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
This makes `assert.ifError` stricter by only accepting `null` and `undefined` from now on. Before any truthy value was accepted. PR-URL: nodejs#18247 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
Landed in 7a23fc0...e65a6e8 |
Looks like this may have landed without a clean CI run? In any event, the message test added here seems to be failing 100% of the time on Windows CI. (Am I wrong somehow about that?) Is there a PR to fix that or should we revert until it's fixed? |
Oh, I see the problem. It's expecting forward slashes, but on Windows, the path separator is (of course) backslashes. Opening a PR to fix. |
Destructure the necessary Error classes from internal/errors. This improves the readability of the error creation. PR-URL: nodejs#18247 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
Destructure the necessary Error classes from internal/errors. This improves the readability of the error creation. Backport-PR-URL: #19230 PR-URL: #18247 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
Destructure the necessary Error classes from internal/errors. This improves the readability of the error creation. Backport-PR-URL: #19230 PR-URL: #18247 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
It is hard to know where ifError is actually triggered due to the original error being thrown. This changes it by wrapping the original error in a AssertionError. This has the positive effect of also making clear that it is indeed a assertion function that triggered that error. The original stack can still be accessed by checking the `actual` property. PR-URL: nodejs#18247 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
Destructure the necessary Error classes from internal/errors. This improves the readability of the error creation. PR-URL: nodejs#18247 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
This makes `assert.ifError` stricter by only accepting `null` and `undefined` from now on. Before any truthy value was accepted. PR-URL: nodejs#18247 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
It is hard to know where
ifError
is actually triggered due to theoriginal error being thrown.
This changes it by wrapping the original error in a AssertionError.
This has the positive effect of also making clear that it is indeed
a assertion function that triggered that error.
The original stack can still be accessed by checking the
actual
property.
Show only the Error class and not
errors.AssertionError
.Make
ifError
stricter by only acceptingnull
andundefined
from now on.Before any truthy value was accepted.
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
assert