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

assert: move assertion part in extra file #20486

Closed
wants to merge 3 commits into from

Conversation

BridgeAR
Copy link
Member

@BridgeAR BridgeAR commented May 2, 2018

This improves the doc a tiny bit by providing the type for the message and some other minor doc fixes.

It also moves the AssertionError from internal/errors into a new file since the errors begin to become pretty big. Since we changed the way how we create the errors, we also do not have to have the base classes in the errors file anymore.

Besides that I moved the functions from some error codes directly to the code, so the code is directly related instead of having to jump around to see what code belongs to what code.

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

@nodejs-github-bot nodejs-github-bot added assert Issues and PRs related to the assert subsystem. build Issues and PRs related to build files or the CI. errors Issues and PRs related to JavaScript errors originated in Node.js core. labels May 2, 2018
@BridgeAR
Copy link
Member Author

BridgeAR commented May 2, 2018

This adds concrete expected types to the assert documentation.

It also fixes a `changes` entry and improves some minor comments.
This moves the `assert` parts from `internal/errors` into an own
file. `internal/errors` got bigger and bigger and it was difficult
to keep a good overview of what was going on. While doing so it
also removes the `internalAssert` function and just lazy loads
`assert`.
This makes sure the functions are actually directly beneat the
specification of an error code.
That way it is not necessary to jump around when looking at the
functionality.
@BridgeAR
Copy link
Member Author

BridgeAR commented May 7, 2018

Rebased due to conflicts.

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

@nodejs/testing @jasnell @joyeecheung PTAL. This is open for five days without getting a single bit of attention...

@BridgeAR BridgeAR added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label May 7, 2018
Copy link
Member

@joyeecheung joyeecheung left a comment

Choose a reason for hiding this comment

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

LGTM with an open discussion

E('ERR_INVALID_ARG_TYPE', invalidArgType, TypeError);
E('ERR_INVALID_ARG_TYPE',
(name, expected, actual) => {
assert(typeof name === 'string', "'name' must be a string");
Copy link
Member

@joyeecheung joyeecheung May 8, 2018

Choose a reason for hiding this comment

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

In general I think it's pretty odd to use assert in internal/errors.js. My impression is that assert in our own code is used to guard against potential user-land monkey-patching gone wrong, but the internal errors are not open to monkey-patching in any way, and most of the internal error code should be simple enough to debug without assertions (even if they are not that simple now, at least they are supposed to be, since internal/errors is required by almost every module). It is especially odd in cases like this where the formatter of ERR_INVALID_ARG_TYPE is effectively guarding against a ERR_INVALID_ARG_TYPE. Even if we want to guard against our own code, we should probably be using DCHECK macros that are only available in debug mode instead of asserts

Copy link
Member Author

Choose a reason for hiding this comment

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

I did not add anything new here. Before it was just about using internalAssert instead. In this specific case I do not think it provides a lot of benefit to check this (especially since we do not check input types in any other error). There are a few cases in here that make it much easier to implement the error codes though. I found a few cases that were wrong when validating the passed through arguments and I definitely would like to keep that.

That aside: when I worked on #20567 I also tried to remove assert. As far as I see it is partially used in places where we would now use other errors from in here and partially to prevent malicious values getting through to the C++ layer (you could call it "monkey-patching gone wrong" ;-) ).

Using CHECK and DCHECK for these things would definitely be one way to deal with these. There is just one problem: using e.g., DCHECK means our regular builds while coding will not show any errors and we would only see the issues in the CI. I am not sure if that is fine.

Copy link
Member Author

Choose a reason for hiding this comment

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

In general: I guess this discussion is not really about the specific code change?

BridgeAR added a commit to BridgeAR/node that referenced this pull request May 10, 2018
This adds concrete expected types to the assert documentation.

It also fixes a `changes` entry and improves some minor comments.

PR-URL: nodejs#20486
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
BridgeAR added a commit to BridgeAR/node that referenced this pull request May 10, 2018
This moves the `assert` parts from `internal/errors` into an own
file. `internal/errors` got bigger and bigger and it was difficult
to keep a good overview of what was going on. While doing so it
also removes the `internalAssert` function and just lazy loads
`assert`.

PR-URL: nodejs#20486
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
BridgeAR added a commit to BridgeAR/node that referenced this pull request May 10, 2018
This makes sure the functions are actually directly beneath the
specification of an error code.
That way it is not necessary to jump around when looking at the
functionality.

PR-URL: nodejs#20486
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
@BridgeAR
Copy link
Member Author

Landed in cf7be86...5a0e379

@BridgeAR BridgeAR closed this May 10, 2018
targos pushed a commit that referenced this pull request May 12, 2018
This adds concrete expected types to the assert documentation.

It also fixes a `changes` entry and improves some minor comments.

PR-URL: #20486
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
targos pushed a commit that referenced this pull request May 12, 2018
This moves the `assert` parts from `internal/errors` into an own
file. `internal/errors` got bigger and bigger and it was difficult
to keep a good overview of what was going on. While doing so it
also removes the `internalAssert` function and just lazy loads
`assert`.

PR-URL: #20486
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
targos pushed a commit that referenced this pull request May 12, 2018
This makes sure the functions are actually directly beneath the
specification of an error code.
That way it is not necessary to jump around when looking at the
functionality.

PR-URL: #20486
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
@addaleax addaleax mentioned this pull request May 14, 2018
@BridgeAR BridgeAR deleted the improve-assertion-stuff branch January 20, 2020 11:32
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. build Issues and PRs related to build files or the CI. errors Issues and PRs related to JavaScript errors originated in Node.js core.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants