Bug 1394849 - Add error.stack to create stacktraces. r?automatedtester draft
authorAndreas Tolfsen <ato@sny.no>
Tue, 29 Aug 2017 17:34:37 +0100
changeset 656657 f93872bc14f4cf2d8a564da16c5d3ad89e992526
parent 656656 1989519c5034b759b91e9241bcfc914cce671228
child 729196 6e929aff8241a71e7906f5cac48669e9a0286626
push id77274
push userbmo:ato@sny.no
push dateThu, 31 Aug 2017 12:54:27 +0000
reviewersautomatedtester
bugs1394849
milestone57.0a1
Bug 1394849 - Add error.stack to create stacktraces. r?automatedtester This patch introduces a new error.stack function as a shorthand for creating stacktraces. It is equivalent to calling new Error().stack and removing the first line of the stack. Removing the first line is needed to make it appear as if the error originated from the caller's position in the program. MozReview-Commit-ID: DpSSWU5vPDm
testing/marionette/error.js
testing/marionette/test_error.js
--- a/testing/marionette/error.js
+++ b/testing/marionette/error.js
@@ -43,16 +43,17 @@ const BUILTIN_ERRORS = new Set([
   "SyntaxError",
   "TypeError",
   "URIError",
 ]);
 
 this.EXPORTED_SYMBOLS = [
   "error",
   "pprint",
+  "stack",
 ].concat(Array.from(ERRORS));
 
 /** @namespace */
 this.error = {};
 
 /**
  * Check if |val| is an instance of the |Error| prototype.
  *
@@ -210,16 +211,24 @@ this.pprint = function(ss, ...values) {
         s = typeof val;
       }
       res.push(s);
     }
   }
   return res.join("");
 };
 
+/** Create a stacktrace to the current line in the program. */
+this.stack = function() {
+  let trace = new Error().stack;
+  let sa = trace.split("\n");
+  sa = sa.slice(1);
+  return "stacktrace:\n" + sa.join("\n");
+};
+
 /**
  * WebDriverError is the prototypal parent of all WebDriver errors.
  * It should not be used directly, as it does not correspond to a real
  * error in the specification.
  */
 class WebDriverError extends Error {
   /**
    * @param {(string|Error)=} x
--- a/testing/marionette/test_error.js
+++ b/testing/marionette/test_error.js
@@ -19,16 +19,17 @@ const {
   MoveTargetOutOfBoundsError,
   NoAlertOpenError,
   NoSuchElementError,
   NoSuchFrameError,
   NoSuchWindowError,
   pprint,
   ScriptTimeoutError,
   SessionNotCreatedError,
+  stack,
   StaleElementReferenceError,
   TimeoutError,
   UnableToSetCookieError,
   UnexpectedAlertOpenError,
   UnknownCommandError,
   UnknownError,
   UnsupportedOperationError,
   WebDriverError,
@@ -139,16 +140,24 @@ add_test(function test_pprint() {
     classList: {length: 1},
     className: "bar baz",
   };
   equal('<input id="foo" class="bar baz">', pprint`${el}`);
 
   run_next_test();
 });
 
+add_test(function test_stack() {
+  equal("string", typeof stack());
+  ok(stack().includes("test_stack"));
+  ok(!stack().includes("add_test"));
+
+  run_next_test();
+});
+
 add_test(function test_toJSON() {
   let e0 = new WebDriverError();
   let e0s = e0.toJSON();
   equal(e0s.error, "webdriver error");
   equal(e0s.message, "");
   equal(e0s.stacktrace, e0.stack);
 
   let e1 = new WebDriverError("a");