forked from appium/appium-base-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.js
367 lines (329 loc) · 9.26 KB
/
errors.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import ES6Error from 'es6-error';
import _ from 'lodash';
// base error class for all of our errors
class MJSONWPError extends ES6Error {
constructor (msg, jsonwpCode) {
super(msg);
this.jsonwpCode = jsonwpCode;
}
}
class NoSuchDriverError extends MJSONWPError {
static code () {
return 6;
}
constructor (err) {
super(err || 'A session is either terminated or not started', NoSuchDriverError.code());
}
}
class NoSuchElementError extends MJSONWPError {
static code () {
return 7;
}
constructor (err) {
super(err || 'An element could not be located on the page using the given ' +
'search parameters.', NoSuchElementError.code());
}
}
class NoSuchFrameError extends MJSONWPError {
static code () {
return 8;
}
constructor (err) {
super(err || 'A request to switch to a frame could not be satisfied because ' +
'the frame could not be found.', NoSuchFrameError.code());
}
}
class UnknownCommandError extends MJSONWPError {
static code () {
return 9;
}
constructor (err) {
super(err || 'The requested resource could not be found, or a request was ' +
'received using an HTTP method that is not supported by the mapped ' +
'resource.', UnknownCommandError.code());
}
}
class StaleElementReferenceError extends MJSONWPError {
static code () {
return 10;
}
constructor (err) {
super(err || 'An element command failed because the referenced element is no ' +
'longer attached to the DOM.', StaleElementReferenceError.code());
}
}
class ElementNotVisibleError extends MJSONWPError {
static code() {
return 11;
}
constructor (err) {
super(err || 'An element command could not be completed because the element is ' +
'not visible on the page.', ElementNotVisibleError.code());
}
}
class InvalidElementStateError extends MJSONWPError {
static code() {
return 12;
}
constructor (err) {
super(err || 'An element command could not be completed because the element is ' +
'in an invalid state (e.g. attempting to click a disabled element).',
InvalidElementStateError.code());
}
}
class UnknownError extends MJSONWPError {
static code() {
return 13;
}
constructor (originalError) {
let origMessage = originalError;
if (originalError instanceof Error) {
origMessage = originalError.message;
}
let message = 'An unknown server-side error occurred while processing ' +
'the command.';
if (originalError) {
message = `${message} Original error: ${origMessage}`;
}
super(message, UnknownError.code());
}
}
class ElementIsNotSelectableError extends MJSONWPError {
static code() {
return 15;
}
constructor (err) {
super(err || 'An attempt was made to select an element that cannot be selected.',
ElementIsNotSelectableError.code());
}
}
class JavaScriptError extends MJSONWPError {
static code() {
return 17;
}
constructor (err) {
super(err || 'An error occurred while executing user supplied JavaScript.',
JavaScriptError.code());
}
}
class XPathLookupError extends MJSONWPError {
static code() {
return 19;
}
constructor (err) {
super(err || 'An error occurred while searching for an element by XPath.',
XPathLookupError.code());
}
}
class TimeoutError extends MJSONWPError {
static code() {
return 21;
}
constructor (err) {
super(err || 'An operation did not complete before its timeout expired.',
TimeoutError.code());
}
}
class NoSuchWindowError extends MJSONWPError {
static code() {
return 23;
}
constructor (err) {
super(err || 'A request to switch to a different window could not be satisfied ' +
'because the window could not be found.', NoSuchWindowError.code());
}
}
class InvalidCookieDomainError extends MJSONWPError {
static code() {
return 24;
}
constructor (err) {
super(err || 'An illegal attempt was made to set a cookie under a different ' +
'domain than the current page.', InvalidCookieDomainError.code());
}
}
class UnableToSetCookieError extends MJSONWPError {
static code() {
return 25;
}
constructor (err) {
super(err || 'A request to set a cookie\'s value could not be satisfied.',
UnableToSetCookieError.code());
}
}
class UnexpectedAlertOpenError extends MJSONWPError {
static code() {
return 26;
}
constructor (err) {
super(err || 'A modal dialog was open, blocking this operation',
UnexpectedAlertOpenError.code());
}
}
class NoAlertOpenError extends MJSONWPError {
static code() {
return 27;
}
constructor (err) {
super(err || 'An attempt was made to operate on a modal dialog when one ' +
'was not open.', NoAlertOpenError.code());
}
}
class ScriptTimeoutError extends MJSONWPError {
static code() {
return 28;
}
constructor (err) {
super(err || 'A script did not complete before its timeout expired.',
ScriptTimeoutError.code());
}
}
class InvalidElementCoordinatesError extends MJSONWPError {
static code() {
return 29;
}
constructor (err) {
super(err || 'The coordinates provided to an interactions operation are invalid.',
InvalidElementCoordinatesError.code());
}
}
class IMENotAvailableError extends MJSONWPError {
static code() {
return 30;
}
constructor (err) {
super(err || 'IME was not available.', IMENotAvailableError.code());
}
}
class IMEEngineActivationFailedError extends MJSONWPError {
static code() {
return 31;
}
constructor (err) {
super(err || 'An IME engine could not be started.',
IMEEngineActivationFailedError.code());
}
}
class InvalidSelectorError extends MJSONWPError {
static code() {
return 32;
}
constructor (err) {
super(err || 'Argument was an invalid selector (e.g. XPath/CSS).',
InvalidSelectorError.code());
}
}
class SessionNotCreatedError extends MJSONWPError {
static code() {
return 33;
}
constructor (details) {
let message = 'A new session could not be created.';
if (details) {
message += ` Details: ${details}`;
}
super(message, SessionNotCreatedError.code());
}
}
class MoveTargetOutOfBoundsError extends MJSONWPError {
static code() {
return 34;
}
constructor (err) {
super(err || 'Target provided for a move action is out of bounds.',
MoveTargetOutOfBoundsError.code());
}
}
class NoSuchContextError extends MJSONWPError {
static code() {
return 35;
}
constructor (err) {
super(err || 'No such context found.', NoSuchContextError.code());
}
}
class InvalidContextError extends MJSONWPError {
static code() {
return 36;
}
constructor (err) {
super(err || 'That command could not be executed in the current context.',
InvalidContextError.code());
}
}
class NotYetImplementedError extends MJSONWPError {
static code () {
return 13;
}
constructor (err) {
super(err || 'Method has not yet been implemented', NotYetImplementedError.code());
}
}
class NotImplementedError extends MJSONWPError {
static code() {
return 13;
}
constructor (err) {
super(err || 'Method is not implemented', NotImplementedError.code());
}
}
class BadParametersError extends ES6Error {
constructor (requiredParams, actualParams) {
super(`Parameters were incorrect. We wanted ` +
`${JSON.stringify(requiredParams)} and you ` +
`sent ${JSON.stringify(actualParams)}`);
}
}
// map of error class name to error class
const errors = {NotYetImplementedError,
NotImplementedError,
BadParametersError,
NoSuchDriverError,
NoSuchElementError,
UnknownCommandError,
StaleElementReferenceError,
ElementNotVisibleError,
InvalidElementStateError,
UnknownError,
ElementIsNotSelectableError,
JavaScriptError,
XPathLookupError,
TimeoutError,
NoSuchWindowError,
InvalidCookieDomainError,
UnableToSetCookieError,
UnexpectedAlertOpenError,
NoAlertOpenError,
ScriptTimeoutError,
InvalidElementCoordinatesError,
IMENotAvailableError,
IMEEngineActivationFailedError,
InvalidSelectorError,
SessionNotCreatedError,
MoveTargetOutOfBoundsError,
NoSuchContextError,
InvalidContextError,
NoSuchFrameError};
// map of error code to error class
const errorCodeMap = {};
for (let ErrorClass of _.values(errors)) {
if (ErrorClass.code) {
errorCodeMap[ErrorClass.code()] = ErrorClass;
}
}
function isErrorType (err, type) {
// `name` property is the constructor name
if (type.name === MJSONWPError.name) {
// `jsonwpCode` is `0` on success
return !!err.jsonwpCode;
}
return err.constructor.name === type.name;
}
// retrieve an error with the code and message
function errorFromCode (code, message) {
if (code !== 13 && errorCodeMap[code]) {
return new errorCodeMap[code](message);
}
return new UnknownError(message);
}
export { MJSONWPError, errors, isErrorType, errorFromCode };