assert: improve AssertionError in case of "Errors"
Showing the stack trace in a error message obfuscates the actual message and should not be visible therefore. PR-URL: https://github.com/nodejs/node/pull/15025 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
This commit is contained in:
parent
dcb24e3be5
commit
2e8217c64e
@ -36,21 +36,26 @@ class AssertionError extends Error {
|
||||
if (typeof options !== 'object' || options === null) {
|
||||
throw new exports.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'object');
|
||||
}
|
||||
if (options.message) {
|
||||
super(options.message);
|
||||
var { actual, expected, message, operator, stackStartFunction } = options;
|
||||
if (message) {
|
||||
super(message);
|
||||
} else {
|
||||
if (actual && actual.stack && actual instanceof Error)
|
||||
actual = `${actual.name}: ${actual.message}`;
|
||||
if (expected && expected.stack && expected instanceof Error)
|
||||
expected = `${expected.name}: ${expected.message}`;
|
||||
if (util === null) util = require('util');
|
||||
super(`${util.inspect(options.actual).slice(0, 128)} ` +
|
||||
`${options.operator} ${util.inspect(options.expected).slice(0, 128)}`);
|
||||
super(`${util.inspect(actual).slice(0, 128)} ` +
|
||||
`${operator} ${util.inspect(expected).slice(0, 128)}`);
|
||||
}
|
||||
|
||||
this.generatedMessage = !options.message;
|
||||
this.generatedMessage = !message;
|
||||
this.name = 'AssertionError [ERR_ASSERTION]';
|
||||
this.code = 'ERR_ASSERTION';
|
||||
this.actual = options.actual;
|
||||
this.expected = options.expected;
|
||||
this.operator = options.operator;
|
||||
Error.captureStackTrace(this, options.stackStartFunction);
|
||||
this.actual = actual;
|
||||
this.expected = expected;
|
||||
this.operator = operator;
|
||||
Error.captureStackTrace(this, stackStartFunction);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -743,3 +743,12 @@ assert.throws(() => {
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
common.expectsError(
|
||||
() => assert.strictEqual(new Error('foo'), new Error('foobar')),
|
||||
{
|
||||
code: 'ERR_ASSERTION',
|
||||
type: assert.AssertionError,
|
||||
message: /^'Error: foo' === 'Error: foobar'$/
|
||||
}
|
||||
);
|
||||
|
Loading…
x
Reference in New Issue
Block a user