Skip to content

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

Closed
wants to merge 2 commits into from

Conversation

BridgeAR
Copy link
Member

@BridgeAR BridgeAR commented Apr 7, 2018

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), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

@BridgeAR BridgeAR added the semver-major PRs that contain breaking changes and should be released in the next major version. label Apr 7, 2018
@nodejs-github-bot nodejs-github-bot added assert Issues and PRs related to the assert subsystem. errors Issues and PRs related to JavaScript errors originated in Node.js core. labels Apr 7, 2018
@BridgeAR
Copy link
Member Author

BridgeAR commented Apr 7, 2018

@@ -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
Copy link
Contributor

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?


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
Copy link
Contributor

Choose a reason for hiding this comment

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

a ambiguous -> ambiguous

@Trott
Copy link
Member

Trott commented Apr 8, 2018

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.

@BridgeAR
Copy link
Member Author

BridgeAR commented Apr 8, 2018

@BridgeAR
Copy link
Member Author

BridgeAR commented Apr 9, 2018

@Trott it seems like the CITGM came out green.

@Trott
Copy link
Member

Trott commented Apr 9, 2018

@nodejs/testing (because: assert)
@nodejs/tsc (because: semver-major)

@targos
Copy link
Member

targos commented Apr 9, 2018

The koa failure in CITGM is related to this change

@BridgeAR
Copy link
Member Author

BridgeAR commented Apr 9, 2018

@targos thanks, I missed that (Note to myself: always run master to compare...). I am looking into it.

@BridgeAR
Copy link
Member Author

BridgeAR commented Apr 9, 2018

It seems like this PR detected bugs in the koa tests :-). The message was used wrongly there and that resulted in the errors.

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.
@BridgeAR BridgeAR force-pushed the improve-wrong-throws-usage branch from 47e3255 to 566f6dd Compare April 10, 2018 02:07
@BridgeAR
Copy link
Member Author

Rebased due to conflicts.

New CI https://ci.nodejs.org/job/node-test-pull-request/14167/

@BridgeAR
Copy link
Member Author

Ok, the tests in koa are fixed and I also opened a PR for coffeescript as that also came up when I looked closer.

@BridgeAR
Copy link
Member Author

@nodejs/testing PTAL. This PR already detected two modules that wrongly used the error argument and I would like to get this in.

@BridgeAR
Copy link
Member Author

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/

@BridgeAR BridgeAR requested a review from a team April 12, 2018 13:28
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

LGTM

@BridgeAR BridgeAR added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Apr 12, 2018
@BridgeAR BridgeAR requested a review from a team April 12, 2018 14:53
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.
Copy link
Member

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.

@BridgeAR
Copy link
Member Author

Landed in a25f567

@BridgeAR BridgeAR closed this Apr 13, 2018
BridgeAR added a commit to BridgeAR/node that referenced this pull request Apr 13, 2018
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]>
jasnell pushed a commit that referenced this pull request Apr 16, 2018
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]>
sharwell added a commit to sharwell/antlr4ts that referenced this pull request Nov 25, 2018
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
sharwell added a commit to sharwell/antlr4ts that referenced this pull request Nov 29, 2018
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
@BridgeAR BridgeAR deleted the improve-wrong-throws-usage branch April 1, 2019 23:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
assert Issues and PRs related to the assert subsystem. author ready PRs that have at least one approval, no pending requests for changes, and a CI started. errors Issues and PRs related to JavaScript errors originated in Node.js core. semver-major PRs that contain breaking changes and should be released in the next major version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants