Bug 1245153 - error.isError must recognise built-in Error prototypes; r=automatedtester
Due to a previous programming error, error.isError only recognised
the base Error prototype. It must also test for the other built-in
prototypes, such as TypeError et al.
MozReview-Commit-ID: HLkiOAg0Jl1
--- a/testing/marionette/error.js
+++ b/testing/marionette/error.js
@@ -1,17 +1,17 @@
/* 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";
const {interfaces: Ci, utils: Cu} = Components;
-const errors = [
+const ERRORS = [
"ElementNotAccessibleError",
"ElementNotVisibleError",
"InvalidArgumentError",
"InvalidElementStateError",
"InvalidSelectorError",
"InvalidSessionIdError",
"JavaScriptError",
"NoAlertOpenError",
@@ -24,20 +24,31 @@ const errors = [
"TimeoutError",
"UnableToSetCookieError",
"UnknownCommandError",
"UnknownError",
"UnsupportedOperationError",
"WebDriverError",
];
-this.EXPORTED_SYMBOLS = ["error"].concat(errors);
+this.EXPORTED_SYMBOLS = ["error"].concat(ERRORS);
this.error = {};
+error.BuiltinErrors = {
+ Error: 0,
+ EvalError: 1,
+ InternalError: 2,
+ RangeError: 3,
+ ReferenceError: 4,
+ SyntaxError: 5,
+ TypeError: 6,
+ URIError: 7,
+};
+
/**
* Checks if obj is an instance of the Error prototype in a safe manner.
* Prefer using this over using instanceof since the Error prototype
* isn't unique across browsers, and XPCOM nsIException's are special
* snowflakes.
*
* @param {*} val
* Any value that should be undergo the test for errorness.
@@ -45,26 +56,26 @@ this.error = {};
* True if error, false otherwise.
*/
error.isError = function(val) {
if (val === null || typeof val != "object") {
return false;
} else if (val instanceof Ci.nsIException) {
return true;
} else {
- return Object.getPrototypeOf(val) == "Error";
+ return Object.getPrototypeOf(val) in error.BuiltinErrors;
}
};
/**
* Checks if obj is an object in the WebDriverError prototypal chain.
*/
error.isWebDriverError = function(obj) {
return error.isError(obj) &&
- ("name" in obj && errors.indexOf(obj.name) >= 0);
+ ("name" in obj && ERRORS.indexOf(obj.name) >= 0);
};
/**
* Unhandled error reporter. Dumps the error and its stacktrace to console,
* and reports error to the Browser Console.
*/
error.report = function(err) {
let msg = `Marionette threw an error: ${error.stringify(err)}`;
@@ -323,14 +334,14 @@ this.UnsupportedOperationError = functio
WebDriverError.call(this, msg);
this.name = "UnsupportedOperationError";
this.status = "unsupported operation";
};
UnsupportedOperationError.prototype = Object.create(WebDriverError.prototype);
const nameLookup = new Map();
const statusLookup = new Map();
-for (let s of errors) {
+for (let s of ERRORS) {
let cls = this[s];
let inst = new cls();
nameLookup.set(inst.name, cls);
statusLookup.set(inst.status, cls);
};
--- a/testing/marionette/test_error.js
+++ b/testing/marionette/test_error.js
@@ -9,32 +9,59 @@ Cu.import("chrome://marionette/content/e
function run_test() {
run_next_test();
}
function notok(condition) {
ok(!(condition));
}
+add_test(function test_BuiltinErrors() {
+ ok("Error" in error.BuiltinErrors);
+ ok("EvalError" in error.BuiltinErrors);
+ ok("InternalError" in error.BuiltinErrors);
+ ok("RangeError" in error.BuiltinErrors);
+ ok("ReferenceError" in error.BuiltinErrors);
+ ok("SyntaxError" in error.BuiltinErrors);
+ ok("TypeError" in error.BuiltinErrors);
+ ok("URIError" in error.BuiltinErrors);
+
+ run_next_test();
+});
+
add_test(function test_isError() {
notok(error.isError(null));
notok(error.isError([]));
notok(error.isError(new Date()));
ok(error.isError(new Components.Exception()));
ok(error.isError(new Error()));
+ ok(error.isError(new EvalError()));
+ ok(error.isError(new InternalError()));
+ ok(error.isError(new RangeError()));
+ ok(error.isError(new ReferenceError()));
+ ok(error.isError(new SyntaxError()));
+ ok(error.isError(new TypeError()));
+ ok(error.isError(new URIError()));
ok(error.isError(new WebDriverError()));
ok(error.isError(new InvalidArgumentError()));
run_next_test();
});
add_test(function test_isWebDriverError() {
notok(error.isWebDriverError(new Components.Exception()));
notok(error.isWebDriverError(new Error()));
+ notok(error.isWebDriverError(new EvalError()));
+ notok(error.isWebDriverError(new InternalError()));
+ notok(error.isWebDriverError(new RangeError()));
+ notok(error.isWebDriverError(new ReferenceError()));
+ notok(error.isWebDriverError(new SyntaxError()));
+ notok(error.isWebDriverError(new TypeError()));
+ notok(error.isWebDriverError(new URIError()));
ok(error.isWebDriverError(new WebDriverError()));
ok(error.isWebDriverError(new InvalidArgumentError()));
run_next_test();
});
add_test(function test_stringify() {