Bug 1333014 - Use static lookup of error status; r?whimboo draft
authorAndreas Tolfsen <ato@mozilla.com>
Fri, 03 Feb 2017 19:31:12 +0000
changeset 479459 8f80f1b9bfbcaccd9ac8293fc4cc2f8d5e30fcdb
parent 479458 188a8f00ffc1c26091d9ede2284536ccfb8ae7d1
child 479460 e050ac0c4ed462234678f489df467b053e7a5c7e
push id44264
push userbmo:ato@mozilla.com
push dateMon, 06 Feb 2017 19:13:44 +0000
reviewerswhimboo
bugs1333014
milestone54.0a1
Bug 1333014 - Use static lookup of error status; r?whimboo We previously constructed each error instance on loading the testing/marionette/error.js module. We can avoid this overhead by using a static lookup table. MozReview-Commit-ID: 6S7XFvxPZpI
testing/marionette/error.js
--- a/testing/marionette/error.js
+++ b/testing/marionette/error.js
@@ -174,22 +174,23 @@ error.toJson = function (err) {
  *
  * @param {Object.<string, string>} json
  *     JSON dictionary with the keys "error", "message", and "stacktrace".
  *
  * @return {WebDriverError}
  *     Deserialised error prototype.
  */
 error.fromJson = function (json) {
-  if (!statusLookup.has(json.error)) {
+  if (!STATUSES.has(json.error)) {
     throw new TypeError(`Undeserialisable error type: ${json.error}`);
   }
 
-  let errCls = statusLookup.get(json.error);
-  let err = new errCls(json.message);
+  let errCls = STATUSES.get(json.error);
+  let err = new errCls();
+  err.message = json.message;
   if ("stacktrace" in json) {
     err.stack = json.stacktrace;
   }
   return err;
 };
 
 /**
  * WebDriverError is the prototypal parent of all WebDriver errors.
@@ -423,16 +424,32 @@ UnknownError.prototype = Object.create(W
 
 this.UnsupportedOperationError = function (msg) {
   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) {
-  let cls = this[s];
-  let inst = new cls();
-  nameLookup.set(inst.name, cls);
-  statusLookup.set(inst.status, cls);
-};
+const STATUSES = new Map([
+  ["element click intercepted", ElementClickInterceptedError],
+  ["element not accessible", ElementNotAccessibleError],
+  ["element not interactable", ElementNotInteractableError],
+  ["insecure certificate", InsecureCertificateError],
+  ["invalid argument", InvalidArgumentError],
+  ["invalid element state", InvalidElementStateError],
+  ["invalid selector", InvalidSelectorError],
+  ["invalid session id", InvalidSessionIdError],
+  ["javascript error", JavaScriptError],
+  ["no alert open", NoAlertOpenError],
+  ["no such element", NoSuchElementError],
+  ["no such frame", NoSuchFrameError],
+  ["no such window", NoSuchWindowError],
+  ["script timeout", ScriptTimeoutError],
+  ["session not created", SessionNotCreatedError],
+  ["stale element reference", StaleElementReferenceError],
+  ["timeout", TimeoutError],
+  ["unable to set cookie", UnableToSetCookieError],
+  ["unknown command", UnknownCommandError],
+  ["unknown error", UnknownError],
+  ["unsupported operation", UnsupportedOperationError],
+  ["webdriver error", WebDriverError],
+]);