assert: support custom errors

This commit adds special handling of Error instances when passed
as the message argument to assert functions. With this commit,
if an Error is passed as the message, then that Error is thrown
instead of an AssertionError.

PR-URL: https://github.com/nodejs/node/pull/15304
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
geek 2017-09-09 20:36:47 -05:00 committed by cjihrig
parent a10856a7d3
commit e13d1df89b
No known key found for this signature in database
GPG key ID: 7434390BDBE9B9C5
4 changed files with 93 additions and 18 deletions

View file

@ -38,6 +38,8 @@ const assert = module.exports = ok;
// display purposes.
function innerFail(actual, expected, message, operator, stackStartFunction) {
if (message instanceof Error) throw message;
throw new errors.AssertionError({
message,
actual,