Skip to content

Commit

Permalink
[java] Improving error message if a remote side responses with code 4…
Browse files Browse the repository at this point in the history
…05 Method Not Allowed
  • Loading branch information
barancev committed Nov 17, 2019
1 parent 39b8c3e commit 6a9c546
Showing 1 changed file with 35 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import static com.google.common.base.Strings.nullToEmpty;
import static com.google.common.net.HttpHeaders.CONTENT_TYPE;
import static java.net.HttpURLConnection.HTTP_BAD_METHOD;
import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
import static java.net.HttpURLConnection.HTTP_OK;
import static org.openqa.selenium.json.Json.MAP_TYPE;
Expand Down Expand Up @@ -87,42 +88,46 @@ public Response decode(HttpResponse encodedResponse) {
// {"error":"no such alert","message":"No tab modal was open when attempting to get the dialog text"}
if (HTTP_OK != encodedResponse.getStatus()) {
log.fine("Processing an error");
Map<String, Object> obj = json.toType(content, MAP_TYPE);

if (HTTP_BAD_METHOD == encodedResponse.getStatus()) {
response.setStatus(ErrorCodes.UNKNOWN_COMMAND);
response.setValue(content);
} else {
Map<String, Object> obj = json.toType(content, MAP_TYPE);

Object w3cWrappedValue = obj.get("value");
if (w3cWrappedValue instanceof Map && ((Map<?, ?>) w3cWrappedValue).containsKey("error")) {
//noinspection unchecked
obj = (Map<String, Object>) w3cWrappedValue;
}
Object w3cWrappedValue = obj.get("value");
if (w3cWrappedValue instanceof Map && ((Map<?, ?>) w3cWrappedValue).containsKey("error")) {
//noinspection unchecked
obj = (Map<String, Object>) w3cWrappedValue;
}

String message = "An unknown error has occurred";
if (obj.get("message") instanceof String) {
message = (String) obj.get("message");
}
String message = "An unknown error has occurred";
if (obj.get("message") instanceof String) {
message = (String) obj.get("message");
}

String error = "unknown error";
if (obj.get("error") instanceof String) {
error = (String) obj.get("error");
}
String error = "unknown error";
if (obj.get("error") instanceof String) {
error = (String) obj.get("error");
}

response.setState(error);
response.setStatus(errorCodes.toStatus(error, Optional.of(encodedResponse.getStatus())));

// For now, we'll inelegantly special case unhandled alerts.
if ("unexpected alert open".equals(error) &&
HTTP_INTERNAL_ERROR == encodedResponse.getStatus()) {
String text = "";
Object data = obj.get("data");
if (data != null) {
Object rawText = ((Map<?, ?>) data).get("text");
if (rawText instanceof String) {
text = (String) rawText;
response.setState(error);
response.setStatus(errorCodes.toStatus(error, Optional.of(encodedResponse.getStatus())));

// For now, we'll inelegantly special case unhandled alerts.
if ("unexpected alert open".equals(error) &&
HTTP_INTERNAL_ERROR == encodedResponse.getStatus()) {
String text = "";
Object data = obj.get("data");
if (data != null) {
Object rawText = ((Map<?, ?>) data).get("text");
if (rawText instanceof String) {
text = (String) rawText;
}
}
response.setValue(new UnhandledAlertException(message, text));
} else {
response.setValue(createException(error, message));
}
response.setValue(new UnhandledAlertException(message, text));
} else {
response.setValue(createException(error, message));
}
return response;
}
Expand Down

0 comments on commit 6a9c546

Please sign in to comment.