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
--- 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");