Skip to content

Commit 4959695

Browse files
committed
Modified logic of ErrorHandler mapping to *Exception.
Add logic to try to resolve *Exception class in case RemoteWebDriver doesn’t use a Selenium Java Error Classes. Fixes #7437.
1 parent fc79624 commit 4959695

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

java/client/src/org/openqa/selenium/remote/ErrorHandler.java

+22-12
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public Response throwIfResponseFailed(Response response, long duration) throws R
115115
message = String.valueOf(e);
116116
}
117117

118-
Throwable serverError = rebuildServerError(rawErrorData);
118+
Throwable serverError = rebuildServerError(rawErrorData, response.getStatus());
119119

120120
// If serverError is null, then the server did not provide a className (only expected if
121121
// the server is a Java process) or a stack trace. The lack of a className is OK, but
@@ -141,7 +141,7 @@ public Response throwIfResponseFailed(Response response, long duration) throws R
141141

142142
String duration1 = duration(duration);
143143

144-
if (message != null && message.indexOf(duration1) == -1) {
144+
if (message != null && !message.contains(duration1)) {
145145
message = message + duration1;
146146
}
147147

@@ -216,7 +216,7 @@ private <T extends Throwable> T createThrowable(
216216
return null;
217217
}
218218

219-
private Throwable rebuildServerError(Map<String, Object> rawErrorData) {
219+
private Throwable rebuildServerError(Map<String, Object> rawErrorData, int responseStatus) {
220220

221221
if (!rawErrorData.containsKey(CLASS) && !rawErrorData.containsKey(STACK_TRACE)) {
222222
// Not enough information for us to try to rebuild an error.
@@ -225,24 +225,34 @@ private Throwable rebuildServerError(Map<String, Object> rawErrorData) {
225225

226226
Throwable toReturn = null;
227227
String message = (String) rawErrorData.get(MESSAGE);
228+
Class clazz = null;
228229

230+
// First: allow Remote Driver to specify the Selenium Server internal exception
229231
if (rawErrorData.containsKey(CLASS)) {
230232
String className = (String) rawErrorData.get(CLASS);
231233
try {
232-
Class clazz = Class.forName(className);
233-
if (clazz.equals(UnhandledAlertException.class)) {
234-
toReturn = createUnhandledAlertException(rawErrorData);
235-
} else if (Throwable.class.isAssignableFrom(clazz)) {
236-
@SuppressWarnings({"unchecked"})
237-
Class<? extends Throwable> throwableType = (Class<? extends Throwable>) clazz;
238-
toReturn = createThrowable(throwableType, new Class<?>[] {String.class},
239-
new Object[] {message});
240-
}
234+
clazz = Class.forName(className);
241235
} catch (ClassNotFoundException ignored) {
242236
// Ok, fall-through
243237
}
244238
}
245239

240+
// If the above fails, map Response Status to Exception class
241+
if (null == clazz) {
242+
clazz = errorCodes.getExceptionType(responseStatus);
243+
}
244+
245+
if (clazz.equals(UnhandledAlertException.class)) {
246+
toReturn = createUnhandledAlertException(rawErrorData);
247+
} else if (Throwable.class.isAssignableFrom(clazz)) {
248+
@SuppressWarnings({"unchecked"})
249+
Class<? extends Throwable> throwableType = (Class<? extends Throwable>) clazz;
250+
toReturn = createThrowable(
251+
throwableType,
252+
new Class<?>[] {String.class},
253+
new Object[] {message});
254+
}
255+
246256
if (toReturn == null) {
247257
toReturn = new UnknownServerException(message);
248258
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ public void testShouldIncludeScreenshotIfProvided() throws Exception {
249249

250250
@SuppressWarnings({"unchecked", "ThrowableInstanceNeverThrown"})
251251
@Test
252-
public void testShouldDefaultToUnknownServerErrorIfClassIsNotSpecified()
252+
public void testShouldDefaultToWebDriverExceptionIfClassIsNotSpecified()
253253
throws Exception {
254254
RuntimeException serverError = new RuntimeException("foo bar baz!");
255255
Map<String, Object> data = toMap(serverError);
@@ -264,7 +264,7 @@ public void testShouldDefaultToUnknownServerErrorIfClassIsNotSpecified()
264264

265265
Throwable cause = expected.getCause();
266266
assertNotNull(cause);
267-
assertEquals(ErrorHandler.UnknownServerException.class, cause.getClass());
267+
assertEquals(WebDriverException.class, cause.getClass());
268268
assertEquals(new WebDriverException(serverError.getMessage()).getMessage(),
269269
cause.getMessage());
270270
assertStackTracesEqual(serverError.getStackTrace(), cause.getStackTrace());
@@ -273,7 +273,7 @@ public void testShouldDefaultToUnknownServerErrorIfClassIsNotSpecified()
273273

274274
@SuppressWarnings({"unchecked", "ThrowableInstanceNeverThrown"})
275275
@Test
276-
public void testShouldStillTryToBuildServerErrorIfClassIsNotProvidedAndStackTraceIsNotForJava() {
276+
public void testShouldStillTryToBuildWebDriverExceptionIfClassIsNotProvidedAndStackTraceIsNotForJava() {
277277
Map<String, ?> data = ImmutableMap.of(
278278
"message", "some error message",
279279
"stackTrace", Lists.newArrayList(
@@ -297,7 +297,7 @@ public void testShouldStillTryToBuildServerErrorIfClassIsNotProvidedAndStackTrac
297297

298298
Throwable cause = expected.getCause();
299299
assertNotNull(cause);
300-
assertEquals(ErrorHandler.UnknownServerException.class, cause.getClass());
300+
assertEquals(WebDriverException.class, cause.getClass());
301301
assertEquals(helper.getMessage(),
302302
cause.getMessage());
303303

0 commit comments

Comments
 (0)