Bug 878319: Move makeInfallible into its own JSM. r=dcamp
☠☠ backed out by fc1972bf1410 ☠ ☠
authorJim Blandy <jimb@mozilla.com>
Sat, 01 Jun 2013 13:31:07 -0700
changeset 133714 b4913818b95e5c52e95f52390c6c46be661bea04
parent 133713 b2d707e007045e5016e9835aafa46dbb238acbd3
child 133715 fc1972bf14104cbd000ef24f6dceb9fa1149cc0b
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersdcamp
bugs878319
milestone24.0a1
Bug 878319: Move makeInfallible into its own JSM. r=dcamp
toolkit/devtools/DevToolsUtils.jsm
toolkit/devtools/server/tests/unit/test_dbgsocket.js
toolkit/devtools/server/transport.js
new file mode 100644
--- /dev/null
+++ b/toolkit/devtools/DevToolsUtils.jsm
@@ -0,0 +1,80 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+/* General utilities used throughout devtools. */
+
+this.EXPORTED_SYMBOLS = [ "DevToolsUtils" ];
+
+var Cu = Components.utils;
+
+/* Turn the error e into a string, without fail. */
+function safeErrorString(aError) {
+  try {
+    var s = aError.toString();
+    if (typeof s === "string")
+      return s;
+  } catch (ee) { }
+
+  return "<failed trying to find error description>";
+}
+
+/**
+ * Report that |aWho| threw an exception, |aException|.
+ */
+function reportException(aWho, aException) {
+  let msg = aWho + " threw an exception: " + safeErrorString(aException);
+  if (aException.stack) {
+    msg += "\nCall stack:\n" + aException.stack;
+  }
+
+  dump(msg + "\n");
+
+  if (Cu.reportError) {
+    /*
+     * Note that the xpcshell test harness registers an observer for
+     * console messages, so when we're running tests, this will cause
+     * the test to quit.
+     */
+    Cu.reportError(msg);
+  }
+}
+
+/**
+ * Given a handler function that may throw, return an infallible handler
+ * function that calls the fallible handler, and logs any exceptions it
+ * throws.
+ *
+ * @param aHandler function
+ *      A handler function, which may throw.
+ * @param aName string
+ *      A name for aHandler, for use in error messages. If omitted, we use
+ *      aHandler.name.
+ *
+ * (SpiderMonkey does generate good names for anonymous functions, but we
+ * don't have a way to get at them from JavaScript at the moment.)
+ */
+function makeInfallible(aHandler, aName) {
+  if (!aName)
+    aName = aHandler.name;
+
+  return function (/* arguments */) {
+    try {
+      return aHandler.apply(this, arguments);
+    } catch (ex) {
+      let who = "Handler function";
+      if (aName) {
+        who += " " + aName;
+      }
+      reportException(who, ex);
+    }
+  }
+}
+
+this.DevToolsUtils = {
+  safeErrorString: safeErrorString,
+  reportException: reportException,
+  makeInfallible: makeInfallible
+};
--- a/toolkit/devtools/server/tests/unit/test_dbgsocket.js
+++ b/toolkit/devtools/server/tests/unit/test_dbgsocket.js
@@ -1,13 +1,16 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 Cu.import("resource://gre/modules/devtools/dbg-server.jsm");
 Cu.import("resource://gre/modules/devtools/dbg-client.jsm");
+Cu.import("resource://gre/modules/devtools/DevToolsUtils.jsm");
+
+var { safeErrorString } = DevToolsUtils;
 
 let port = 2929;
 
 function run_test()
 {
   do_print("Starting test at " + new Date().toTimeString());
   initTestDebuggerServer();
 
--- a/toolkit/devtools/server/transport.js
+++ b/toolkit/devtools/server/transport.js
@@ -2,71 +2,18 @@
 /* vim: set ft=javascript ts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 "use strict";
 Components.utils.import("resource://gre/modules/NetUtil.jsm");
 
-/* Turn the error e into a string, without fail. */
-function safeErrorString(aError) {
-  try {
-    var s = aError.toString();
-    if (typeof s === "string")
-      return s;
-  } catch (ee) { }
-
-  return "<failed trying to find error description>";
-}
-
-/**
- * Given a handler function that may throw, return an infallible handler
- * function that calls the fallible handler, and logs any exceptions it
- * throws.
- *
- * @param aHandler function
- *      A handler function, which may throw.
- * @param aName string
- *      A name for aHandler, for use in error messages. If omitted, we use
- *      aHandler.name.
- *
- * (SpiderMonkey does generate good names for anonymous functions, but we
- * don't have a way to get at them from JavaScript at the moment.)
- */
-function makeInfallible(aHandler, aName) {
-  if (!aName)
-    aName = aHandler.name;
-
-  return function (/* arguments */) {
-    try {
-      return aHandler.apply(this, arguments);
-    } catch (ex) {
-      let msg = "Handler function ";
-      if (aName) {
-        msg += aName + " ";
-      }
-      msg += "threw an exception: " + safeErrorString(ex);
-      if (ex.stack) {
-        msg += "\nCall stack:\n" + ex.stack;
-      }
-
-      dump(msg + "\n");
-
-      if (Cu.reportError) {
-        /*
-         * Note that the xpcshell test harness registers an observer for
-         * console messages, so when we're running tests, this will cause
-         * the test to quit.
-         */
-        Cu.reportError(msg);
-      }
-    }
-  }
-}
+Components.utils.import("resource://gre/modules/devtools/DevToolsUtils.jsm");
+var { makeInfallible } = DevToolsUtils;
 
 /**
  * An adapter that handles data transfers between the debugger client and
  * server. It can work with both nsIPipe and nsIServerSocket transports so
  * long as the properly created input and output streams are specified.
  *
  * @param aInput nsIInputStream
  *        The input stream.