Skip to content

assert: wrap original err on ifError fail #12820

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 1 commit into from

Conversation

refack
Copy link
Contributor

@refack refack commented May 4, 2017

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

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

refack commented May 4, 2017

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

@refack refack requested a review from addaleax May 4, 2017 03:15
@Trott
Copy link
Member

Trott commented May 4, 2017

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
Member

addaleax commented May 4, 2017

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

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.

We should add a test for this..

@jasnell
Copy link
Member

jasnell commented May 4, 2017

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
Contributor Author

refack commented May 4, 2017

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
Contributor

cjihrig commented May 4, 2017

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

@refack
Copy link
Contributor Author

refack commented May 4, 2017

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

Bingo!

@refack refack 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
Contributor Author

refack commented May 4, 2017

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

@refack refack changed the title assert: generate interesting stack trace assert: wrap original err on ifError fail May 4, 2017
@jasnell
Copy link
Member

jasnell commented May 4, 2017

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

@refack
Copy link
Contributor Author

refack commented May 4, 2017

@refack
Copy link
Contributor Author

refack commented May 26, 2017

@refack refack self-assigned this May 26, 2017
@@ -546,4 +546,7 @@ function doesNotThrow(block, /*optional*/error, /*optional*/message) {
_throws(false, block, error, message);
}

assert.ifError = function ifError(err) { if (err) throw err; };
assert.ifError = function ifError(err, message) {
Copy link
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
Contributor Author

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
Contributor Author

refack commented May 27, 2017

@refack
Copy link
Contributor Author

refack commented May 27, 2017

@refack
Copy link
Contributor Author

refack commented Jun 2, 2017

Ping @nodejs/testing

@refack
Copy link
Contributor Author

refack commented Jun 9, 2017

I now kinda hate the current ifError. Re:

1307	parallel/test-util-callbackify	
duration_ms	0.89
severity	fail
stack	->

assert.js:586
assert.ifError = function ifError(err) { if (err) throw err; };
                                                  ^
function () {
    context.actual++;
    return fn.apply(this, arguments);
  }


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
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
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.

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
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
Member

Ping @refack

@BridgeAR
Copy link
Member

BridgeAR commented Oct 2, 2017

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

@BridgeAR BridgeAR closed this Oct 2, 2017
@refack refack removed their assignment Oct 12, 2018
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. semver-major PRs that contain breaking changes and should be released in the next major version. test Issues and PRs related to the tests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants