Skip to content

Commit fe3d8f2

Browse files
mikealry
authored andcommitted
Add assert.ifError
1 parent 2b34363 commit fe3d8f2

3 files changed

Lines changed: 7 additions & 0 deletions

File tree

doc/api.markdown

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2546,6 +2546,9 @@ Expects `block` to throw an error.
25462546

25472547
Expects `block` not to throw an error.
25482548

2549+
### assert.ifError(value)
2550+
2551+
Tests if value is not a false value, throws if it is a true value. Useful when testing the first argument, `error` in callbacks.
25492552

25502553
## Path
25512554

lib/assert.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,4 @@ assert.doesNotThrow = function(block, /*optional*/error, /*optional*/message) {
284284
_throws.apply(this, [false].concat(pSlice.call(arguments)));
285285
};
286286

287+
assert.ifError = function (err) { if (err) {throw err;}};

test/simple/test-assert.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,6 @@ try {
154154
}
155155
assert.equal(true,threw,'a.doesNotThrow is not catching type matching errors');
156156

157+
assert.throws(function () {assert.ifError(new Error('test error'))});
158+
assert.doesNotThrow(function(){assert.ifError(null)});
159+
assert.doesNotThrow(function(){assert.ifError()});

0 commit comments

Comments
 (0)