Skip to content

Commit 34f02d9

Browse files
committed
assert: stricter ifError
This makes `assert.ifError` stricter by only accepting `null` and `undefined` from now on. Before any truthy value was accepted.
1 parent 56403f7 commit 34f02d9

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

doc/api/assert.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -438,21 +438,23 @@ changes:
438438
pr-url: https://github.com/nodejs/node/pull/18247
439439
description: Instead of throwing the original error it is now wrapped into
440440
a AssertionError that contains the full stack trace.
441+
- version: REPLACEME
442+
pr-url: https://github.com/nodejs/node/pull/18247
443+
description: Value may now only be `undefined` or `null`. Before any truthy
444+
input was accepted.
441445
-->
442446
* `value` {any}
443447

444-
Throws `value` if `value` is truthy. This is useful when testing the `error`
445-
argument in callbacks.
448+
Throws `value` if `value` is not `undefined` or `null`. This is useful when
449+
testing the `error` argument in callbacks.
446450

447451
```js
448452
const assert = require('assert').strict;
449453

450454
assert.ifError(null);
451455
// OK
452456
assert.ifError(0);
453-
// OK
454-
assert.ifError(1);
455-
// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 1
457+
// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 0
456458
assert.ifError('error');
457459
// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 'error'
458460
assert.ifError(new Error());

lib/assert.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ assert.doesNotThrow = function doesNotThrow(block, error, message) {
445445
};
446446

447447
assert.ifError = function ifError(err) {
448-
if (err) {
448+
if (err !== null && err !== undefined) {
449449
let message = 'ifError got unwanted exception: ';
450450
if (typeof err === 'object' && typeof err.message === 'string') {
451451
if (err.message.length === 0 && err.constructor) {

test/parallel/test-assert-if-error.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,16 @@ assert.throws(
6060
}
6161
);
6262

63+
assert.throws(
64+
() => { assert.ifError(false); },
65+
{
66+
message: 'ifError got unwanted exception: false'
67+
}
68+
);
69+
6370
assert.doesNotThrow(() => { assert.ifError(null); });
6471
assert.doesNotThrow(() => { assert.ifError(); });
6572
assert.doesNotThrow(() => { assert.ifError(undefined); });
66-
assert.doesNotThrow(() => { assert.ifError(false); });
6773

6874
// https://github.com/nodejs/node-v0.x-archive/issues/2893
6975
{

test/sequential/test-inspector-port-zero.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ function test(arg, port = '') {
1616
let stderr = '';
1717
proc.stdout.on('data', (data) => stdout += data);
1818
proc.stderr.on('data', (data) => stderr += data);
19-
proc.stdout.on('close', assert.ifError);
20-
proc.stderr.on('close', assert.ifError);
19+
proc.stdout.on('close', (hadErr) => assert(!hadErr));
20+
proc.stderr.on('close', (hadErr) => assert(!hadErr));
2121
proc.stderr.on('data', () => {
2222
if (!stderr.includes('\n')) return;
2323
assert(/Debugger listening on (.+)/.test(stderr));

0 commit comments

Comments
 (0)