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: fix AssertException, assign error code #12651

Closed
wants to merge 1 commit into from

Conversation

jasnell
Copy link
Member

@jasnell jasnell commented Apr 25, 2017

Using assert.AssertException() without the new keyword results in a non-intuitive error:

> assert.AssertionError({})
TypeError: Cannot assign to read only property 'name' of function 'function ok(value, message) {
  if (!value) fail(value, true, message, '==', assert.ok);
}'
    at Function.AssertionError (assert.js:45:13)
    at repl:1:8
    at realRunInThisContextScript (vm.js:22:35)
    at sigintHandlersWrap (vm.js:98:12)
    at ContextifyScript.Script.runInThisContext (vm.js:24:12)
    at REPLServer.defaultEval (repl.js:346:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.onLine (repl.js:545:10)
    at emitOne (events.js:101:20)
>

The assert.AssertionError() can only be used correctly with new, so this converts it into a proper ES6 class that will give an appropriate error message.

This also associates the appropriate internal/errors code with all assert.AssertionError instances and updates the appropriate test cases.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
Affected core subsystem(s)

assert

@jasnell jasnell added assert Issues and PRs related to the assert subsystem. 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. labels Apr 25, 2017
@nodejs-github-bot nodejs-github-bot added the assert Issues and PRs related to the assert subsystem. label Apr 25, 2017
@Trott
Copy link
Member

Trott commented Apr 25, 2017

If I'm understanding correctly, this makes new AssertionError() work if assert module has been required. Otherwise, one will get a ReferenceError when trying to create an AssertionError?

Are we OK with that sort of magic? (AssertionError exists if assert has been required but otherwise not.)

@Trott
Copy link
Member

Trott commented Apr 25, 2017

Ooops! No, I misread it entirely and I'm totally wrong, I think. Ignore my comment! :-D

@jasnell
Copy link
Member Author

jasnell commented Apr 25, 2017

@Trott ... heh... AssertionError is already exported by assert... it's already a requirement to require('assert') to use it.


function truncate(s, n) {
return s.slice(0, n);
// TODO(jasnell): Consider moving AssertionError into internal/errors.js
Copy link
Member

Choose a reason for hiding this comment

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

Nit: I dislike TODO comments because they tend to confuse others rather than communicate clearly what's going on. See all the longstanding TODOs that we just can't seem to address and all the confusion it sows for people reading them in #4641 #4642 #4640

In this case, what are the considerations? I'd rather see this opened as a separate issue with full sentences and paragraphs explaining the issue.

On the other hand, bonus points for putting your name there so at least the reader knows who to ask.

You can totally ignore this nit, of course. But in the off-chance you agree, then please open an issue.

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 expected at least one person to ask about this one :-) ... if we think it's a good idea, then I can just add a commit to this PR that moves it. It would be a minimally invasive change and would allow us to simplify some of the code. I just didn't do it yet because I want to know if there are any objections before doing so.

Copy link
Member

Choose a reason for hiding this comment

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

TODO comments like this are still better than not having them, IMO.

@Trott
Copy link
Member

Trott commented Apr 25, 2017

SGTM in principle. Since this involves significant changes to lib/assert.js and that happens to be one of the files we have complete test coverage for, it would be great if a coverage run could confirm that we still have 100% coverage across the board for it after these changes. I haven't tried to do a coverage run locally myself so I'm actually not sure how much work that is. If it's onerous, never mind. :-D

@jasnell
Copy link
Member Author

jasnell commented Apr 25, 2017

I'll give the coverage run a go, but considering the fact that the tests had not previously caught that calling assert.AssertionError() without new was throwing a not-so-useful error, the 100% coverage is just a tad bit dubious ;-)

@Trott
Copy link
Member

Trott commented Apr 25, 2017

I'll give the coverage run a go, but considering the fact that the tests had not previously caught that calling assert.AssertionError() without new was throwing a not-so-useful error, the 100% coverage is just a tad bit dubious ;-)

I know you're being facetious (I hope), but that won't stop me from saying: 100% coverage just means that all the code gets exercised, not that there are no unhelpful error messages or (for that matter) bugs. 100% coverage isn't a guarantee that everything is tested. But 90% coverage is a guarantee that not everything is tested. So let's keep it at 100%. :-)

@jasnell
Copy link
Member Author

jasnell commented Apr 25, 2017

Yes I am being facetious ;-) ... I'm running the coverage report now

@jasnell
Copy link
Member Author

jasnell commented Apr 26, 2017

@Trott ... updated! This should have 100% coverage on the assert module now

@jasnell
Copy link
Member Author

jasnell commented Apr 27, 2017

Ping @Trott (and other @nodejs/ctc)

@jasnell
Copy link
Member Author

jasnell commented Apr 27, 2017

@Fishrock123
Copy link
Member

No comment on the code but +1 to the idea.

@Trott
Copy link
Member

Trott commented Apr 27, 2017

CI is failing. Something needs some adjusting....

@jasnell
Copy link
Member Author

jasnell commented Apr 27, 2017

Ah... yeah, non-obvious rebase conflict. Rebasing and updating now.

@jasnell
Copy link
Member Author

jasnell commented Apr 27, 2017

@jasnell
Copy link
Member Author

jasnell commented Apr 27, 2017

CI is green.

Using `assert.AssertException()` without the `new` keyword results
in a non-intuitive error:

```js
> assert.AssertionError({})
TypeError: Cannot assign to read only property 'name' of function 'function ok(value, message) {
  if (!value) fail(value, true, message, '==', assert.ok);
}'
    at Function.AssertionError (assert.js:45:13)
    at repl:1:8
    at realRunInThisContextScript (vm.js:22:35)
    at sigintHandlersWrap (vm.js:98:12)
    at ContextifyScript.Script.runInThisContext (vm.js:24:12)
    at REPLServer.defaultEval (repl.js:346:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.onLine (repl.js:545:10)
    at emitOne (events.js:101:20)
>
```

The `assert.AssertionError()` can only be used correctly with `new`,
so this converts it into a proper ES6 class that will give an
appropriate error message.

This also associates the appropriate internal/errors code with all
`assert.AssertionError` instances and updates the appropriate test
cases.
@jasnell jasnell added this to the 8.0.0 milestone Apr 28, 2017
@jasnell
Copy link
Member Author

jasnell commented Apr 29, 2017

ping @nodejs/ctc ... I'd like to get this landed in time for 8.0.0 :-)

Copy link
Member

@mhdawson mhdawson left a comment

Choose a reason for hiding this comment

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

LGTM and nice to see ongoing progress on supporting error codes.

@jasnell
Copy link
Member Author

jasnell commented May 4, 2017

Landed in 3f34ede

@jasnell jasnell closed this May 4, 2017
@targos
Copy link
Member

targos commented May 4, 2017

Shouldn't it be "AssertionError" instead of "AssertException" in the commit message?

@jasnell
Copy link
Member Author

jasnell commented May 4, 2017

doh! ok, we're still within the ten minutes so I'm going to push a fix

@jasnell
Copy link
Member Author

jasnell commented May 4, 2017

fixed!

jasnell added a commit that referenced this pull request May 4, 2017
Using `assert.AssertionError()` without the `new` keyword results
in a non-intuitive error:

```js
> assert.AssertionError({})
TypeError: Cannot assign to read only property 'name' of function 'function ok(value, message) {
  if (!value) fail(value, true, message, '==', assert.ok);
}'
    at Function.AssertionError (assert.js:45:13)
    at repl:1:8
    at realRunInThisContextScript (vm.js:22:35)
    at sigintHandlersWrap (vm.js:98:12)
    at ContextifyScript.Script.runInThisContext (vm.js:24:12)
    at REPLServer.defaultEval (repl.js:346:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.onLine (repl.js:545:10)
    at emitOne (events.js:101:20)
>
```

The `assert.AssertionError()` can only be used correctly with `new`,
so this converts it into a proper ES6 class that will give an
appropriate error message.

This also associates the appropriate internal/errors code with all
`assert.AssertionError` instances and updates the appropriate test
cases.

PR-URL: #12651
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
anchnk pushed a commit to anchnk/node that referenced this pull request May 6, 2017
Using `assert.AssertionError()` without the `new` keyword results
in a non-intuitive error:

```js
> assert.AssertionError({})
TypeError: Cannot assign to read only property 'name' of function 'function ok(value, message) {
  if (!value) fail(value, true, message, '==', assert.ok);
}'
    at Function.AssertionError (assert.js:45:13)
    at repl:1:8
    at realRunInThisContextScript (vm.js:22:35)
    at sigintHandlersWrap (vm.js:98:12)
    at ContextifyScript.Script.runInThisContext (vm.js:24:12)
    at REPLServer.defaultEval (repl.js:346:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.onLine (repl.js:545:10)
    at emitOne (events.js:101:20)
>
```

The `assert.AssertionError()` can only be used correctly with `new`,
so this converts it into a proper ES6 class that will give an
appropriate error message.

This also associates the appropriate internal/errors code with all
`assert.AssertionError` instances and updates the appropriate test
cases.

PR-URL: nodejs#12651
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
@jasnell jasnell mentioned this pull request May 11, 2017
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. 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.

None yet

7 participants