Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
doc: update assert's validation functions
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
commit 6dd4ba5558d81b00f431d55dd5c3dfc1d92a0dae
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
Comment thread
BridgeAR marked this conversation as resolved.
Resolved
// validation that failed instead (as done in this example).
return true;
},
'unexpected error'
);
Expand Down