Bug 966674: #2 Move exceptionStr() and stackTrace() from utils.js to Log.jsm; r=gps,rnewman
authorIrving Reid <irving@mozilla.com>
Tue, 29 Apr 2014 10:09:27 -0400
changeset 181229 ea70694f55c81a214f6208a79700c69f496057c6
parent 181228 3dd34ac404382ce20611435cf593f3fbca7bdb10
child 181230 b106aad655ad43d334e9d7593a57511918975f47
push id272
push userpvanderbeken@mozilla.com
push dateMon, 05 May 2014 16:31:18 +0000
reviewersgps, rnewman
bugs966674
milestone32.0a1
Bug 966674: #2 Move exceptionStr() and stackTrace() from utils.js to Log.jsm; r=gps,rnewman
services/common/utils.js
toolkit/modules/Log.jsm
--- a/services/common/utils.js
+++ b/services/common/utils.js
@@ -64,62 +64,19 @@ this.CommonUtils = {
     for (let x of a) {
       if (!b.has(x)) {
         return false;
       }
     }
     return true;
   },
 
-  exceptionStr: function exceptionStr(e) {
-    if (!e) {
-      return "" + e;
-    }
-    let message = e.message ? e.message : e;
-    return message + " " + CommonUtils.stackTrace(e);
-  },
-
-  stackTrace: function stackTrace(e) {
-    // Wrapped nsIException
-    if (e.location) {
-      let frame = e.location;
-      let output = [];
-      while (frame) {
-        // Works on frames or exceptions, munges file:// URIs to shorten the paths
-        // FIXME: filename munging is sort of hackish, might be confusing if
-        // there are multiple extensions with similar filenames
-        let str = "<file:unknown>";
-
-        let file = frame.filename || frame.fileName;
-        if (file){
-          str = file.replace(/^(?:chrome|file):.*?([^\/\.]+\.\w+)$/, "$1");
-        }
-
-        if (frame.lineNumber){
-          str += ":" + frame.lineNumber;
-        }
-        if (frame.name){
-          str = frame.name + "()@" + str;
-        }
-
-        if (str){
-          output.push(str);
-        }
-        frame = frame.caller;
-      }
-      return "Stack trace: " + output.join(" < ");
-    }
-    // Standard JS exception
-    if (e.stack){
-      return "JS Stack trace: " + e.stack.trim().replace(/\n/g, " < ").
-        replace(/@[^@]*?([^\/\.]+\.\w+:)/g, "@$1");
-    }
-
-    return "No traceback available";
-  },
+  // Import these from Log.jsm for backward compatibility
+  exceptionStr: Log.exceptionStr,
+  stackTrace: Log.stackTrace,
 
   /**
    * Encode byte string as base64URL (RFC 4648).
    *
    * @param bytes
    *        (string) Raw byte string to encode.
    * @param pad
    *        (bool) Whether to include padding characters (=). Defaults
--- a/toolkit/modules/Log.jsm
+++ b/toolkit/modules/Log.jsm
@@ -112,20 +112,91 @@ this.Log = {
         properties.push(p + " = " + aObject[p]);
       }
       catch(ex) {
         properties.push(p + " = " + ex);
       }
     }
 
     return properties;
+  },
+
+  _formatError: function _formatError(e) {
+    let result = e.toString();
+    if (e.fileName) {
+      result +=  " (" + e.fileName;
+      if (e.lineNumber) {
+        result += ":" + e.lineNumber;
+      }
+      if (e.columnNumber) {
+        result += ":" + e.columnNumber;
+      }
+      result += ")";
+    }
+    return result + " " + Log.stackTrace(e);
+  },
+
+  // This is for back compatibility with toolkit/services/utils.js; we duplicate
+  // some of the logic in ParameterFormatter
+  exceptionStr: function exceptionStr(e) {
+    if (!e) {
+      return "" + e;
+    }
+    if (e instanceof Ci.nsIException) {
+      return e.toString() + " " + Log.stackTrace(e);
+    }
+    else if (isError(e)) {
+      return Log._formatError(e);
+    }
+    // else
+    let message = e.message ? e.message : e;
+    return message + " " + Log.stackTrace(e);
+  },
+
+  stackTrace: function stackTrace(e) {
+    // Wrapped nsIException
+    if (e.location) {
+      let frame = e.location;
+      let output = [];
+      while (frame) {
+        // Works on frames or exceptions, munges file:// URIs to shorten the paths
+        // FIXME: filename munging is sort of hackish, might be confusing if
+        // there are multiple extensions with similar filenames
+        let str = "<file:unknown>";
+
+        let file = frame.filename || frame.fileName;
+        if (file) {
+          str = file.replace(/^(?:chrome|file):.*?([^\/\.]+\.\w+)$/, "$1");
+        }
+
+        if (frame.lineNumber) {
+          str += ":" + frame.lineNumber;
+        }
+
+        if (frame.name) {
+          str = frame.name + "()@" + str;
+        }
+
+        if (str) {
+          output.push(str);
+        }
+        frame = frame.caller;
+      }
+      return "Stack trace: " + output.join(" < ");
+    }
+    // Standard JS exception
+    if (e.stack) {
+      return "JS Stack trace: " + e.stack.trim().replace(/\n/g, " < ").
+        replace(/@[^@]*?([^\/\.]+\.\w+:)/g, "@$1");
+    }
+
+    return "No traceback available";
   }
 };
 
-
 /*
  * LogMessage
  * Encapsulates a single log event's data
  */
 function LogMessage(loggerName, level, message, params) {
   this.loggerName = loggerName;
   this.level = level;
   this.message = message;
@@ -138,17 +209,17 @@ function LogMessage(loggerName, level, m
 }
 LogMessage.prototype = {
   get levelDesc() {
     if (this.level in Log.Level.Desc)
       return Log.Level.Desc[this.level];
     return "UNKNOWN";
   },
 
-  toString: function LogMsg_toString(){
+  toString: function LogMsg_toString() {
     let msg = "LogMessage [" + this.time + " " + this.level + " " +
       this.message;
     if (this.params) {
       msg += " " + JSON.stringify(this.params);
     }
     return msg + "]"
   }
 };