Skip to content

Commit 9f4f47e

Browse files
committed
[java] Changing message when session is not found in Grid
This helps the client to map the exception properly. Also, the IME exceptions were a leftover from a previous commit. [skip ci]
1 parent 20b6957 commit 9f4f47e

5 files changed

Lines changed: 84 additions & 160 deletions

File tree

java/src/org/openqa/selenium/ImeActivationFailedException.java

Lines changed: 0 additions & 34 deletions
This file was deleted.

java/src/org/openqa/selenium/ImeNotAvailableException.java

Lines changed: 0 additions & 35 deletions
This file was deleted.

java/src/org/openqa/selenium/grid/router/HandleSession.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public HttpResponse execute(HttpRequest req) {
140140
response.setContent(asJson(ImmutableMap.of(
141141
"value", req.getUri(),
142142
"message", errorMessage,
143-
"error", NoSuchSessionException.class.getName())));
143+
"error", "invalid session id")));
144144
return response;
145145
}
146146

java/src/org/openqa/selenium/remote/ErrorCodes.java

Lines changed: 65 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@
1919

2020
import com.google.common.collect.ImmutableSet;
2121
import com.google.common.collect.Iterables;
22+
2223
import org.openqa.selenium.ElementClickInterceptedException;
2324
import org.openqa.selenium.ElementNotInteractableException;
24-
import org.openqa.selenium.ImeActivationFailedException;
25-
import org.openqa.selenium.ImeNotAvailableException;
2625
import org.openqa.selenium.InvalidArgumentException;
2726
import org.openqa.selenium.InvalidCookieDomainException;
2827
import org.openqa.selenium.InvalidElementStateException;
@@ -101,6 +100,70 @@ public class ErrorCodes {
101100
public static final int METHOD_NOT_ALLOWED = 405;
102101

103102
private static final Logger log = Logger.getLogger(ErrorCodes.class.getName());
103+
// Every row on this table should be self-explanatory, except for the two booleans at the end.
104+
// The first of these is "isCanonicalJsonCodeForException". This means that when doing the mapping
105+
// for a JSON Wire Protocol status code, this KnownError provides the exception that should be
106+
// thrown. The second boolean is "isCanonicalForW3C". This means that when mapping a state or
107+
// exception to a W3C state, this KnownError provides the default exception and Json Wire Protocol
108+
// status to send.
109+
private static final ImmutableSet<KnownError> KNOWN_ERRORS = ImmutableSet.<KnownError>builder()
110+
.add(new KnownError(ASYNC_SCRIPT_TIMEOUT, "script timeout", 500, ScriptTimeoutException.class, true, true))
111+
.add(new KnownError(ELEMENT_CLICK_INTERCEPTED, "element click intercepted", 400, ElementClickInterceptedException.class, true, true))
112+
.add(new KnownError(ELEMENT_NOT_INTERACTABLE, "element not interactable", 400, ElementNotInteractableException.class, true, true))
113+
.add(new KnownError(INVALID_ARGUMENT, "invalid argument", 400, InvalidArgumentException.class, true, true))
114+
.add(new KnownError(INVALID_COOKIE_DOMAIN, "invalid cookie domain", 400, InvalidCookieDomainException.class, true, true))
115+
.add(new KnownError(INVALID_ELEMENT_STATE, "invalid element state", 400, InvalidElementStateException.class, true, true))
116+
.add(new KnownError(INVALID_SELECTOR_ERROR, "invalid selector", 400, InvalidSelectorException.class, true, true))
117+
.add(new KnownError(INVALID_XPATH_SELECTOR, "invalid selector", 400, InvalidSelectorException.class, false, false))
118+
.add(new KnownError(INVALID_XPATH_SELECTOR_RETURN_TYPER, "invalid selector", 400, InvalidSelectorException.class, false, true))
119+
.add(new KnownError(JAVASCRIPT_ERROR, "javascript error", 500, JavascriptException.class, true, true))
120+
.add(new KnownError(METHOD_NOT_ALLOWED, "unknown method", 405, UnsupportedCommandException.class, false, true))
121+
.add(new KnownError(METHOD_NOT_ALLOWED, "unsupported operation", 500, UnsupportedCommandException.class, false, true))
122+
.add(new KnownError(MOVE_TARGET_OUT_OF_BOUNDS, "move target out of bounds", 500, MoveTargetOutOfBoundsException.class, true, true))
123+
.add(new KnownError(NO_ALERT_PRESENT, "no such alert", 404, NoAlertPresentException.class, true, true))
124+
.add(new KnownError(NO_SUCH_COOKIE, "no such cookie", 404, NoSuchCookieException.class, true, true))
125+
.add(new KnownError(NO_SUCH_ELEMENT, "no such element", 404, NoSuchElementException.class, true, true))
126+
.add(new KnownError(NO_SUCH_FRAME, "no such frame", 404, NoSuchFrameException.class, true, true))
127+
.add(new KnownError(NO_SUCH_SESSION, "invalid session id", 404, NoSuchSessionException.class, true, true))
128+
.add(new KnownError(NO_SUCH_SHADOW_ROOT, "no such shadow root", 404, NoSuchShadowRootException.class, true, true))
129+
.add(new KnownError(NO_SUCH_WINDOW, "no such window", 404, NoSuchWindowException.class, true, true))
130+
.add(new KnownError(SESSION_NOT_CREATED, "session not created", 500, SessionNotCreatedException.class ,true, true))
131+
.add(new KnownError(STALE_ELEMENT_REFERENCE, "stale element reference", 404, StaleElementReferenceException.class, true, true))
132+
.add(new KnownError(TIMEOUT, "timeout", 500, TimeoutException.class, true, true))
133+
.add(new KnownError(XPATH_LOOKUP_ERROR, "invalid selector", 400, InvalidSelectorException.class, false, false))
134+
.add(new KnownError(UNABLE_TO_CAPTURE_SCREEN, "unable to capture screen", 500, ScreenshotException.class, true, true))
135+
.add(new KnownError(UNABLE_TO_SET_COOKIE, "unable to set cookie", 500, UnableToSetCookieException.class, true, true))
136+
.add(new KnownError(UNEXPECTED_ALERT_PRESENT, "unexpected alert open", 500, UnhandledAlertException.class, true, true))
137+
.add(new KnownError(UNHANDLED_ERROR, "unknown error", 500, WebDriverException.class, true, true))
138+
.add(new KnownError(UNKNOWN_COMMAND, "unknown command", 404, UnsupportedCommandException.class, true, true))
139+
140+
.build();
141+
142+
static {{
143+
// Validate uniqueness constraints.
144+
//
145+
// There should be only one canonical JSON Wire protocol code per exception
146+
Map<Object, Set<KnownError>> matched = new HashMap<>();
147+
for (KnownError knownError : KNOWN_ERRORS) {
148+
matched.computeIfAbsent(knownError, key -> new HashSet<>()).add(knownError);
149+
}
150+
for (Set<KnownError> errors : matched.values()) {
151+
if (errors.size() != 1) {
152+
throw new RuntimeException("Duplicate canonical exceptions: " + errors);
153+
}
154+
}
155+
156+
// There should only be one canonical W3C code to JSON Wire Protocol code
157+
// matched = new HashMap<>();
158+
// for (KnownError error : KNOWN_ERRORS) {
159+
// matched.computeIfAbsent(error.getW3cCode(), key -> new HashSet<>()).add(error);
160+
// }
161+
// for (Set<KnownError> errors : matched.values()) {
162+
// if (errors.size() != 1) {
163+
// throw new RuntimeException("Duplicate canonical w3c state codes: " + errors);
164+
// }
165+
// }
166+
}}
104167

105168
public String toState(Integer status) {
106169
if (status == null) {
@@ -211,73 +274,6 @@ public boolean isMappableError(Throwable rootCause) {
211274
return !possibleMatches.isEmpty();
212275
}
213276

214-
// Every row on this table should be self-explanatory, except for the two booleans at the end.
215-
// The first of these is "isCanonicalJsonCodeForException". This means that when doing the mapping
216-
// for a JSON Wire Protocol status code, this KnownError provides the exception that should be
217-
// thrown. The second boolean is "isCanonicalForW3C". This means that when mapping a state or
218-
// exception to a W3C state, this KnownError provides the default exception and Json Wire Protocol
219-
// status to send.
220-
private static final ImmutableSet<KnownError> KNOWN_ERRORS = ImmutableSet.<KnownError>builder()
221-
.add(new KnownError(ASYNC_SCRIPT_TIMEOUT, "script timeout", 500, ScriptTimeoutException.class, true, true))
222-
.add(new KnownError(ELEMENT_CLICK_INTERCEPTED, "element click intercepted", 400, ElementClickInterceptedException.class, true, true))
223-
.add(new KnownError(ELEMENT_NOT_INTERACTABLE, "element not interactable", 400, ElementNotInteractableException.class, true, true))
224-
.add(new KnownError(IME_ENGINE_ACTIVATION_FAILED, "unsupported operation", 500, ImeActivationFailedException.class, true, false))
225-
.add(new KnownError(IME_NOT_AVAILABLE, "unsupported operation", 500, ImeNotAvailableException.class, true, false))
226-
.add(new KnownError(INVALID_ARGUMENT, "invalid argument", 400, InvalidArgumentException.class, true, true))
227-
.add(new KnownError(INVALID_COOKIE_DOMAIN, "invalid cookie domain", 400, InvalidCookieDomainException.class, true, true))
228-
.add(new KnownError(INVALID_ELEMENT_STATE, "invalid element state", 400, InvalidElementStateException.class, true, true))
229-
.add(new KnownError(INVALID_SELECTOR_ERROR, "invalid selector", 400, InvalidSelectorException.class, true, true))
230-
.add(new KnownError(INVALID_XPATH_SELECTOR, "invalid selector", 400, InvalidSelectorException.class, false, false))
231-
.add(new KnownError(INVALID_XPATH_SELECTOR_RETURN_TYPER, "invalid selector", 400, InvalidSelectorException.class, false, true))
232-
.add(new KnownError(JAVASCRIPT_ERROR, "javascript error", 500, JavascriptException.class, true, true))
233-
.add(new KnownError(METHOD_NOT_ALLOWED, "unknown method", 405, UnsupportedCommandException.class, false, true))
234-
.add(new KnownError(METHOD_NOT_ALLOWED, "unsupported operation", 500, UnsupportedCommandException.class, false, true))
235-
.add(new KnownError(MOVE_TARGET_OUT_OF_BOUNDS, "move target out of bounds", 500, MoveTargetOutOfBoundsException.class, true, true))
236-
.add(new KnownError(NO_ALERT_PRESENT, "no such alert", 404, NoAlertPresentException.class, true, true))
237-
.add(new KnownError(NO_SUCH_COOKIE, "no such cookie", 404, NoSuchCookieException.class, true, true))
238-
.add(new KnownError(NO_SUCH_ELEMENT, "no such element", 404, NoSuchElementException.class, true, true))
239-
.add(new KnownError(NO_SUCH_FRAME, "no such frame", 404, NoSuchFrameException.class, true, true))
240-
.add(new KnownError(NO_SUCH_SESSION, "invalid session id", 404, NoSuchSessionException.class, true, true))
241-
.add(new KnownError(NO_SUCH_SHADOW_ROOT, "no such shadow root", 404, NoSuchShadowRootException.class, true, true))
242-
.add(new KnownError(NO_SUCH_WINDOW, "no such window", 404, NoSuchWindowException.class, true, true))
243-
.add(new KnownError(SESSION_NOT_CREATED, "session not created", 500, SessionNotCreatedException.class ,true, true))
244-
.add(new KnownError(STALE_ELEMENT_REFERENCE, "stale element reference", 404, StaleElementReferenceException.class, true, true))
245-
.add(new KnownError(TIMEOUT, "timeout", 500, TimeoutException.class, true, true))
246-
.add(new KnownError(XPATH_LOOKUP_ERROR, "invalid selector", 400, InvalidSelectorException.class, false, false))
247-
.add(new KnownError(UNABLE_TO_CAPTURE_SCREEN, "unable to capture screen", 500, ScreenshotException.class, true, true))
248-
.add(new KnownError(UNABLE_TO_SET_COOKIE, "unable to set cookie", 500, UnableToSetCookieException.class, true, true))
249-
.add(new KnownError(UNEXPECTED_ALERT_PRESENT, "unexpected alert open", 500, UnhandledAlertException.class, true, true))
250-
.add(new KnownError(UNHANDLED_ERROR, "unknown error", 500, WebDriverException.class, true, true))
251-
.add(new KnownError(UNKNOWN_COMMAND, "unknown command", 404, UnsupportedCommandException.class, true, true))
252-
253-
.build();
254-
255-
static {{
256-
// Validate uniqueness constraints.
257-
//
258-
// There should be only one canonical JSON Wire protocol code per exception
259-
Map<Object, Set<KnownError>> matched = new HashMap<>();
260-
for (KnownError knownError : KNOWN_ERRORS) {
261-
matched.computeIfAbsent(knownError, key -> new HashSet<>()).add(knownError);
262-
}
263-
for (Set<KnownError> errors : matched.values()) {
264-
if (errors.size() != 1) {
265-
throw new RuntimeException("Duplicate canonical exceptions: " + errors);
266-
}
267-
}
268-
269-
// There should only be one canonical W3C code to JSON Wire Protocol code
270-
// matched = new HashMap<>();
271-
// for (KnownError error : KNOWN_ERRORS) {
272-
// matched.computeIfAbsent(error.getW3cCode(), key -> new HashSet<>()).add(error);
273-
// }
274-
// for (Set<KnownError> errors : matched.values()) {
275-
// if (errors.size() != 1) {
276-
// throw new RuntimeException("Duplicate canonical w3c state codes: " + errors);
277-
// }
278-
// }
279-
}}
280-
281277
private static class KnownError {
282278
private final int jsonCode;
283279
private final String w3cCode;

java/test/org/openqa/selenium/remote/ErrorHandlerTest.java

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@
1818
package org.openqa.selenium.remote;
1919

2020
import com.google.common.collect.ImmutableMap;
21+
2122
import org.junit.jupiter.api.BeforeEach;
22-
import org.junit.jupiter.api.Test;
2323
import org.junit.jupiter.api.Tag;
24-
import org.openqa.selenium.ImeActivationFailedException;
25-
import org.openqa.selenium.ImeNotAvailableException;
24+
import org.junit.jupiter.api.Test;
2625
import org.openqa.selenium.InvalidCookieDomainException;
2726
import org.openqa.selenium.InvalidElementStateException;
2827
import org.openqa.selenium.InvalidSelectorException;
@@ -54,6 +53,22 @@
5453
public class ErrorHandlerTest {
5554
private ErrorHandler handler;
5655

56+
private static void assertStackTracesEqual(StackTraceElement[] expected, StackTraceElement[] actual) {
57+
assertThat(actual.length).as("Stacktrace length").isEqualTo(expected.length);
58+
for (int i = 0; i < expected.length; i++) {
59+
String message = "Frames at index [" + i + "]";
60+
assertThat(actual[i].getFileName()).as(message).isEqualTo(expected[i].getFileName());
61+
assertThat(actual[i].getClassName()).as(message).isEqualTo(expected[i].getClassName());
62+
assertThat(actual[i].getMethodName()).as(message).isEqualTo(expected[i].getMethodName());
63+
assertThat(actual[i].getLineNumber()).as(message).isEqualTo(expected[i].getLineNumber());
64+
}
65+
}
66+
67+
private static Map<String, Object> toMap(Object o) {
68+
String rawJson = new Json().toJson(o);
69+
return new Json().toType(rawJson, Map.class);
70+
}
71+
5772
@BeforeEach
5873
public void setUp() {
5974
handler = new ErrorHandler();
@@ -396,8 +411,6 @@ public void testStatusCodesRaisedBackToStatusMatches() {
396411
exceptions.put(ErrorCodes.UNEXPECTED_ALERT_PRESENT, UnhandledAlertException.class);
397412
exceptions.put(ErrorCodes.NO_ALERT_PRESENT, NoAlertPresentException.class);
398413
exceptions.put(ErrorCodes.ASYNC_SCRIPT_TIMEOUT, ScriptTimeoutException.class);
399-
exceptions.put(ErrorCodes.IME_NOT_AVAILABLE, ImeNotAvailableException.class);
400-
exceptions.put(ErrorCodes.IME_ENGINE_ACTIVATION_FAILED, ImeActivationFailedException.class);
401414
exceptions.put(ErrorCodes.INVALID_SELECTOR_ERROR, InvalidSelectorException.class);
402415
exceptions.put(ErrorCodes.SESSION_NOT_CREATED, SessionNotCreatedException.class);
403416
exceptions.put(ErrorCodes.MOVE_TARGET_OUT_OF_BOUNDS, MoveTargetOutOfBoundsException.class);
@@ -428,20 +441,4 @@ private Response createResponse(int status, Object value) {
428441
response.setValue(value);
429442
return response;
430443
}
431-
432-
private static void assertStackTracesEqual(StackTraceElement[] expected, StackTraceElement[] actual) {
433-
assertThat(actual.length).as("Stacktrace length").isEqualTo(expected.length);
434-
for (int i = 0; i < expected.length; i++) {
435-
String message = "Frames at index [" + i + "]";
436-
assertThat(actual[i].getFileName()).as(message).isEqualTo(expected[i].getFileName());
437-
assertThat(actual[i].getClassName()).as(message).isEqualTo(expected[i].getClassName());
438-
assertThat(actual[i].getMethodName()).as(message).isEqualTo(expected[i].getMethodName());
439-
assertThat(actual[i].getLineNumber()).as(message).isEqualTo(expected[i].getLineNumber());
440-
}
441-
}
442-
443-
private static Map<String, Object> toMap(Object o) {
444-
String rawJson = new Json().toJson(o);
445-
return new Json().toType(rawJson, Map.class);
446-
}
447444
}

0 commit comments

Comments
 (0)