Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
util: Fix inspection for Error
Browse files Browse the repository at this point in the history
Fixes #1634.
  • Loading branch information
koichik committed Sep 11, 2011
1 parent df480e0 commit 389e2a0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
28 changes: 26 additions & 2 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ function formatValue(ctx, value, recurseTimes) {
if (isDate(value)) {
return ctx.stylize(value.toUTCString(), 'date');
}
if (isError(value)) {
return formatError(value);
}
}

var base = '', array = false, braces = ['{', '}'];
Expand All @@ -211,6 +214,11 @@ function formatValue(ctx, value, recurseTimes) {
base = ' ' + value.toUTCString();
}

// Make error with message first say the error
if (isError(value)) {
base = ' ' + formatError(value);
}

if (keys.length === 0 && (!array || value.length == 0)) {
return braces[0] + base + braces[1];
}
Expand Down Expand Up @@ -264,6 +272,11 @@ function formatPrimitive(ctx, value) {
}


function formatError(value) {
return '[' + Error.prototype.toString.call(value) + ']';
}


function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
var output = [];
for (var i = 0, l = value.length; i < l; ++i) {
Expand Down Expand Up @@ -374,13 +387,24 @@ function isArray(ar) {

function isRegExp(re) {
return re instanceof RegExp ||
(typeof re === 'object' && Object.prototype.toString.call(re) === '[object RegExp]');
(typeof re === 'object' && objectToString(re) === '[object RegExp]');
}


function isDate(d) {
return d instanceof Date ||
(typeof d === 'object' && Object.prototype.toString.call(d) === '[object Date]');
(typeof d === 'object' && objectToString(d) === '[object Date]');
}


function isError(e) {
return e instanceof Error ||
(typeof e === 'object' && objectToString(e) === '[object Error]');
}


function objectToString(o) {
return Object.prototype.toString.call(o);
}


Expand Down
18 changes: 18 additions & 0 deletions test/simple/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,21 @@ delete a[1];
assert.equal(util.inspect(a), "[ 'foo', , 'baz' ]");
assert.equal(util.inspect(a, true), "[ 'foo', , 'baz', [length]: 3 ]");
assert.equal(util.inspect(new Array(5)), '[ , , , , ]');

// exceptions should print the error message, not "{}"
assert.equal(util.inspect(new Error()), '[Error]');
assert.equal(util.inspect(new Error('FAIL')), '[Error: FAIL]');
assert.equal(util.inspect(new TypeError('FAIL')), '[TypeError: FAIL]');
assert.equal(util.inspect(new SyntaxError('FAIL')), '[SyntaxError: FAIL]');
try {
undef();
} catch (e) {
assert.equal(util.inspect(e), '[ReferenceError: undef is not defined]');
}
var ex = util.inspect(new Error('FAIL'), true);
console.log(ex);
assert.ok(ex.indexOf("[Error: FAIL]") != -1);
assert.ok(ex.indexOf("[stack]") != -1);
assert.ok(ex.indexOf("[message]") != -1);
assert.ok(ex.indexOf("[arguments]") != -1);
assert.ok(ex.indexOf("[type]") != -1);

0 comments on commit 389e2a0

Please sign in to comment.