Created
May 31, 2013 00:39
-
-
Save jimblandy/5682302 to your computer and use it in GitHub Desktop.
Move some debugger utility functions into their own JSM.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# HG changeset patch | |
# Parent 015b63c17e3997e8594ed973c3ec87be64dfc284 | |
diff --git a/toolkit/devtools/DevToolsUtils.jsm b/toolkit/devtools/DevToolsUtils.jsm | |
new file mode 100644 | |
--- /dev/null | |
+++ b/toolkit/devtools/DevToolsUtils.jsm | |
@@ -0,0 +1,93 @@ | |
+/* 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); | |
+ } | |
+ } | |
+} | |
+ | |
+Cu.import("resource://gre/modules/commonjs/sdk/core/promise.js"); | |
+ | |
+/** | |
+ * Register a default handler for promise rejections, so that errors thrown | |
+ * by 'then' clauses never disappear into the ether. | |
+ */ | |
+Promise._unhandledError = (ex) => { | |
+ // Make sure we get a backtrace, even if (!ex instanceof Error). | |
+ if (!ex.stack) | |
+ ex = Error(ex); | |
+ reportException("promise handler", ex); | |
+} | |
+ | |
+this.DevToolsUtils = { | |
+ safeErrorString: safeErrorString, | |
+ reportException: reportException, | |
+ makeInfallible: makeInfallible | |
+}; | |
diff --git a/toolkit/devtools/server/tests/unit/test_dbgsocket.js b/toolkit/devtools/server/tests/unit/test_dbgsocket.js | |
--- a/toolkit/devtools/server/tests/unit/test_dbgsocket.js | |
+++ b/toolkit/devtools/server/tests/unit/test_dbgsocket.js | |
@@ -3,6 +3,9 @@ | |
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; | |
diff --git a/toolkit/devtools/server/transport.js b/toolkit/devtools/server/transport.js | |
--- a/toolkit/devtools/server/transport.js | |
+++ b/toolkit/devtools/server/transport.js | |
@@ -6,80 +6,8 @@ | |
"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>"; | |
-} | |
- | |
-/** | |
- * 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); | |
- } | |
- } | |
-} | |
- | |
-/** | |
- * Register a default handler for promise rejections, so that errors thrown | |
- * by 'then' clauses never disappear into the ether. | |
- */ | |
-Promise._unhandledError = (ex) => { | |
- // Make sure we get a backtrace, even if (!ex instanceof Error). | |
- if (!ex.stack) | |
- ex = Error(ex); | |
- reportException("promise handler", ex); | |
-} | |
+Components.utils.import("resource://gre/modules/devtools/DevToolsUtils.jsm"); | |
+var { makeInfallible } = DevToolsUtils; | |
/** | |
* An adapter that handles data transfers between the debugger client and |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment