Skip to content

assert: wrap original err on ifError fail - #12820

Closed
refack wants to merge 1 commit into
nodejs:masterfrom
refack:assrt-iferror
Closed

assert: wrap original err on ifError fail#12820
refack wants to merge 1 commit into
nodejs:masterfrom
refack:assrt-iferror

Conversation

@refack

@refackrefack commented May 4, 2017

Copy link
Copy Markdown
Contributor

For assert.ifError fails, wrap the original err in a new AssertionError

Inspiration: #12803 (comment)
/cc @nodejs/testing

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
Affected core subsystem(s)

test, assert

@refackrefack added assert Issues and PRs related to the assert subsystem. test Issues and PRs related to the tests. labels May 4, 2017
@nodejs-github-botnodejs-github-bot added the assert Issues and PRs related to the assert subsystem. label May 4, 2017
@refack

refack commented May 4, 2017

Copy link
Copy Markdown
ContributorAuthor

The message will not change, but the stack trace will 🤔

@refack
refack requested a review from addaleaxMay 4, 2017 03:15
@Trott

Trott commented May 4, 2017

Copy link
Copy Markdown
Member

It might be the smallest semver-major ever, but I would still say semver-major.

Imagine the impact on someone reading the stack traces if they run the same test on different versions of Node.js (which is what someone might do if they suspect they are triggering a bug in Node.js itself). If assert.ifError() returns a different stack trace in the same exact situation between Node.js 7.10.0 and Node.js 8.0.0, well, that's what happens between major versions. But different results between Node.js 7.10.0 and 7.11.0? I'd be less understanding about that...

@addaleax

Copy link
Copy Markdown
Member

Is this a good idea, though? I think I would usually be more interested in the original stack trace (and apart from that, I think that this won’t work if somebody accessed err.stack before ifError() is called) to figure out why there was an error

@joyeecheungjoyeecheung left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should add a test for this..

@jasnell

Copy link
Copy Markdown
Member

I'm not sure this is a good change to make. The errors are created elsewhere and passed here. Changing the stack trace would definitely be a breaking change (even if a mild one). I don't think this is behavior anyone would expect.

@refack

Copy link
Copy Markdown
ContributorAuthor

Is this a good idea, though? I think I would usually be more interested in the original stack trace (and apart from that, I think that this won’t work if somebody accessed err.stack before ifError() is called) to figure out why there was an error

One could argue that if you want the original stack, just throw it. If you use assert, you want to knwo which asset failed...
It came to me after I sew this:

c:\code\node$ node --no-deprecation test/parallel/test-fs-chmod.js
33060
assert.js:378
assert.ifError = function ifError(err) { if (err) throw err; };
^
Error: EPERM: operation not permitted, open 'd:\code\node\test\fixtures\a1.js'

@cjihrig

Copy link
Copy Markdown
Contributor

I think we should either throw the existing error, as is, or throw a new assertion error. This hybrid seems confusing.

@refack

Copy link
Copy Markdown
ContributorAuthor

I think we should either throw the existing error, as is, or throw a new assertion error. This hybrid seems confusing.

Bingo!

@refackrefack added the semver-major PRs that contain breaking changes and should be released in the next major version. label May 4, 2017
@refack

Copy link
Copy Markdown
ContributorAuthor

Changed to wrapping the original err in a new AssertionError (per @cjihrig's comment).
Now It's definitely semver-major

@refackrefack changed the title assert: generate interesting stack traceassert: wrap original err on ifError failMay 4, 2017
@jasnell

Copy link
Copy Markdown
Member

Wrapping in the AssertionError works. We will definitely want to run some CITGM tests on this tho

@refack

Copy link
Copy Markdown
ContributorAuthor

@refack

Copy link
Copy Markdown
ContributorAuthor

@refackrefack self-assigned this May 26, 2017
Comment threadlib/assert.js Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

message isn't used.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Ack.
Forgot to carry it over into the last commit.

@refack

Copy link
Copy Markdown
ContributorAuthor

@refack

refack commented May 27, 2017

Copy link
Copy Markdown
ContributorAuthor

@refack

Copy link
Copy Markdown
ContributorAuthor

Ping @nodejs/testing

@refack

Copy link
Copy Markdown
ContributorAuthor

I now kinda hate the current ifError. Re:

1307parallel/test-util-callbackifyduration_ms0.89severityfailstack->assert.js:586assert.ifError=functionifError(err){if(err)throwerr;};^function(){context.actual++;returnfn.apply(this,arguments);}

Comment threaddoc/api/assert.md

Throws an `AssertionError` if `value` is truthy. This is useful when testing the
`error` argument in callbacks. If the `message` parameter is undefined, a
default error message is assigned, and `value` is be appended to it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I feel this is somewhat contradicting each other. If it should be used in callbacks, there should always either be an error or the result but never both at the same time.

common.expectsError({
code: 'ERR_ASSERTION',
type: a.AssertionError,
message: /^Error: test_error_slug$/m

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I am not sure how this is any different from the test above. I actually would expect this test to fail because I would have expected it to be wrapped in the ifError failed part.

Comment threadlib/assert.js
assert.ifError = function ifError(err) { if (err) throw err; };
assert.ifError = function ifError(err, message) {
if (!err) return;
message = message || `ifError failed. Original error:\n${err}`;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hm, is ifError really failing here? Maybe smth. like ifError encountered unexpected error: ... might be more intuitive?

@BridgeAR

Copy link
Copy Markdown
Member

Ping @refack

@BridgeAR

Copy link
Copy Markdown
Member

I am closing this due to the long inactivity. @refack please reopen if you would like to pursue this further.

@BridgeARBridgeAR closed this Oct 2, 2017
@refackrefack removed their assignment Oct 12, 2018
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

assertIssues and PRs related to the assert subsystem.semver-majorPRs that contain breaking changes and should be released in the next major version.testIssues and PRs related to the tests.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants

@refack@Trott@addaleax@jasnell@cjihrig@BridgeAR@joyeecheung@richardlau@nodejs-github-bot