Skip to content

Commit

Permalink
SQL: handle X-Pack or X-Pack SQL not being available in a more gracef…
Browse files Browse the repository at this point in the history
…ul way (#34736)

Throw a different error message for a http response code of 400, but also when the error itself is of a specific type.
  • Loading branch information
astefan committed Oct 25, 2018
1 parent 6164475 commit 8d83b2d
Showing 1 changed file with 14 additions and 0 deletions.
Expand Up @@ -47,6 +47,7 @@ public class JreHttpUrlConnection implements Closeable {
* error.
*/
public static final String SQL_STATE_BAD_SERVER = "bad_server";
private static final String SQL_NOT_AVAILABLE_ERROR_MESSAGE = "request [/_xpack/sql] contains unrecognized parameter: [mode]";

public static <R> R http(String path, String query, ConnectionConfiguration cfg, Function<JreHttpUrlConnection, R> handler) {
final URI uriPath = cfg.baseUri().resolve(path); // update path if needed
Expand Down Expand Up @@ -176,6 +177,19 @@ private <R> ResponseOrException<R> parserError() throws IOException {
}
SqlExceptionType type = SqlExceptionType.fromRemoteFailureType(failure.type());
if (type == null) {
// check if x-pack or sql are not available (x-pack not installed or sql not enabled)
// by checking the error message the server is sending back
if (con.getResponseCode() >= HttpURLConnection.HTTP_BAD_REQUEST
&& failure.reason().contains(SQL_NOT_AVAILABLE_ERROR_MESSAGE)) {
return new ResponseOrException<>(new SQLException("X-Pack/SQL do not seem to be available"
+ " on the Elasticsearch node using the access path '"
+ con.getURL().getHost()
+ (con.getURL().getPort() > 0 ? ":" + con.getURL().getPort() : "")
+ "'."
+ " Please verify X-Pack is installed and SQL enabled. Alternatively, check if any proxy is interfering"
+ " the communication to Elasticsearch",
SQL_STATE_BAD_SERVER));
}
return new ResponseOrException<>(new SQLException("Server sent bad type ["
+ failure.type() + "]. Original type was [" + failure.reason() + "]. ["
+ failure.remoteTrace() + "]", SQL_STATE_BAD_SERVER));
Expand Down

0 comments on commit 8d83b2d

Please sign in to comment.