-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
assert: detect faulty throws usage #19867
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
Conversation
doc/api/errors.md
Outdated
@@ -599,6 +599,14 @@ A special type of error that can be triggered whenever Node.js detects an | |||
exceptional logic violation that should never occur. These are raised typically | |||
by the `assert` module. | |||
|
|||
<a id="ERR_AMBIGUOUS_ARGUMENT"></a> | |||
### ERR_AMBIGUOUS_ARGUMENT |
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.
Should it go before the ERR_ARG_NOT_ITERABLE
ABC-wise?
doc/api/errors.md
Outdated
|
||
This is triggered by the `assert` module in case e.g., | ||
`assert.throws(fn, message)` is used in a way that the message is the thrown | ||
error message. This is a ambiguous because the message is not verifying the |
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.
a ambiguous
-> ambiguous
I'd be interested to have feedback from ecosystem testing framework maintainers on this. Not sure how to solicit that feedback, though. I guess a CITGM run would be a first step though. |
Comments addressed. CI: https://ci.nodejs.org/job/node-test-pull-request/14127/ |
@Trott it seems like the CITGM came out green. |
@nodejs/testing (because: |
The koa failure in CITGM is related to this change |
@targos thanks, I missed that (Note to myself: always run master to compare...). I am looking into it. |
It seems like this PR detected bugs in the |
One of the biggest downsides to the `assert.throws` API is that it does not check for the error message in case that is used as second argument. It will instead be used in case no error is thrown. This improves the situation by checking the actual error message against the provided one and throws an error in case they are identical. It is very unlikely that the user wants to use that error message as information instead of checking against that message.
47e3255
to
566f6dd
Compare
Rebased due to conflicts. New CI https://ci.nodejs.org/job/node-test-pull-request/14167/ |
Ok, the tests in koa are fixed and I also opened a PR for coffeescript as that also came up when I looked closer. |
@nodejs/testing PTAL. This PR already detected two modules that wrongly used the |
Fixed a edge case that I just saw by looking at this again. New CI https://ci.nodejs.org/job/node-test-pull-request/14181/ |
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
This is triggered by the `assert` module in case e.g., | ||
`assert.throws(fn, message)` is used in a way that the message is the thrown | ||
error message. This is ambiguous because the message is not verifying the error | ||
message and will only be thrown in case no error is thrown. |
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.
Non-blocking but if someone can help tighten this text up a bit, that would be good. @nodejs/documentation It communicates what it needs to, but there is room to make it a bit more clear I think. Doesn't have to happen in this PR (or at all). It could always happen at a later date.
Landed in a25f567 |
One of the biggest downsides to the `assert.throws` API is that it does not check for the error message in case that is used as second argument. It will instead be used in case no error is thrown. This improves the situation by checking the actual error message against the provided one and throws an error in case they are identical. It is very unlikely that the user wants to use that error message as information instead of checking against that message. PR-URL: nodejs#19867 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
One of the biggest downsides to the `assert.throws` API is that it does not check for the error message in case that is used as second argument. It will instead be used in case no error is thrown. This improves the situation by checking the actual error message against the provided one and throws an error in case they are identical. It is very unlikely that the user wants to use that error message as information instead of checking against that message. PR-URL: #19867 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
The previous use of strings instead of regular expressions caused the assertion to treat the message as an additional output string should the test function fail to throw. Switching to RegExp causes the assertion to instead validate the error message itself. See nodejs/node#19867
The previous use of strings instead of regular expressions caused the assertion to treat the message as an additional output string should the test function fail to throw. Switching to RegExp causes the assertion to instead validate the error message itself. See nodejs/node#19867
This is a attempt in fixing the ambiguous behavior of the
assert.throws
message. It is the best way to detect it out of my perspective. This ambiguity is the only real problem with the current API. It would of course be nicer if this would have always tested for the error message but changing that is not possible because quite some code will rely on the current behavior.The PR currently relies on #19865 to prevent conflicts after landing that PR. I am going to remove that commit as soon as that lands.Done.Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes