mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 13:48:44 +02:00
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 <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
7a23fc0760
commit
72bb4445c6
9 changed files with 187 additions and 32 deletions
|
@ -452,7 +452,53 @@ assert.doesNotThrow = function doesNotThrow(block, error, message) {
|
|||
throw actual;
|
||||
};
|
||||
|
||||
assert.ifError = function ifError(err) { if (err) throw err; };
|
||||
assert.ifError = function ifError(err) {
|
||||
if (err) {
|
||||
let message = 'ifError got unwanted exception: ';
|
||||
if (typeof err === 'object' && typeof err.message === 'string') {
|
||||
if (err.message.length === 0 && err.constructor) {
|
||||
message += err.constructor.name;
|
||||
} else {
|
||||
message += err.message;
|
||||
}
|
||||
} else {
|
||||
message += inspect(err);
|
||||
}
|
||||
|
||||
const newErr = new assert.AssertionError({
|
||||
actual: err,
|
||||
expected: null,
|
||||
operator: 'ifError',
|
||||
message,
|
||||
stackStartFn: ifError
|
||||
});
|
||||
|
||||
// Make sure we actually have a stack trace!
|
||||
const origStack = err.stack;
|
||||
|
||||
if (typeof origStack === 'string') {
|
||||
// This will remove any duplicated frames from the error frames taken
|
||||
// from within `ifError` and add the original error frames to the newly
|
||||
// created ones.
|
||||
const tmp2 = origStack.split('\n');
|
||||
tmp2.shift();
|
||||
// Filter all frames existing in err.stack.
|
||||
let tmp1 = newErr.stack.split('\n');
|
||||
for (var i = 0; i < tmp2.length; i++) {
|
||||
// Find the first occurrence of the frame.
|
||||
const pos = tmp1.indexOf(tmp2[i]);
|
||||
if (pos !== -1) {
|
||||
// Only keep new frames.
|
||||
tmp1 = tmp1.slice(0, pos);
|
||||
break;
|
||||
}
|
||||
}
|
||||
newErr.stack = `${tmp1.join('\n')}\n${tmp2.join('\n')}`;
|
||||
}
|
||||
|
||||
throw newErr;
|
||||
}
|
||||
};
|
||||
|
||||
// Expose a strict only variant of assert
|
||||
function strict(...args) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue