Skip to content
Permalink
Browse files

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: #15025
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
  • Loading branch information...
BridgeAR authored and jasnell committed Aug 25, 2017
1 parent 93c08b0 commit 4f7d9392e7f9405999533c21113aeebe433ecd94
Showing with 23 additions and 9 deletions.
  1. +14 −9 lib/internal/errors.js
  2. +9 −0 test/parallel/test-assert.js
@@ -35,21 +35,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'$/
}
);

0 comments on commit 4f7d939

Please sign in to comment.
You can’t perform that action at this time.