Skip to content

Commit

Permalink
doc: update assert's validation functions
Browse files Browse the repository at this point in the history
Using `assert.throws()` or `assert.rejects()` in combination with
validation function can be very powerful. Using them by returning
any value besides `true` makes debugging very difficult though
because there's no context or reason why the error validation failed.
Thus, it is recommended to throw an error in such cases instead of
returning anything besides `true`.
  • Loading branch information
BridgeAR committed Jun 5, 2019
1 parent e36fd00 commit 6dd4ba5
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions doc/api/assert.md
Original file line number Diff line number Diff line change
Expand Up @@ -1210,10 +1210,14 @@ assert.throws(
() => {
throw new Error('Wrong value');
},
function(err) {
if ((err instanceof Error) && /value/.test(err)) {
return true;
}
(err) => {
assert(err instanceof Error);
assert(/value/.test(err));
// Returning anything from validation functions besides `true` is not
// recommended. Doing so results in the caught error being thrown again.
// That is mostly not the desired outcome. Throw an error about the specific
// validation that failed instead (as done in this example).
return true;
},
'unexpected error'
);
Expand Down

0 comments on commit 6dd4ba5

Please sign in to comment.