From 72bb4445c64af98f3e481c11320afbfb995f010c Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Fri, 19 Jan 2018 10:35:39 +0100 Subject: assert: wrap original error in ifError It is hard to know where ifError is actually triggered due to the original error being thrown. This changes it by wrapping the original error in a AssertionError. This has the positive effect of also making clear that it is indeed a assertion function that triggered that error. The original stack can still be accessed by checking the `actual` property. PR-URL: https://github.com/nodejs/node/pull/18247 Reviewed-By: Matteo Collina Reviewed-By: James M Snell --- test/message/if-error-has-good-stack.js | 22 ++++++++++++++++++++++ test/message/if-error-has-good-stack.out | 19 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 test/message/if-error-has-good-stack.js create mode 100644 test/message/if-error-has-good-stack.out (limited to 'test/message') diff --git a/test/message/if-error-has-good-stack.js b/test/message/if-error-has-good-stack.js new file mode 100644 index 0000000000..1db25d2fa5 --- /dev/null +++ b/test/message/if-error-has-good-stack.js @@ -0,0 +1,22 @@ +'use strict'; + +require('../common'); +const assert = require('assert'); + +let err; +// Create some random error frames. +(function a() { + (function b() { + (function c() { + err = new Error('test error'); + })(); + })(); +})(); + +(function x() { + (function y() { + (function z() { + assert.ifError(err); + })(); + })(); +})(); diff --git a/test/message/if-error-has-good-stack.out b/test/message/if-error-has-good-stack.out new file mode 100644 index 0000000000..fa72322b44 --- /dev/null +++ b/test/message/if-error-has-good-stack.out @@ -0,0 +1,19 @@ +assert.js:* + throw newErr; + ^ + +AssertionError [ERR_ASSERTION]: ifError got unwanted exception: test error + at z (*/if-error-has-good-stack.js:*:* + at y (*/if-error-has-good-stack.js:*:*) + at x (*/if-error-has-good-stack.js:*:*) + at Object. (*/if-error-has-good-stack.js:*:*) + at c (*/if-error-has-good-stack.js:*:*) + at b (*/if-error-has-good-stack.js:*:*) + at a (*/if-error-has-good-stack.js:*:*) + at Object. (*/if-error-has-good-stack.js:*:*) + at Module._compile (module.js:*:*) + at Object.Module._extensions..js (module.js:*:*) + at Module.load (module.js:*:*) + at tryModuleLoad (module.js:*:*) + at Function.Module._load (module.js:*:*) + at Function.Module.runMain (module.js:*:*) -- cgit v1.2.3