Skip to content

Commit 8d83b2d

Browse files
committed
SQL: handle X-Pack or X-Pack SQL not being available in a more graceful 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.
1 parent 6164475 commit 8d83b2d

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/JreHttpUrlConnection.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public class JreHttpUrlConnection implements Closeable {
4747
* error.
4848
*/
4949
public static final String SQL_STATE_BAD_SERVER = "bad_server";
50+
private static final String SQL_NOT_AVAILABLE_ERROR_MESSAGE = "request [/_xpack/sql] contains unrecognized parameter: [mode]";
5051

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

0 commit comments

Comments
 (0)