forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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: nodejs#18247 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
- Loading branch information
Showing
9 changed files
with
187 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
})(); | ||
})(); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.<anonymous> (*/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.<anonymous> (*/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:*:*) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
const assert = require('assert').strict; | ||
/* eslint-disable no-restricted-properties */ | ||
|
||
// Test that assert.ifError has the correct stack trace of both stacks. | ||
|
||
let err; | ||
// Create some random error frames. | ||
(function a() { | ||
(function b() { | ||
(function c() { | ||
err = new Error('test error'); | ||
})(); | ||
})(); | ||
})(); | ||
|
||
const msg = err.message; | ||
const stack = err.stack; | ||
|
||
(function x() { | ||
(function y() { | ||
(function z() { | ||
let threw = false; | ||
try { | ||
assert.ifError(err); | ||
} catch (e) { | ||
assert.equal(e.message, 'ifError got unwanted exception: test error'); | ||
assert.equal(err.message, msg); | ||
assert.equal(e.actual, err); | ||
assert.equal(e.actual.stack, stack); | ||
assert.equal(e.expected, null); | ||
assert.equal(e.operator, 'ifError'); | ||
threw = true; | ||
} | ||
assert(threw); | ||
})(); | ||
})(); | ||
})(); | ||
|
||
assert.throws( | ||
() => assert.ifError(new TypeError()), | ||
{ | ||
message: 'ifError got unwanted exception: TypeError' | ||
} | ||
); | ||
|
||
assert.throws( | ||
() => assert.ifError({ stack: false }), | ||
{ | ||
message: 'ifError got unwanted exception: { stack: false }' | ||
} | ||
); | ||
|
||
assert.throws( | ||
() => assert.ifError({ constructor: null, message: '' }), | ||
{ | ||
message: 'ifError got unwanted exception: ' | ||
} | ||
); | ||
|
||
assert.doesNotThrow(() => { assert.ifError(null); }); | ||
assert.doesNotThrow(() => { assert.ifError(); }); | ||
assert.doesNotThrow(() => { assert.ifError(undefined); }); | ||
assert.doesNotThrow(() => { assert.ifError(false); }); | ||
|
||
// https://github.com/nodejs/node-v0.x-archive/issues/2893 | ||
{ | ||
let threw = false; | ||
try { | ||
// eslint-disable-next-line no-restricted-syntax | ||
assert.throws(() => { | ||
assert.ifError(null); | ||
}); | ||
} catch (e) { | ||
threw = true; | ||
assert.strictEqual(e.message, 'Missing expected exception.'); | ||
assert(!e.stack.includes('throws'), e.stack); | ||
} | ||
assert(threw); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters