Bug 956804 - Use DevToolsUtils.reportException in Parser.jsm; r=fitzgen
authorAlexey Novak <alexey.novak.mail@gmail.com>
Thu, 23 Jan 2014 09:55:16 -0800
changeset 180944 67d36c90a7ea74533ed7e029ebb41ce030edaed2
parent 180943 a9913c62d98a74bcf097c6a6780bf75cdeb8187e
child 180945 066c526104ef75518dd40e9d4c976b45c7c2269a
child 180975 e61a79610e6894b0f9b4515f9a2b6bea4766f756
push id3343
push userffxbld
push dateMon, 17 Mar 2014 21:55:32 +0000
treeherdermozilla-beta@2f7d3415f79f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfitzgen
bugs956804
milestone29.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 956804 - Use DevToolsUtils.reportException in Parser.jsm; r=fitzgen
browser/devtools/shared/Parser.jsm
toolkit/devtools/DevToolsUtils.js
toolkit/devtools/tests/unit/test_safeErrorString.js
--- a/browser/devtools/shared/Parser.jsm
+++ b/browser/devtools/shared/Parser.jsm
@@ -4,16 +4,17 @@
  * 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";
 
 const Ci = Components.interfaces;
 const Cu = Components.utils;
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+const { DevToolsUtils } = Cu.import("resource://gre/modules/devtools/DevToolsUtils.jsm", {});
 
 XPCOMUtils.defineLazyModuleGetter(this,
   "Reflect", "resource://gre/modules/reflect.jsm");
 
 this.EXPORTED_SYMBOLS = ["Parser", "ParserHelpers", "SyntaxTreeVisitor"];
 
 /**
  * A JS parser using the reflection API.
@@ -60,31 +61,31 @@ Parser.prototype = {
     if (!scriptMatches.length) {
       // Reflect.parse throws when encounters a syntax error.
       try {
         let nodes = Reflect.parse(aSource);
         let length = aSource.length;
         syntaxTrees.push(new SyntaxTree(nodes, aUrl, length));
       } catch (e) {
         this.errors.push(e);
-        log(aUrl, e);
+        DevToolsUtils.reportException(aUrl, e);
       }
     }
     // Generate the AST nodes for each script.
     else {
       for (let script of scriptMatches) {
         // Reflect.parse throws when encounters a syntax error.
         try {
           let nodes = Reflect.parse(script);
           let offset = aSource.indexOf(script);
           let length = script.length;
           syntaxTrees.push(new SyntaxTree(nodes, aUrl, length, offset));
         } catch (e) {
           this.errors.push(e);
-          log(aUrl, e);
+          DevToolsUtils.reportException(aUrl, e);
         }
       }
     }
 
     let pool = new SyntaxTreesPool(syntaxTrees);
 
     // Cache the syntax trees pool by the specified url. This is entirely
     // optional, but it's strongly encouraged to cache ASTs because
@@ -220,17 +221,17 @@ SyntaxTreesPool.prototype = {
           scriptLength: syntaxTree.length,
           scriptOffset: syntaxTree.offset,
           parseResults: syntaxTree[aFunction].apply(syntaxTree, aParams)
         });
       } catch (e) {
         // Can't guarantee that the tree traversal logic is forever perfect :)
         // Language features may be added, in which case the recursive methods
         // need to be updated. If an exception is thrown here, file a bug.
-        log("syntax tree", e);
+        DevToolsUtils.reportException("syntax tree", e);
       }
     }
     this._cache.set(requestId, results);
     return results;
   },
 
   _trees: null,
   _cache: null
@@ -2336,31 +2337,9 @@ let SyntaxTreeVisitor = {
       }
     }
     if (aCallbacks.onLiteral) {
       aCallbacks.onLiteral(aNode);
     }
   }
 };
 
-/**
- * Logs a warning.
- *
- * @param string aStr
- *        The message to be displayed.
- * @param Exception aEx
- *        The thrown exception.
- */
-function log(aStr, aEx) {
-  let msg = "Warning: " + aStr + ", " + aEx.message;
-
-  if ("lineNumber" in aEx && "columnNumber" in aEx) {
-    msg += ", line: " + aEx.lineNumber + ", column: " + aEx.columnNumber;
-  }
-  if ("stack" in aEx) {
-    msg += "\n" + aEx.stack;
-  }
-
-  Cu.reportError(msg);
-  dump(msg + "\n");
-};
-
 XPCOMUtils.defineLazyGetter(Parser, "reflectionAPI", () => Reflect);
--- a/toolkit/devtools/DevToolsUtils.js
+++ b/toolkit/devtools/DevToolsUtils.js
@@ -11,28 +11,32 @@ let { Promise: promise } = Components.ut
 let { Services } = Components.utils.import("resource://gre/modules/Services.jsm", {});
 
 /**
  * Turn the error |aError| into a string, without fail.
  */
 this.safeErrorString = function safeErrorString(aError) {
   try {
     let errorString = aError.toString();
-    if (typeof errorString === "string") {
+    if (typeof errorString == "string") {
       // Attempt to attach a stack to |errorString|. If it throws an error, or
       // isn't a string, don't use it.
       try {
         if (aError.stack) {
           let stack = aError.stack.toString();
-          if (typeof stack === "string") {
+          if (typeof stack == "string") {
             errorString += "\nStack: " + stack;
           }
         }
       } catch (ee) { }
 
+      if (typeof aError.lineNumber == "number" && typeof aError.columnNumber == "number") {
+        errorString += ", line: " + aError.lineNumber + ", column: " + aError.columnNumber;
+      }
+
       return errorString;
     }
   } catch (ee) { }
 
   return "<failed trying to find error description>";
 }
 
 /**
--- a/toolkit/devtools/tests/unit/test_safeErrorString.js
+++ b/toolkit/devtools/tests/unit/test_safeErrorString.js
@@ -14,16 +14,19 @@ function run_test() {
 
 function test_with_error() {
   let s = DevToolsUtils.safeErrorString(new Error("foo bar"));
   // Got the message.
   do_check_true(s.contains("foo bar"));
   // Got the stack.
   do_check_true(s.contains("test_with_error"))
   do_check_true(s.contains("test_safeErrorString.js"));
+  // Got the lineNumber and columnNumber.
+  do_check_true(s.contains("line"));
+  do_check_true(s.contains("column"));
 }
 
 function test_with_tricky_error() {
   let e = new Error("batman");
   e.stack = { toString: Object.create(null) };
   let s = DevToolsUtils.safeErrorString(e);
   // Still got the message, despite a bad stack property.
   do_check_true(s.contains("batman"));