From 6a9c5463ccdf245a4f5a8f51f627b26bbdc3b4b2 Mon Sep 17 00:00:00 2001 From: Alexei Barantsev Date: Sun, 17 Nov 2019 15:40:40 +0300 Subject: [PATCH] [java] Improving error message if a remote side responses with code 405 Method Not Allowed --- .../codec/w3c/W3CHttpResponseCodec.java | 65 ++++++++++--------- 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/java/client/src/org/openqa/selenium/remote/codec/w3c/W3CHttpResponseCodec.java b/java/client/src/org/openqa/selenium/remote/codec/w3c/W3CHttpResponseCodec.java index 726c6760550ea..6ea0eb1b57ea2 100644 --- a/java/client/src/org/openqa/selenium/remote/codec/w3c/W3CHttpResponseCodec.java +++ b/java/client/src/org/openqa/selenium/remote/codec/w3c/W3CHttpResponseCodec.java @@ -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; @@ -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 obj = json.toType(content, MAP_TYPE); - + if (HTTP_BAD_METHOD == encodedResponse.getStatus()) { + response.setStatus(ErrorCodes.UNKNOWN_COMMAND); + response.setValue(content); + } else { + Map obj = json.toType(content, MAP_TYPE); - Object w3cWrappedValue = obj.get("value"); - if (w3cWrappedValue instanceof Map && ((Map) w3cWrappedValue).containsKey("error")) { - //noinspection unchecked - obj = (Map) w3cWrappedValue; - } + Object w3cWrappedValue = obj.get("value"); + if (w3cWrappedValue instanceof Map && ((Map) w3cWrappedValue).containsKey("error")) { + //noinspection unchecked + obj = (Map) 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; }