Made exception logging routines more informative and added logtests for them.
authorAtul Varma <varmaa@toolness.com>
Wed, 25 Jun 2008 00:13:36 -0700
changeset 44738 13571441ff63672cef23d1608c1ef7f7fcc17fef
parent 44737 16328ae5765cd45fb7ab622502abc48f57bd4a9a
child 44739 bd3accb2fccae83cde40c1b902e4a2bf7d01a61a
push id1
push userroot
push dateTue, 26 Apr 2011 22:38:44 +0000
treeherdermozilla-beta@bfdb6e623a36 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
Made exception logging routines more informative and added logtests for them.
services/sync/modules/util.js
services/sync/tests/unit/test_util_tracebacks.js
services/sync/tests/unit/test_util_tracebacks.log.expected
--- a/services/sync/modules/util.js
+++ b/services/sync/modules/util.js
@@ -199,29 +199,47 @@ let Utils = {
         ret[key] = Utils.deepCopy(thing[key]);
     }
 
     return ret;
   },
 
   exceptionStr: function Weave_exceptionStr(e) {
     let message = e.message? e.message : e;
-    let location = e.location? " (" + e.location + ")" : "";
+    let location = "";
+
+    if (e.location)
+      // It's a wrapped nsIException.
+      location = e.location;
+    else if (e.fileName && e.lineNumber)
+      // It's a standard JS exception.
+      location = "file '" + e.fileName + "', line " + e.lineNumber;
+
+    if (location)
+      location = " (" + location + ")";
     return message + location;
   },
 
-  stackTrace: function Weave_stackTrace(stackFrame, str) {
-    if (stackFrame.caller)
-      str = Utils.stackTrace(stackFrame.caller, str);
+  stackTrace: function Weave_stackTrace(e) {
+    let output = "";
+    if (e.location) {
+      // It's a wrapped nsIException.
+      let frame = e.location;
+      while (frame) {
+        output += frame + "\n";
+        frame = frame.caller;
+      }
+    } else if (e.stack)
+      // It's a standard JS exception
+      output += e.stack;
+    else
+      // It's some other thrown object, e.g. a bare string.
+      output += "No traceback available.\n";
 
-    if (!str)
-      str = "";
-    str = stackFrame + "\n" + str;
-
-    return str;
+    return output;
   },
 
   checkStatus: function Weave_checkStatus(code, msg, ranges) {
     if (!ranges)
       ranges = [[200,300]];
 
     for (let i = 0; i < ranges.length; i++) {
       var rng = ranges[i];
new file mode 100644
--- /dev/null
+++ b/services/sync/tests/unit/test_util_tracebacks.js
@@ -0,0 +1,36 @@
+Cu.import("resource://weave/util.js");
+
+function _reportException(e) {
+  dump("Exception caught.\n");
+  dump("Exception: " + Utils.exceptionStr(e) + "\n");
+  dump("Traceback:\n\n" + Utils.stackTrace(e) + "\n");
+}
+
+function test_stackTrace_works_with_error_object() {
+  try {
+    dump("Throwing new Error object.\n");
+    throw new Error("Error!");
+  } catch (e) {
+    _reportException(e);
+  }
+}
+
+function test_stackTrace_works_with_bare_string() {
+  try {
+    dump("Throwing bare string.\n");
+    throw "Error!";
+  } catch (e) {
+    _reportException(e);
+  }
+}
+
+function test_stackTrace_works_with_nsIException() {
+  try {
+    dump("Throwing a wrapped nsIException.\n");
+    let dirSvc = Cc["@mozilla.org/file/directory_service;1"].
+                 getService(Ci.nsIProperties);
+    dirSvc.get("nonexistentPropertyName");
+  } catch (e) {
+    _reportException(e);
+  }
+}
new file mode 100644
--- /dev/null
+++ b/services/sync/tests/unit/test_util_tracebacks.log.expected
@@ -0,0 +1,36 @@
+*** test pending
+Running test: test_stackTrace_works_with_error_object
+Throwing new Error object.
+Exception caught.
+Exception: Error! (file 'test_util_tracebacks.js', line 12)
+Traceback:
+
+Error("Error!")@:0
+test_stackTrace_works_with_error_object()@test_util_tracebacks.js:12
+_find_and_run_tests()@../harness/head.js:106
+_execute_test(_find_and_run_tests)@../harness/head.js:144
+@../harness/tail.js:38
+
+Running test: test_stackTrace_works_with_bare_string
+Throwing bare string.
+Exception caught.
+Exception: Error!
+Traceback:
+
+No traceback available.
+
+Running test: test_stackTrace_works_with_nsIException
+Throwing a wrapped nsIException.
+Exception caught.
+Exception: Not enough arguments [nsIProperties.get] (JS frame :: test_util_tracebacks.js :: test_stackTrace_works_with_nsIException :: line 32)
+Traceback:
+
+JS frame :: test_util_tracebacks.js :: test_stackTrace_works_with_nsIException :: line 32
+JS frame :: ../harness/head.js :: _find_and_run_tests :: line 106
+JS frame :: ../harness/head.js :: _execute_test :: line 144
+JS frame :: ../harness/tail.js :: <TOP_LEVEL> :: line 38
+
+3 of 3 tests passed.
+*** test finished
+*** exiting
+*** PASS ***