Skip to content

Commit 1cdf5b4

Browse files
committed
[java] use the ErrorCodec to build the WebDriverException #13580
1 parent a72ab32 commit 1cdf5b4

File tree

3 files changed

+8
-17
lines changed

3 files changed

+8
-17
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public class ErrorCodec {
8585
new W3CError("unable to capture screen", ScreenshotException.class, 500),
8686
new W3CError("unable to set cookie", UnableToSetCookieException.class, 500),
8787
new W3CError("unexpected alert open", UnhandledAlertException.class, 500),
88-
new W3CError("unsupported operation", UnsupportedCommandException.class, 404),
88+
new W3CError("unsupported operation", UnsupportedCommandException.class, 500),
8989
new W3CError("unknown command", UnsupportedCommandException.class, 404),
9090
new W3CError("unknown method", UnsupportedCommandException.class, 405),
9191
new W3CError("unknown error", WebDriverException.class, 500));

java/src/org/openqa/selenium/remote/codec/w3c/W3CHttpResponseCodec.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import static org.openqa.selenium.json.Json.OBJECT_TYPE;
2626
import static org.openqa.selenium.remote.http.Contents.string;
2727

28-
import java.lang.reflect.Constructor;
2928
import java.util.Map;
3029
import java.util.Objects;
3130
import java.util.Optional;
@@ -99,12 +98,15 @@ public Response decode(HttpResponse encodedResponse) {
9998
response.setStatus(ErrorCodes.UNHANDLED_ERROR);
10099
response.setValue(content);
101100
} else {
102-
Map<String, Object> obj = json.toType(content, MAP_TYPE);
101+
Map<String, Object> org = json.toType(content, MAP_TYPE);
102+
Map<String, Object> obj;
103103

104-
Object w3cWrappedValue = obj.get("value");
104+
Object w3cWrappedValue = org.get("value");
105105
if (w3cWrappedValue instanceof Map && ((Map<?, ?>) w3cWrappedValue).containsKey("error")) {
106106
//noinspection unchecked
107107
obj = (Map<String, Object>) w3cWrappedValue;
108+
} else {
109+
obj = org;
108110
}
109111

110112
String message = "An unknown error has occurred";
@@ -133,7 +135,7 @@ public Response decode(HttpResponse encodedResponse) {
133135
}
134136
response.setValue(new UnhandledAlertException(message, text));
135137
} else {
136-
response.setValue(createException(error, message));
138+
response.setValue(errorCodec.decode(org));
137139
}
138140
}
139141
return response;
@@ -178,15 +180,4 @@ protected Response reconstructValue(Response response) {
178180
response.setValue(elementConverter.apply(response.getValue()));
179181
return response;
180182
}
181-
182-
private WebDriverException createException(String error, String message) {
183-
Class<? extends WebDriverException> clazz = errorCodes.getExceptionType(error);
184-
185-
try {
186-
Constructor<? extends WebDriverException> constructor = clazz.getConstructor(String.class);
187-
return constructor.newInstance(message);
188-
} catch (ReflectiveOperationException e) {
189-
throw new WebDriverException(message);
190-
}
191-
}
192183
}

java/test/org/openqa/selenium/remote/codec/w3c/W3CHttpResponseCodecTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ void decodingAnErrorWithoutAStacktraceIsDecodedProperlyForNonCompliantImplementa
115115
error.put("message", "I like peas");
116116
error.put("stacktrace", "");
117117

118-
HttpResponse response = createValidResponse(HTTP_INTERNAL_ERROR, error);
118+
HttpResponse response = createValidResponse(HTTP_INTERNAL_ERROR, Map.of("value", error));
119119

120120
Response decoded = new W3CHttpResponseCodec().decode(response);
121121

0 commit comments

Comments
 (0)