Skip to content

Commit 681059e

Browse files
committed
[java] Reducing duplication of code that populates WebDriverException
1 parent 5b190ec commit 681059e

2 files changed

Lines changed: 25 additions & 35 deletions

File tree

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

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -527,58 +527,48 @@ protected Response execute(CommandPayload payload) {
527527
// Unwrap the response value by converting any JSON objects of the form
528528
// {"ELEMENT": id} to RemoteWebElements.
529529
Object value = getElementConverter().apply(response.getValue());
530-
if (value instanceof WebDriverException) {
531-
((WebDriverException) value).addInfo("Command", command.toString());
532-
}
533530
response.setValue(value);
534531
} catch (Throwable e) {
535532
log(sessionId, command.getName(), command, When.EXCEPTION);
533+
WebDriverException toThrow;
536534
if (command.getName().equals(DriverCommand.NEW_SESSION)) {
537-
throw new SessionNotCreatedException(
538-
"Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.",
535+
toThrow = new SessionNotCreatedException(
536+
"Could not start a new session. Possible causes are invalid address"
537+
+ " of the remote server or browser start-up failure.",
539538
e);
539+
} else if (e instanceof WebDriverException) {
540+
toThrow = (WebDriverException) e;
540541
} else {
541-
WebDriverException toThrow =
542-
e instanceof WebDriverException
543-
? (WebDriverException) e
544-
: new UnreachableBrowserException(
545-
"Error communicating with the remote browser. It may have died.", e);
546-
toThrow.addInfo("Command", command.toString());
547-
if (getSessionId() != null) {
548-
toThrow.addInfo(WebDriverException.SESSION_ID, getSessionId().toString());
549-
}
550-
if (getCapabilities() != null) {
551-
toThrow.addInfo("Capabilities", getCapabilities().toString());
552-
}
553-
throw toThrow;
542+
toThrow = new UnreachableBrowserException(
543+
"Error communicating with the remote browser. It may have died.", e);
554544
}
545+
populateWebDriverException(toThrow);
546+
toThrow.addInfo("Command", command.toString());
547+
throw toThrow;
555548
} finally {
556549
Thread.currentThread().setName(currentName);
557550
}
558551

559552
try {
560553
errorHandler.throwIfResponseFailed(response, System.currentTimeMillis() - start);
561554
} catch (WebDriverException ex) {
562-
if (command.getParameters() != null && command.getParameters().containsKey("using") && command.getParameters().containsKey("value")) {
563-
ex.addInfo(
564-
"*** Element info",
565-
String.format(
566-
"{Using=%s, value=%s}",
567-
command.getParameters().get("using"),
568-
command.getParameters().get("value")));
569-
}
570-
ex.addInfo(WebDriverException.DRIVER_INFO, this.getClass().getName());
571-
if (getSessionId() != null) {
572-
ex.addInfo(WebDriverException.SESSION_ID, getSessionId().toString());
573-
}
574-
if (getCapabilities() != null) {
575-
ex.addInfo("Capabilities", getCapabilities().toString());
576-
}
555+
populateWebDriverException(ex);
556+
ex.addInfo("Command", command.toString());
577557
throw ex;
578558
}
579559
return response;
580560
}
581561

562+
private void populateWebDriverException(WebDriverException ex) {
563+
ex.addInfo(WebDriverException.DRIVER_INFO, this.getClass().getName());
564+
if (getSessionId() != null) {
565+
ex.addInfo(WebDriverException.SESSION_ID, getSessionId().toString());
566+
}
567+
if (getCapabilities() != null) {
568+
ex.addInfo("Capabilities", getCapabilities().toString());
569+
}
570+
}
571+
582572
protected Response execute(String driverCommand, Map<String, ?> parameters) {
583573
return execute(new CommandPayload(driverCommand, parameters));
584574
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ public void canHandleWebDriverExceptionThrownByCommandExecutor() {
929929
.withMessageStartingWith("BOOM!!!")
930930
.withMessageContaining("Build info: ")
931931
.withMessageContaining(
932-
"Driver info: driver.version: RemoteWebDriver")
932+
"Driver info: org.openqa.selenium.remote.RemoteWebDriver")
933933
.withMessageContaining(String.format(
934934
"Session ID: %s", driver.getSessionId()))
935935
.withMessageContaining(String.format(
@@ -961,7 +961,7 @@ public void canHandleGeneralExceptionThrownByCommandExecutor() {
961961
.withMessageStartingWith("Error communicating with the remote browser. It may have died.")
962962
.withMessageContaining("Build info: ")
963963
.withMessageContaining(
964-
"Driver info: driver.version: RemoteWebDriver")
964+
"Driver info: org.openqa.selenium.remote.RemoteWebDriver")
965965
.withMessageContaining(String.format(
966966
"Session ID: %s", driver.getSessionId()))
967967
.withMessageContaining(String.format(

0 commit comments

Comments
 (0)