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: improve ifError #18247

Closed
wants to merge 3 commits into from
Closed

Conversation

BridgeAR
Copy link
Member

@BridgeAR BridgeAR commented Jan 19, 2018

  1. Commit:

It is hard to know where ifError is actually triggered due to the
original error being thrown.
This changes it by wrapping the original error in a AssertionError.
This has the positive effect of also making clear that it is indeed
a assertion function that triggered that error.

The original stack can still be accessed by checking the actual
property.

  1. Commit:

Show only the Error class and not errors.AssertionError.

  1. Commit:

Make ifError stricter by only accepting null and undefined from now on.
Before any truthy value was accepted.

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)

assert

@BridgeAR BridgeAR added 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. notable-change PRs with changes that should be highlighted in changelogs. labels Jan 19, 2018
@nodejs-github-bot nodejs-github-bot added the assert Issues and PRs related to the assert subsystem. label Jan 19, 2018
assert.doesNotThrow(() => { assert.ifError(null); });
assert.doesNotThrow(() => { assert.ifError(); });
assert.doesNotThrow(() => { assert.ifError(undefined); });
assert.doesNotThrow(() => { assert.ifError(false); });
Copy link
Member Author

Choose a reason for hiding this comment

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

What do others think about actually throwing in such a case? I personally think it would be best to only accept null and undefined.

Copy link
Member

Choose a reason for hiding this comment

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

I agree, it's already semver-major so let's do it. Can you also update the PR description with this change?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

@BridgeAR
Copy link
Member Author

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

I'm thinking this might not be a good idea.
If an error we didn't want is passed, I think we prefer to see the original stacktrace and not the new one.

// Calling test will use `stdout` to assert value of `err.message`
console.log(err.message);
});

async function fn() {
return await Promise.reject(sentinel);
return Promise.reject(sentinel);
Copy link
Member

Choose a reason for hiding this comment

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

if you remove await  there is no need for this to be an async function.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hm, true. I am not sure why it was a async function in the first place. I guess a oversight.

@BridgeAR
Copy link
Member Author

@mcollina about the changed stack trace: you should have both. You can access the original stack trace by checking the actual property.

I could also combine the stack traces (I thought about it before but I rejected it again) if that feels better to others.

@mcollina
Copy link
Member

If you put it in actual, most testing frameworks would not print it by default. Then, the information will be lost.

@BridgeAR
Copy link
Member Author

@mcollina I rewrote this to extend the actual error with the necessary frames. So now you get a combined stack that is way more informative than before.

@BridgeAR
Copy link
Member Author

}
}
newErr.stack = `${tmp1.join('\n')}\n${tmp2.join('\n')}`;
}
Copy link
Member

Choose a reason for hiding this comment

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

the goal of this code is not really clear, can you add some comments?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

assert(threw);
})();
})();
})();
Copy link
Member

Choose a reason for hiding this comment

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

I would prefer a test showing a full stack trace, rather than this check. It would mean future edits are easier to understand and fix.

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 added a separate test to check for the stack trace and minimized this test to the necessary parts.

assert.doesNotThrow(() => { assert.ifError(null); });
assert.doesNotThrow(() => { assert.ifError(); });
assert.doesNotThrow(() => { assert.ifError(undefined); });
assert.doesNotThrow(() => { assert.ifError(false); });
Copy link
Member

Choose a reason for hiding this comment

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

I agree, it's already semver-major so let's do it. Can you also update the PR description with this change?

It is hard to know where ifError is actually triggered due to the
original error being thrown.
This changes it by wrapping the original error in a AssertionError.
This has the positive effect of also making clear that it is indeed
a assertion function that triggered that error.

The original stack can still be accessed by checking the `actual`
property.
Destructure the necessary Error classes from internal/errors.
This improves the readability of the error creation.
This makes `assert.ifError` stricter by only accepting `null` and
`undefined` from now on. Before any truthy value was accepted.
@BridgeAR BridgeAR changed the title assert: wrap original error in ifError assert: improve ifError Jan 23, 2018
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

LGTM

BridgeAR added a commit to BridgeAR/node that referenced this pull request Jan 24, 2018
It is hard to know where ifError is actually triggered due to the
original error being thrown.
This changes it by wrapping the original error in a AssertionError.
This has the positive effect of also making clear that it is indeed
a assertion function that triggered that error.

The original stack can still be accessed by checking the `actual`
property.

PR-URL: nodejs#18247
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
BridgeAR added a commit to BridgeAR/node that referenced this pull request Jan 24, 2018
Destructure the necessary Error classes from internal/errors.
This improves the readability of the error creation.

PR-URL: nodejs#18247
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
BridgeAR added a commit to BridgeAR/node that referenced this pull request Jan 24, 2018
This makes `assert.ifError` stricter by only accepting `null` and
`undefined` from now on. Before any truthy value was accepted.

PR-URL: nodejs#18247
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
@BridgeAR
Copy link
Member Author

Landed in 7a23fc0...e65a6e8

@BridgeAR BridgeAR closed this Jan 24, 2018
@Trott
Copy link
Member

Trott commented Jan 25, 2018

Looks like this may have landed without a clean CI run? In any event, the message test added here seems to be failing 100% of the time on Windows CI. (Am I wrong somehow about that?) Is there a PR to fix that or should we revert until it's fixed?

@Trott
Copy link
Member

Trott commented Jan 25, 2018

Oh, I see the problem. It's expecting forward slashes, but on Windows, the path separator is (of course) backslashes. Opening a PR to fix.

@apapirovski
Copy link
Member

@Trott fix already in #18378

BridgeAR added a commit to BridgeAR/node that referenced this pull request Mar 8, 2018
Destructure the necessary Error classes from internal/errors.
This improves the readability of the error creation.

PR-URL: nodejs#18247
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
MylesBorins pushed a commit that referenced this pull request Mar 15, 2018
Destructure the necessary Error classes from internal/errors.
This improves the readability of the error creation.

Backport-PR-URL: #19230
PR-URL: #18247
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
@targos targos mentioned this pull request Mar 18, 2018
MylesBorins pushed a commit that referenced this pull request Mar 20, 2018
Destructure the necessary Error classes from internal/errors.
This improves the readability of the error creation.

Backport-PR-URL: #19230
PR-URL: #18247
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
MayaLekova pushed a commit to MayaLekova/node that referenced this pull request May 8, 2018
It is hard to know where ifError is actually triggered due to the
original error being thrown.
This changes it by wrapping the original error in a AssertionError.
This has the positive effect of also making clear that it is indeed
a assertion function that triggered that error.

The original stack can still be accessed by checking the `actual`
property.

PR-URL: nodejs#18247
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
MayaLekova pushed a commit to MayaLekova/node that referenced this pull request May 8, 2018
Destructure the necessary Error classes from internal/errors.
This improves the readability of the error creation.

PR-URL: nodejs#18247
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
MayaLekova pushed a commit to MayaLekova/node that referenced this pull request May 8, 2018
This makes `assert.ifError` stricter by only accepting `null` and
`undefined` from now on. Before any truthy value was accepted.

PR-URL: nodejs#18247
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
@BridgeAR BridgeAR deleted the improve-assert-iferror branch April 1, 2019 23:38
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. notable-change PRs with changes that should be highlighted in changelogs. 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.

6 participants