mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 21:58:48 +02:00
assert: use util.inspect() to create error messages
Currently, JSON.stringify() is used to create error messages on failed assertions. This causes an error when stringifying objects with circular references. This commit switches out JSON.stringify() for util.inspect(), which can handle circular references. PR-URL: https://github.com/iojs/io.js/pull/668 Reviewed-By: Julien Gilli <julien.gilli@joyent.com> Reviewed-By: Bert Belder <bertbelder@gmail.com> Reviewed-By: Chris Dickinson <christopher.s.dickinson@gmail.com>
This commit is contained in:
parent
bc2c85ceef
commit
40e29dcbbf
2 changed files with 20 additions and 28 deletions
|
@ -58,19 +58,6 @@ assert.AssertionError = function AssertionError(options) {
|
|||
// assert.AssertionError instanceof Error
|
||||
util.inherits(assert.AssertionError, Error);
|
||||
|
||||
function replacer(key, value) {
|
||||
if (util.isUndefined(value)) {
|
||||
return '' + value;
|
||||
}
|
||||
if (util.isNumber(value) && !isFinite(value)) {
|
||||
return value.toString();
|
||||
}
|
||||
if (util.isFunction(value) || util.isRegExp(value)) {
|
||||
return value.toString();
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function truncate(s, n) {
|
||||
if (util.isString(s)) {
|
||||
return s.length < n ? s : s.slice(0, n);
|
||||
|
@ -80,9 +67,9 @@ function truncate(s, n) {
|
|||
}
|
||||
|
||||
function getMessage(self) {
|
||||
return truncate(JSON.stringify(self.actual, replacer), 128) + ' ' +
|
||||
return truncate(util.inspect(self.actual), 128) + ' ' +
|
||||
self.operator + ' ' +
|
||||
truncate(JSON.stringify(self.expected, replacer), 128);
|
||||
truncate(util.inspect(self.expected), 128);
|
||||
}
|
||||
|
||||
// At present only the three keys mentioned above are used and
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue