Bug 1263747 - Log error messages when stringifying errors. r?bgrins
MozReview-Commit-ID: 2MB8IDKfVHo
--- a/toolkit/modules/Console.jsm
+++ b/toolkit/modules/Console.jsm
@@ -101,16 +101,30 @@ function getCtorName(aObj) {
return aObj.constructor.name;
}
// If that fails, use Objects toString which sometimes gives something
// better than 'Object', and at least defaults to Object if nothing better
return Object.prototype.toString.call(aObj).slice(8, -1);
}
/**
+ * Indicates whether an object is a JS or `Components.Exception` error.
+ *
+ * @param {object} aThing
+ The object to check
+ * @return {boolean}
+ Is this object an error?
+ */
+function isError(aThing) {
+ return (typeof aThing.name == "string" &&
+ aThing.name.startsWith("NS_ERROR_")) ||
+ getCtorName(aThing).endsWith("Error");
+}
+
+/**
* A single line stringification of an object designed for use by humans
*
* @param {any} aThing
* The object to be stringified
* @param {boolean} aAllowNewLines
* @return {string}
* A single line representation of aThing, which will generally be at
* most 80 chars long
@@ -119,16 +133,20 @@ function stringify(aThing, aAllowNewLine
if (aThing === undefined) {
return "undefined";
}
if (aThing === null) {
return "null";
}
+ if (isError(aThing)) {
+ return "Message: " + aThing;
+ }
+
if (typeof aThing == "object") {
let type = getCtorName(aThing);
if (aThing instanceof Components.interfaces.nsIDOMNode && aThing.tagName) {
return debugElement(aThing);
}
type = (type == "Object" ? "" : type + " ");
let json;
try {
@@ -198,19 +216,17 @@ function log(aThing) {
else if (type == "Set") {
let i = 0;
reply += "Set\n";
for (let value of aThing) {
reply += logProperty('' + i, value);
i++;
}
}
- else if (type.match("Error$") ||
- (typeof aThing.name == "string" &&
- aThing.name.match("NS_ERROR_"))) {
+ else if (isError(aThing)) {
reply += " Message: " + aThing + "\n";
if (aThing.stack) {
reply += " Stack:\n";
var frame = aThing.stack;
while (frame) {
reply += " " + frame + "\n";
frame = frame.caller;
}