-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
Add assertion error docs #19724
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
Add assertion error docs #19724
Conversation
The AssertionError was always exposed but never properly documented. This explains how it is used and what options it accepts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With some nits)
doc/api/errors.md
Outdated
// AssertionError [ERR_ASSERTION]: 1 === 2 | ||
``` | ||
A subclass of `Error` that indicates the failure of an assertion. For further | ||
details check asserts [`Class: AssertionError`][]. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
asserts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a comma after details
and change check
to see
.
doc/api/errors.md
Outdated
@@ -1680,6 +1675,7 @@ Creation of a [`zlib`][] object failed due to incorrect configuration. | |||
[`process.setUncaughtExceptionCaptureCallback()`]: process.html#process_process_setuncaughtexceptioncapturecallback_fn | |||
[`require('crypto').setEngine()`]: crypto.html#crypto_crypto_setengine_engine_flags | |||
[`server.listen()`]: net.html#net_server_listen | |||
[`Class: AssertionError`]: assert.html#class_assertionerror |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
##assert_class_assertionerror
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or, if the suggestions given below are valid, even #assert_class_assert_assertionerror
.
doc/api/assert.md
Outdated
## Class: AssertionError | ||
|
||
A subclass of `Error` that indicates the failure of an assertion. | ||
All errors thrown by the assert module will be instance of the `AssertionError` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure, but maybe instance
-> instances
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert module
-> `assert` module
(add backticks)
doc/api/assert.md
Outdated
@@ -13,6 +13,71 @@ A `strict` and a `legacy` mode exist, while it is recommended to only use | |||
For more information about the used equality comparisons see | |||
[MDN's guide on equality comparisons and sameness][mdn-equality-guide]. | |||
|
|||
## Class: AssertionError |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As this is not global, should not this be ## Class: assert.AssertionError
?
Compare:
Class: net.Server
and Class: net.Socket
doc/api/assert.md
Outdated
All errors thrown by the assert module will be instance of the `AssertionError` | ||
class. | ||
|
||
### new AssertionError(options) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same.
Compare: new net.Server()
and new net.Socket()
.
doc/api/assert.md
Outdated
[`assert.strictEqual()`] is used. | ||
* `expected` {any} Set to the expected value in case e.g., | ||
[`assert.strictEqual()`] is used. | ||
* `generatedMessage` {boolean} Indicates if the message was auto generated |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure, but maybe auto generated
-> auto-generated
?
doc/api/assert.md
Outdated
* `generatedMessage` {boolean} Indicates if the message was auto generated | ||
(`true`) or not. | ||
* `code` {string} This is always set to the string `ERR_ASSERTION` to indicate | ||
that the error is actually a assertion error. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a assertion
-> an assertion
?
doc/api/assert.md
Outdated
```js | ||
const assert = require('assert'); | ||
|
||
// Generate a AssertionError to compare the error message later:. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a AssertionError
-> an AssertionError
?
:.
-> :
?
doc/api/assert.md
Outdated
assert.equal(err.expected, 2); | ||
assert.equal(err.code, 'ERR_ASSERTION'); | ||
assert.equal(err.operator, '=='); | ||
assert.equal(err.generatedMessage, true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if we should popularize legacy loose equality mode here.
Mabe this way?
const assert = require('assert');
// Generate an AssertionError to compare the error message later:
const { message } = new assert.AssertionError({
actual: 1,
expected: 2,
operator: 'strictEqual'
});
// Verify error output:
try {
assert.strictEqual(1, 2);
} catch (err) {
assert(err instanceof assert.AssertionError);
assert.strictEqual(err.message, message);
assert.strictEqual(err.name, 'AssertionError [ERR_ASSERTION]');
assert.strictEqual(err.actual, 1);
assert.strictEqual(err.expected, 2);
assert.strictEqual(err.code, 'ERR_ASSERTION');
assert.strictEqual(err.operator, 'strictEqual');
assert.strictEqual(err.generatedMessage, true);
}
Or maybe with 1
and '1'
?
doc/api/assert.md
Outdated
|
||
A subclass of `Error` that indicates the failure of an assertion. | ||
|
||
All instances contain the built-in Error properties (i.e., `message` and `name`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Add backticks around
Error
. - Is
i.e.
the right abbreviation here? If so, then just get rid ofi.e.
. If not, change it toincluding
orsuch as
.
doc/api/assert.md
Outdated
A subclass of `Error` that indicates the failure of an assertion. | ||
|
||
All instances contain the built-in Error properties (i.e., `message` and `name`) | ||
plus the following: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
plus the following:
-> and:
Since there's substantial new text here: @nodejs/documentation |
Comments addressed. Lite CI: https://ci.nodejs.org/job/node-test-pull-request-lite/378/ |
doc/api/errors.md
Outdated
@@ -365,7 +365,7 @@ detailed [here](#errors_system_errors). | |||
## Class: AssertionError | |||
|
|||
A subclass of `Error` that indicates the failure of an assertion. For further | |||
details check asserts [`Class: AssertionError`][]. | |||
details, see [`Class: AssertionError`][]. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AssertionError
-> assert.AssertionError
?
doc/api/errors.md
Outdated
@@ -1675,7 +1675,7 @@ Creation of a [`zlib`][] object failed due to incorrect configuration. | |||
[`process.setUncaughtExceptionCaptureCallback()`]: process.html#process_process_setuncaughtexceptioncapturecallback_fn | |||
[`require('crypto').setEngine()`]: crypto.html#crypto_crypto_setengine_engine_flags | |||
[`server.listen()`]: net.html#net_server_listen | |||
[`Class: AssertionError`]: assert.html#class_assertionerror | |||
[`Class: AssertionError`]: assert.html#assert_class_assert_assertionerror |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AssertionError
-> assert.AssertionError
?
doc/api/assert.md
Outdated
* `generatedMessage` {boolean} Indicates if the message was auto-generated | ||
(`true`) or not. | ||
* `code` {string} This is always set to the string `ERR_ASSERTION` to indicate | ||
that the error is actually a assertion error. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still a assertion
-> an assertion
?
doc/api/errors.md
Outdated
assert.strictEqual(1, 2); | ||
// AssertionError [ERR_ASSERTION]: 1 === 2 | ||
``` | ||
A subclass of `Error` that indicates the failure of an assertion. For further |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Microscopic nit that can totally be ignored if you want: The word further
can be omitted. For that matter, so can all of For further details,
. It can just be See [`Class: AssertionError`][].
doc/api/errors.md
Outdated
// AssertionError [ERR_ASSERTION]: 1 === 2 | ||
``` | ||
A subclass of `Error` that indicates the failure of an assertion. For further | ||
details, see [`Class: AssertionError`][]. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should it now be Class: assert.AssertionError
?
New comments addressed. New Lite-CI: https://ci.nodejs.org/job/node-test-pull-request-lite/390/ |
The AssertionError was always exposed but never properly documented. This explains how it is used and what options it accepts. PR-URL: nodejs#19724 Reviewed-By: Vse Mozhet Byt <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
PR-URL: nodejs#19724 Reviewed-By: Vse Mozhet Byt <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
The AssertionError was always exposed but never properly documented. This explains how it is used and what options it accepts. PR-URL: #19724 Reviewed-By: Vse Mozhet Byt <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
PR-URL: #19724 Reviewed-By: Vse Mozhet Byt <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
This was never properly documented but it is used in the wild and it makes sense to actually explain how it works and what properties the errors contain. Those properties can be useful for the users.
Please note: I explicitly did not document the
errorDiff
option as I plan on removing it again in 10.x. It was introduced in v.9.9.x and I think we can still go ahead to remove it as it is unlikely that it is already used.Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes