# lib/atoms/error.js

1 | // Licensed to the Software Freedom Conservancy (SFC) under one
2 | // or more contributor license agreements. See the NOTICE file
3 | // distributed with this work for additional information
4 | // regarding copyright ownership. The SFC licenses this file
5 | // to you under the Apache License, Version 2.0 (the
6 | // "License"); you may not use this file except in compliance
7 | // with the License. You may obtain a copy of the License at
8 | //
9 | // http://www.apache.org/licenses/LICENSE-2.0
10 | //
11 | // Unless required by applicable law or agreed to in writing,
12 | // software distributed under the License is distributed on an
13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 | // KIND, either express or implied. See the License for the
15 | // specific language governing permissions and limitations
16 | // under the License.
17 |
18 | /**
19 | * @fileoverview Utilities for working with errors as defined by WebDriver's
20 | * wire protocol: https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol
21 | */
22 |
23 | goog.provide('bot.Error');
24 | goog.provide('bot.ErrorCode');
25 |
26 |
27 | /**
28 | * Error codes from the Selenium WebDriver protocol:
29 | * https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol#response-status-codes
30 | *
31 | * @enum {number}
32 | */
33 | bot.ErrorCode = {
34 | SUCCESS: 0, // Included for completeness
35 |
36 | NO_SUCH_ELEMENT: 7,
37 | NO_SUCH_FRAME: 8,
38 | UNKNOWN_COMMAND: 9,
39 | UNSUPPORTED_OPERATION: 9, // Alias.
40 | STALE_ELEMENT_REFERENCE: 10,
41 | ELEMENT_NOT_VISIBLE: 11,
42 | INVALID_ELEMENT_STATE: 12,
43 | UNKNOWN_ERROR: 13,
44 | ELEMENT_NOT_SELECTABLE: 15,
45 | JAVASCRIPT_ERROR: 17,
46 | XPATH_LOOKUP_ERROR: 19,
47 | TIMEOUT: 21,
48 | NO_SUCH_WINDOW: 23,
49 | INVALID_COOKIE_DOMAIN: 24,
50 | UNABLE_TO_SET_COOKIE: 25,
51 | UNEXPECTED_ALERT_OPEN: 26,
52 | NO_SUCH_ALERT: 27,
53 | SCRIPT_TIMEOUT: 28,
54 | INVALID_ELEMENT_COORDINATES: 29,
55 | IME_NOT_AVAILABLE: 30,
56 | IME_ENGINE_ACTIVATION_FAILED: 31,
57 | INVALID_SELECTOR_ERROR: 32,
58 | SESSION_NOT_CREATED: 33,
59 | MOVE_TARGET_OUT_OF_BOUNDS: 34,
60 | SQL_DATABASE_ERROR: 35,
61 | INVALID_XPATH_SELECTOR: 51,
62 | INVALID_XPATH_SELECTOR_RETURN_TYPE: 52,
63 | // The following error codes are derived straight from HTTP return codes.
64 | METHOD_NOT_ALLOWED: 405
65 | };
66 |
67 |
68 | /**
69 | * Represents an error returned from a WebDriver command request.
70 | *
71 | * @param {!bot.ErrorCode} code The error's status code.
72 | * @param {string=} opt_message Optional error message.
73 | * @constructor
74 | * @extends {Error}
75 | */
76 | bot.Error = function(code, opt_message) {
77 |
78 | /**
79 | * This error's status code.
80 | * @type {!bot.ErrorCode}
81 | */
82 | this.code = code;
83 |
84 | /** @type {string} */
85 | this.state =
86 | bot.Error.CODE_TO_STATE_[code] || bot.Error.State.UNKNOWN_ERROR;
87 |
88 | /** @override */
89 | this.message = opt_message || '';
90 |
91 | var name = this.state.replace(/((?:^|\s+)[a-z])/g, function(str) {
92 | // IE<9 does not support String#trim(). Also, IE does not include 0xa0
93 | // (the non-breaking-space) in the \s character class, so we have to
94 | // explicitly include it.
95 | return str.toUpperCase().replace(/^[\s\xa0]+/g, '');
96 | });
97 |
98 | var l = name.length - 'Error'.length;
99 | if (l < 0 || name.indexOf('Error', l) != l) {
100 | name += 'Error';
101 | }
102 |
103 | /** @override */
104 | this.name = name;
105 |
106 | // Generate a stacktrace for our custom error; ensure the error has our
107 | // custom name and message so the stack prints correctly in all browsers.
108 | var template = new Error(this.message);
109 | template.name = this.name;
110 |
111 | /** @override */
112 | this.stack = template.stack || '';
113 | };
114 | goog.inherits(bot.Error, Error);
115 |
116 |
117 | /**
118 | * Status strings enumerated in the W3C WebDriver protocol.
119 | * @enum {string}
120 | * @see https://w3c.github.io/webdriver/webdriver-spec.html#handling-errors
121 | */
122 | bot.Error.State = {
123 | ELEMENT_NOT_SELECTABLE: 'element not selectable',
124 | ELEMENT_NOT_VISIBLE: 'element not visible',
125 | INVALID_ARGUMENT: 'invalid argument',
126 | INVALID_COOKIE_DOMAIN: 'invalid cookie domain',
127 | INVALID_ELEMENT_COORDINATES: 'invalid element coordinates',
128 | INVALID_ELEMENT_STATE: 'invalid element state',
129 | INVALID_SELECTOR: 'invalid selector',
130 | INVALID_SESSION_ID: 'invalid session id',
131 | JAVASCRIPT_ERROR: 'javascript error',
132 | MOVE_TARGET_OUT_OF_BOUNDS: 'move target out of bounds',
133 | NO_SUCH_ALERT: 'no such alert',
134 | NO_SUCH_ELEMENT: 'no such element',
135 | NO_SUCH_FRAME: 'no such frame',
136 | NO_SUCH_WINDOW: 'no such window',
137 | SCRIPT_TIMEOUT: 'script timeout',
138 | SESSION_NOT_CREATED: 'session not created',
139 | STALE_ELEMENT_REFERENCE: 'stale element reference',
140 | TIMEOUT: 'timeout',
141 | UNABLE_TO_SET_COOKIE: 'unable to set cookie',
142 | UNEXPECTED_ALERT_OPEN: 'unexpected alert open',
143 | UNKNOWN_COMMAND: 'unknown command',
144 | UNKNOWN_ERROR: 'unknown error',
145 | UNKNOWN_METHOD: 'unknown method',
146 | UNSUPPORTED_OPERATION: 'unsupported operation'
147 | };
148 |
149 |
150 | /**
151 | * A map of error codes to state string.
152 | * @private {!Object.<bot.ErrorCode, bot.Error.State>}
153 | */
154 | bot.Error.CODE_TO_STATE_ = {};
155 | goog.scope(function() {
156 | var map = bot.Error.CODE_TO_STATE_;
157 | var code = bot.ErrorCode;
158 | var state = bot.Error.State;
159 |
160 | map[code.ELEMENT_NOT_SELECTABLE] = state.ELEMENT_NOT_SELECTABLE;
161 | map[code.ELEMENT_NOT_VISIBLE] = state.ELEMENT_NOT_VISIBLE;
162 | map[code.IME_ENGINE_ACTIVATION_FAILED] = state.UNKNOWN_ERROR;
163 | map[code.IME_NOT_AVAILABLE] = state.UNKNOWN_ERROR;
164 | map[code.INVALID_COOKIE_DOMAIN] = state.INVALID_COOKIE_DOMAIN;
165 | map[code.INVALID_ELEMENT_COORDINATES] = state.INVALID_ELEMENT_COORDINATES;
166 | map[code.INVALID_ELEMENT_STATE] = state.INVALID_ELEMENT_STATE;
167 | map[code.INVALID_SELECTOR_ERROR] = state.INVALID_SELECTOR;
168 | map[code.INVALID_XPATH_SELECTOR] = state.INVALID_SELECTOR;
169 | map[code.INVALID_XPATH_SELECTOR_RETURN_TYPE] = state.INVALID_SELECTOR;
170 | map[code.JAVASCRIPT_ERROR] = state.JAVASCRIPT_ERROR;
171 | map[code.METHOD_NOT_ALLOWED] = state.UNSUPPORTED_OPERATION;
172 | map[code.MOVE_TARGET_OUT_OF_BOUNDS] = state.MOVE_TARGET_OUT_OF_BOUNDS;
173 | map[code.NO_SUCH_ALERT] = state.NO_SUCH_ALERT;
174 | map[code.NO_SUCH_ELEMENT] = state.NO_SUCH_ELEMENT;
175 | map[code.NO_SUCH_FRAME] = state.NO_SUCH_FRAME;
176 | map[code.NO_SUCH_WINDOW] = state.NO_SUCH_WINDOW;
177 | map[code.SCRIPT_TIMEOUT] = state.SCRIPT_TIMEOUT;
178 | map[code.SESSION_NOT_CREATED] = state.SESSION_NOT_CREATED;
179 | map[code.STALE_ELEMENT_REFERENCE] = state.STALE_ELEMENT_REFERENCE;
180 | map[code.TIMEOUT] = state.TIMEOUT;
181 | map[code.UNABLE_TO_SET_COOKIE] = state.UNABLE_TO_SET_COOKIE;
182 | map[code.UNEXPECTED_ALERT_OPEN] = state.UNEXPECTED_ALERT_OPEN
183 | map[code.UNKNOWN_ERROR] = state.UNKNOWN_ERROR;
184 | map[code.UNSUPPORTED_OPERATION] = state.UNKNOWN_COMMAND;
185 | }); // goog.scope
186 |
187 |
188 | /**
189 | * Flag used for duck-typing when this code is embedded in a Firefox extension.
190 | * This is required since an Error thrown in one component and then reported
191 | * to another will fail instanceof checks in the second component.
192 | * @type {boolean}
193 | */
194 | bot.Error.prototype.isAutomationError = true;
195 |
196 |
197 | if (goog.DEBUG) {
198 | /** @return {string} The string representation of this error. */
199 | bot.Error.prototype.toString = function() {
200 | return this.name + ': ' + this.message;
201 | };
202 | }