Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions java/src/org/openqa/selenium/remote/ErrorCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@

package org.openqa.selenium.remote;

import static java.util.Objects.requireNonNullElse;
import static java.util.Objects.requireNonNullElseGet;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import org.openqa.selenium.DetachedShadowRootException;
import org.openqa.selenium.ElementClickInterceptedException;
Expand Down Expand Up @@ -88,7 +92,7 @@ public class ErrorCodec {
new W3CError<>("unsupported operation", UnsupportedCommandException::new, 500),
new W3CError<>("unknown command", UnsupportedCommandException::new, 404),
new W3CError<>("unknown method", UnsupportedCommandException::new, 405),
new W3CError<>("unknown error", WebDriverException::new, 500));
DEFAULT_ERROR);

private ErrorCodec() {
// This will switch to being an interface at some point. Use `createDefault`
Expand Down Expand Up @@ -144,24 +148,28 @@ public int getHttpStatusCode(Throwable throwable) {

public WebDriverException decode(Map<String, Object> response) {
if (!(response.get("value") instanceof Map)) {
throw new IllegalArgumentException("Unable to find mapping for " + response);
throw new InvalidResponseException("missing \"value\" field", response);
}

Map<?, ?> value = (Map<?, ?>) response.get("value");
if (!(value.get("error") instanceof String)) {
throw new IllegalArgumentException("Unable to find mapping for " + response);
}

String error = (String) value.get("error");
String message = value.get("message") instanceof String ? (String) value.get("message") : null;
Object error = requireNonNullElse(value.get("error"), "");
Object message = requireNonNullElseGet(value.get("message"), response::toString);

W3CError<?> w3CError =
ERRORS.stream()
.filter(err -> error.equals(err.w3cErrorString))
.findFirst()
.orElse(DEFAULT_ERROR);
if (!(error instanceof String)) {
throw new InvalidResponseException("\"error\" field must be a string", response);
}
if (!(message instanceof String)) {
throw new InvalidResponseException("\"message\" field must be a string", response);
}

return w3CError.exceptionConstructor.apply(message);
Optional<W3CError<? extends WebDriverException>> w3CError =
ERRORS.stream().filter(err -> error.equals(err.w3cErrorString)).findFirst();
if (w3CError.isPresent()) {
return w3CError.get().exceptionConstructor.apply((String) message);
}
String extendedMessage = String.format("%s (error code: \"%s\")", message, error);
return DEFAULT_ERROR.exceptionConstructor.apply(extendedMessage);
}

private W3CError<? extends WebDriverException> fromThrowable(Throwable throwable) {
Expand Down
30 changes: 30 additions & 0 deletions java/src/org/openqa/selenium/remote/InvalidResponseException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.remote;

import java.util.Map;
import org.jspecify.annotations.NullMarked;

@NullMarked
public class InvalidResponseException extends IllegalArgumentException {
private static final String w3cErrorFormat = "https://www.w3.org/TR/webdriver2/#errors";

public InvalidResponseException(String message, Map<String, Object> response) {
super(String.format("%s: %s%nSee %s", message, response, w3cErrorFormat));
}
}
50 changes: 47 additions & 3 deletions java/test/org/openqa/selenium/remote/ErrorCodecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@

package org.openqa.selenium.remote;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.text.ParseException;
import java.util.Map;
import java.util.UUID;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.DetachedShadowRootException;
Expand Down Expand Up @@ -119,11 +121,53 @@ void encodeCommonErrors() {
encodeAndCheck(new ClassNotFoundException(msg), "unknown error", msg);
}

@Test
void decode_w3cCompliantResponse() {
assertThat(
errorCodec.decode(
Map.of(
"value",
Map.of(
"error", "script timeout",
"message", "Script timed out"))))
.isInstanceOf(ScriptTimeoutException.class)
.hasMessageStartingWith("Script timed out");
}

@Test
void decode_unknownError() {
assertThat(
errorCodec.decode(
Map.of(
"value",
Map.of(
"error", "some.magic.error.code",
"message", "Something unbelievable happened"))))
.isInstanceOf(WebDriverException.class)
.hasMessageStartingWith("Something unbelievable happened")
.hasMessageContaining("some.magic.error.code");
}

@Test
void decode_noValue() {
assertThatThrownBy(() -> errorCodec.decode(Map.of("unexpected", "format")))
.isInstanceOf(InvalidResponseException.class)
.hasMessageStartingWith("missing \"value\" field: {unexpected=format}")
.hasMessageContaining("See https://www.w3.org/TR/webdriver2/#errors");
}

@Test
void decode_noError() {
assertThat(errorCodec.decode(Map.of("value", Map.of("message", "Session was not created"))))
.isInstanceOf(WebDriverException.class)
.hasMessageStartingWith("Session was not created");
}

void encodeAndCheck(Throwable toEncode, String expectedType, String expectedMessage) {
Map<String, Object> encoded = errorCodec.encode(toEncode);
Map<String, Object> value = (Map<String, Object>) encoded.get("value");

Assertions.assertThat(value.get("error")).isEqualTo(expectedType);
Assertions.assertThat(value.get("message")).isEqualTo(toEncode.getMessage());
assertThat(value.get("error")).isEqualTo(expectedType);
assertThat(value.get("message")).isEqualTo(toEncode.getMessage());
}
}