Skip to content

Commit

Permalink
[java] use the W3C state to detect errors
Browse files Browse the repository at this point in the history
  • Loading branch information
joerg1985 committed Jan 26, 2024
1 parent 4ffaab1 commit fb062a5
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion java/src/org/openqa/selenium/remote/ErrorCodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public class ErrorCodes {
405,
UnsupportedCommandException.class,
false,
true),
false),
new KnownError(
METHOD_NOT_ALLOWED,
"unsupported operation",
Expand Down
4 changes: 1 addition & 3 deletions java/src/org/openqa/selenium/remote/ErrorHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

package org.openqa.selenium.remote;

import static org.openqa.selenium.remote.ErrorCodes.SUCCESS;

import java.lang.reflect.Constructor;
import java.math.BigDecimal;
import java.math.RoundingMode;
Expand Down Expand Up @@ -82,7 +80,7 @@ public void setIncludeServerErrors(boolean includeServerErrors) {

@SuppressWarnings("unchecked")
public Response throwIfResponseFailed(Response response, long duration) throws RuntimeException {
if (response.getStatus() == null || response.getStatus() == SUCCESS) {
if ("success".equals(response.getState())) {
return response;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,12 @@ public Response decode(HttpResponse encodedResponse) {
if (!encodedResponse.isSuccessful()) {
LOG.fine("Processing an error");
if (HTTP_BAD_METHOD == encodedResponse.getStatus()) {
response.setState("unknown command");
response.setStatus(ErrorCodes.UNKNOWN_COMMAND);
response.setValue(content);
} else if (HTTP_GATEWAY_TIMEOUT == encodedResponse.getStatus()
|| HTTP_BAD_GATEWAY == encodedResponse.getStatus()) {
response.setState("unknown error");
response.setStatus(ErrorCodes.UNHANDLED_ERROR);
response.setValue(content);
} else {
Expand Down
2 changes: 2 additions & 0 deletions java/test/org/openqa/selenium/remote/AugmenterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ protected StubExecutor(Capabilities capabilities) {
public Response execute(Command command) {
if (DriverCommand.NEW_SESSION.equals(command.getName())) {
Response response = new Response(new SessionId("foo"));
response.setState("success");
response.setValue(capabilities.asMap());
return response;
}
Expand All @@ -316,6 +317,7 @@ public Response execute(Command command) {
if (possibleMatch.commandName.equals(command.getName())
&& possibleMatch.args.equals(command.getParameters())) {
Response response = new Response(new SessionId("foo"));
response.setState("success");
response.setValue(possibleMatch.returnValue);
return response;
}
Expand Down
1 change: 1 addition & 0 deletions java/test/org/openqa/selenium/remote/ErrorHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ private Response createResponse(int status) {
private Response createResponse(int status, Object value) {
Response response = new Response();
response.setStatus(status);
response.setState(new ErrorCodes().toState(status));
response.setValue(value);
return response;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ void canHandleUnknownPlatformNameAndFallsBackToUnix() {
void canHandleNonStandardCapabilitiesReturnedByRemoteEnd() throws IOException {
Response resp = new Response();
resp.setSessionId(UUID.randomUUID().toString());
resp.setState("success");
resp.setValue(ImmutableMap.of("platformName", "xxx"));
CommandExecutor executor = mock(CommandExecutor.class);
when(executor.execute(any())).thenReturn(resp);
Expand Down
2 changes: 2 additions & 0 deletions java/test/org/openqa/selenium/remote/WebDriverFixture.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ private boolean areEqual(Object left, Object right) {
public static Function<Command, Response> valueResponder(Object value) {
return cmd -> {
Response response = new Response();
response.setState("success");
response.setValue(value);
response.setSessionId(cmd.getSessionId() != null ? cmd.getSessionId().toString() : null);
return response;
Expand All @@ -181,6 +182,7 @@ public static Function<Command, Response> errorResponder(String state, Object va
Collection<Capabilities> capabilities =
(Collection<Capabilities>) cmd.getParameters().get("capabilities");

response.setState("success");
response.setValue(
capabilities.iterator().next().asMap().entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().toString())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public void createMocks() {
void shouldIncludeRemoteInfoForWrappedDriverTimeout() throws IOException {
Capabilities caps = new MutableCapabilities();
Response response = new Response(new SessionId("foo"));
response.setState("success");
response.setValue(caps.asMap());
CommandExecutor executor = mock(CommandExecutor.class);
when(executor.execute(any(Command.class))).thenReturn(response);
Expand Down

0 comments on commit fb062a5

Please sign in to comment.