Bug 1263747 - Log error messages when stringifying errors. r?bgrins draft
authorKit Cambridge <kcambridge@mozilla.com>
Mon, 11 Apr 2016 14:51:33 -0700
changeset 349542 015cfc8d956658bc001618a69771f1244d001fd2
parent 348506 b6683e141c47c022598c0caac3ea8ba8c6236d42
child 518133 4ead22ecabcdaf58bd24d750e56d9b59e4667e80
push id15122
push userkcambridge@mozilla.com
push dateMon, 11 Apr 2016 21:59:15 +0000
reviewersbgrins
bugs1263747
milestone48.0a1
Bug 1263747 - Log error messages when stringifying errors. r?bgrins MozReview-Commit-ID: 2MB8IDKfVHo
toolkit/modules/Console.jsm
--- 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;
         }