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

fs,test: throw rather than assert if condition is possible #32028

Closed
wants to merge 3 commits into from

Conversation

Trott
Copy link
Member

@Trott Trott commented Mar 1, 2020

Three commits:

fs: throw rather than assert on errant monkey-patching

It is possible to make `.close()` assert on an FSWatcher if the
`_handle` property is monkey-patched. Assertions should be reserved for
things that are believed to be logically impossible. Since we know end
users can cause the assertion, change it to a throw.

test: remove common.expectsInternal Assertion

Remove convenience function for internal assertions. It is only used
once.

test: add coverage for FSWatcher exception

Cover an previously uncovered exception possible in the internal start
function for FSWatcher.
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

It is possible to make `.close()` assert on an FSWatcher if the
`_handle` property is monkey-patched. Assertions should be reserved for
things that are believed to be logically impossible. Since we know end
users can cause the assertion, change it to a throw.
Remove convenience function for internal assertions. It is only used
once.
Cover an previously uncovered exception possible in the internal start
function for FSWatcher.
@Trott Trott added the semver-major PRs that contain breaking changes and should be released in the next major version. label Mar 1, 2020
@nodejs-github-bot nodejs-github-bot added errors Issues and PRs related to JavaScript errors originated in Node.js core. fs Issues and PRs related to the fs subsystem / file system. labels Mar 1, 2020
@nodejs-github-bot

This comment has been minimized.

@nodejs-github-bot

This comment has been minimized.

@@ -163,7 +162,9 @@ FSWatcher.prototype[kFSWatchStart] = function(filename,
if (this._handle === null) { // closed
return;
}
assert(this._handle instanceof FSEvent, 'handle must be a FSEvent');
Copy link
Member

Choose a reason for hiding this comment

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

I prefer to keep it as is. Use of assertions to ensure that requirements are met is a common pattern used by many languages. Also the ERR_UNEXPECTED_INSTANCE custom error does not make sense in my opinion. It's an error that should not exist.

That said I won't block this if others are ok with removing assertions from the code base.

Copy link
Member Author

Choose a reason for hiding this comment

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

prefer to keep it as is. Use of assertions to ensure that requirements are met is a common pattern used by many languages.

I certainly am not excited about adding another error code--wish we had vastly fewer codes--so could leave the assert() part alone. This PR would then just add the test case and remove the expectInternalAssertion() from common.

@addaleax
Copy link
Member

addaleax commented Mar 1, 2020

I’m feeling the same way as @lpinca – I’d prefer to keep these as assertions. Throwing a specific kind of error would make monkey-patching seem like a supported API feature. (Which it is not in this case.)

Assertions should be reserved for things that are believed to be logically impossible.

We do use assertions a lot in situations in which users can break them by monkey-patching or illegally accessing internals.

@Trott
Copy link
Member Author

Trott commented Mar 1, 2020

I’d prefer to keep these as assertions.

I could leave the assert() part alone. This PR would then just add the test case and remove the expectInternalAssertion() from common (which is undocumented, unused in other tests, and trivially wraps assert.throws()).

We do use assertions a lot in situations in which users can break them by monkey-patching or illegally accessing internals.

Would you be in favor, at least in theory, of semver-major hardening things like what's in this PR to make them more difficult or impossible to monkey-patch? Or would there be concern that will just make life harder for APM folks? (Maybe they already have better options?)

@nodejs-github-bot
Copy link
Collaborator

@Trott
Copy link
Member Author

Trott commented Mar 3, 2020

Closing in favor of #32057 which adds the test case and removes the common module convenience function, but leaves the internal asserts.

@Trott Trott closed this Mar 3, 2020
@Trott Trott deleted the int-assert branch April 14, 2022 11:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
errors Issues and PRs related to JavaScript errors originated in Node.js core. fs Issues and PRs related to the fs subsystem / file system. 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.

4 participants