58
58
import java .util .Set ;
59
59
import java .util .logging .Logger ;
60
60
import java .util .stream .Collectors ;
61
+ import java .util .stream .Stream ;
61
62
62
63
/**
63
64
* Defines common error codes for the wire protocol.
@@ -97,6 +98,9 @@ public class ErrorCodes {
97
98
// json wire protocol doesn't have analogous status codes for
98
99
// these new W3C status repsonse 'codes', so making some up!
99
100
public static final int ELEMENT_NOT_INTERACTABLE = 60 ;
101
+ public static final int INVALID_ARGUMENT = 61 ;
102
+ public static final int NO_SUCH_COOKIE = 62 ;
103
+ public static final int UNABLE_TO_CAPTURE_SCREEN = 63 ;
100
104
101
105
// The following error codes are derived straight from HTTP return codes.
102
106
public static final int METHOD_NOT_ALLOWED = 405 ;
@@ -113,9 +117,9 @@ public class ErrorCodes {
113
117
.put (400 ,
114
118
ImmutableSortedSet .<StatusTuple >naturalOrder ()
115
119
.add (new StatusTuple ("element not selectable" , ELEMENT_NOT_SELECTABLE , ElementNotSelectableException .class ))
116
- .add (new StatusTuple ("element not interactable" , ELEMENT_NOT_INTERACTABLE , ElementNotInteractableException .class ))
120
+ .add (new StatusTuple ("element not interactable" , ELEMENT_NOT_INTERACTABLE , ElementNotInteractableException .class , UNHANDLED_ERROR ))
117
121
.add (new StatusTuple ("element not visible" , ELEMENT_NOT_VISIBLE , ElementNotVisibleException .class ))
118
- .add (new StatusTuple ("invalid argument" , UNHANDLED_ERROR , InvalidArgumentException .class ))
122
+ .add (new StatusTuple ("invalid argument" , INVALID_ARGUMENT , InvalidArgumentException .class , UNHANDLED_ERROR ))
119
123
.add (new StatusTuple ("invalid cookie domain" , INVALID_COOKIE_DOMAIN , InvalidCookieDomainException .class ))
120
124
.add (new StatusTuple ("invalid element coordinates" , INVALID_ELEMENT_COORDINATES , InvalidCoordinatesException .class ))
121
125
.add (new StatusTuple ("invalid element state" , INVALID_ELEMENT_STATE , InvalidElementStateException .class ))
@@ -131,9 +135,9 @@ public class ErrorCodes {
131
135
.put (404 ,
132
136
ImmutableSortedSet .<StatusTuple >naturalOrder ()
133
137
.add (new StatusTuple ("invalid session id" , NO_SUCH_SESSION , NoSuchSessionException .class ))
134
- .add (new StatusTuple ("no such cookie" , UNHANDLED_ERROR , NoSuchCookieException .class ))
138
+ .add (new StatusTuple ("no such cookie" , NO_SUCH_COOKIE , NoSuchCookieException .class , UNHANDLED_ERROR ))
135
139
.add (new StatusTuple ("no such element" , NO_SUCH_ELEMENT , NoSuchElementException .class ))
136
- .add (new StatusTuple ("unknown command" , UNKNOWN_COMMAND , UnsupportedCommandException .class , UNHANDLED_ERROR ))
140
+ .add (new StatusTuple ("unknown command" , UNKNOWN_COMMAND , UnsupportedCommandException .class ))
137
141
.build ())
138
142
.put (405 ,
139
143
ImmutableSortedSet .<StatusTuple >naturalOrder ()
@@ -146,13 +150,13 @@ public class ErrorCodes {
146
150
.build ())
147
151
.put (500 ,
148
152
ImmutableSortedSet .<StatusTuple >naturalOrder ()
149
- .add (new StatusTuple ("javascript error" , JAVASCRIPT_ERROR , WebDriverException .class , UNHANDLED_ERROR ))
153
+ .add (new StatusTuple ("javascript error" , JAVASCRIPT_ERROR , WebDriverException .class ))
150
154
.add (new StatusTuple ("move target out of bounds" , MOVE_TARGET_OUT_OF_BOUNDS , MoveTargetOutOfBoundsException .class ))
151
155
.add (new StatusTuple ("session not created" , SESSION_NOT_CREATED , SessionNotCreatedException .class ))
152
156
.add (new StatusTuple ("unable to set cookie" , UNABLE_TO_SET_COOKIE , UnableToSetCookieException .class ))
153
- .add (new StatusTuple ("unable to capture screen" , UNHANDLED_ERROR , ScreenshotException .class ))
157
+ .add (new StatusTuple ("unable to capture screen" , UNABLE_TO_CAPTURE_SCREEN , ScreenshotException .class , UNHANDLED_ERROR ))
154
158
.add (new StatusTuple ("unexpected alert open" , UNEXPECTED_ALERT_PRESENT , UnhandledAlertException .class ))
155
- .add (new StatusTuple ("unknown error" , UNHANDLED_ERROR , WebDriverException .class , UNHANDLED_ERROR ))
159
+ .add (new StatusTuple ("unknown error" , UNHANDLED_ERROR , WebDriverException .class ))
156
160
.add (new StatusTuple ("unsupported operation" , METHOD_NOT_ALLOWED , UnsupportedCommandException .class , UNHANDLED_ERROR ))
157
161
.add (new StatusTuple ("unsupported operation" , IME_NOT_AVAILABLE , ImeNotAvailableException .class ))
158
162
.add (new StatusTuple ("unsupported operation" , IME_ENGINE_ACTIVATION_FAILED , ImeActivationFailedException .class ))
@@ -165,6 +169,11 @@ public class ErrorCodes {
165
169
.flatMap (Collection ::stream )
166
170
.collect (Collectors .toMap (StatusTuple ::asStatus , StatusTuple ::asState , (key1 , key2 ) -> key1 ));
167
171
172
+ private static final Map <Integer , Class > JSON_TO_EXCEPTION = ALL_CODES .values ().stream ()
173
+ .flatMap (Collection ::stream )
174
+ .filter (tuple -> tuple .getException () != null )
175
+ .collect (Collectors .toMap (StatusTuple ::asStatus , StatusTuple ::getException , (key1 , key2 ) -> key1 ));
176
+
168
177
private static final Map <String , Integer > W3C_TO_JSON = ALL_CODES .values ().stream ()
169
178
.flatMap (Collection ::stream )
170
179
.collect (Collectors .toMap (StatusTuple ::asState , StatusTuple ::asStatus , (key1 , key2 ) -> key1 ));
@@ -221,7 +230,7 @@ public int toStatus(String webdriverState, Optional<Integer> httpStatus) {
221
230
* {@code statusCode == 0}.
222
231
*/
223
232
public Class <? extends WebDriverException > getExceptionType (int statusCode ) {
224
- return getExceptionType ( toState ( statusCode ) );
233
+ return JSON_TO_EXCEPTION . getOrDefault ( statusCode , WebDriverException . class );
225
234
}
226
235
227
236
public Class <? extends WebDriverException > getExceptionType (String webdriverState ) {
@@ -242,13 +251,34 @@ public int toStatusCode(Throwable e) {
242
251
}
243
252
244
253
// And then handle the other cases
245
- Set < Integer > possibleMatches = ALL_CODES .values ().stream ()
254
+ Stream < StatusTuple > allCodesStream = ALL_CODES .values ().stream ()
246
255
.flatMap (Collection ::stream )
247
256
.filter (tuple -> tuple .getException () != null )
248
- .filter (tuple -> tuple .associatedException .isAssignableFrom (e .getClass ()))
257
+ .filter (tuple -> tuple .associatedException .isAssignableFrom (e .getClass ()));
258
+
259
+ if (e instanceof WebDriverException && ((WebDriverException )e ).getStatusCode () != null ) {
260
+ allCodesStream = allCodesStream .filter (tuple -> ((WebDriverException )e ).getStatusCode () == tuple .jsonStatus );
261
+ }
262
+
263
+ Set <Integer > possibleMatches = allCodesStream
249
264
.map (StatusTuple ::getStatusFromException )
250
265
.collect (Collectors .toSet ());
251
266
267
+ if (possibleMatches .size () > 1 ) {
268
+ // if there's multiple matches, let's try filtering on the exact exception class to see if we
269
+ // can reduce the possibilities
270
+ Set <Integer > reducedPossibleMatches = ALL_CODES .values ().stream ()
271
+ .flatMap (Collection ::stream )
272
+ .filter (tuple -> tuple .getException () != null )
273
+ .filter (tuple -> tuple .associatedException .equals (e .getClass ()))
274
+ .map (StatusTuple ::getStatusFromException )
275
+ .collect (Collectors .toSet ());
276
+
277
+ if (reducedPossibleMatches .size () > 0 ) {
278
+ possibleMatches = reducedPossibleMatches ;
279
+ }
280
+ }
281
+
252
282
return Preconditions .checkNotNull (Iterables .getFirst (possibleMatches , UNHANDLED_ERROR ));
253
283
}
254
284
0 commit comments