diff options
author | Ruben Bridgewater <ruben@bridgewater.de> | 2019-06-14 17:14:50 +0300 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2019-10-01 23:55:11 +0300 |
commit | d47b6786c91908e2601400f4e3e381143049beed (patch) | |
tree | 4cb0b194b48565ba0f0ea754c99cfdb81eaef61e /lib | |
parent | 0b3242c3ce0ff2cdf9c740f45f3755a683073c32 (diff) |
assert: wrap validation function errors
This makes sure that validation function used by `assert.throws` and
`assert.rejects` always throw validatin errors instead of rethrowing
the received error.
That should improve the debugging experience for developers since
they have a better context where the error is coming from and they
also get to know what triggered it.
PR-URL: https://github.com/nodejs/node/pull/28263
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/assert.js | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/lib/assert.js b/lib/assert.js index 1beb33265d6..3d54dcd63fa 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -653,7 +653,21 @@ function expectedException(actual, expected, message, fn) { // Check validation functions return value. const res = expected.call({}, actual); if (res !== true) { - throw actual; + if (!message) { + generatedMessage = true; + const name = expected.name ? `"${expected.name}" ` : ''; + message = `The ${name}validation function is expected to return "true".` + + ` Received ${inspect(res)}`; + } + const err = new AssertionError({ + actual, + expected, + message, + operator: fn.name, + stackStartFn: fn + }); + err.generatedMessage = generatedMessage; + throw err; } } |