SQL: JdbcException clean up (elastic/x-pack-elasticsearch#2637)
Original commit: elastic/x-pack-elasticsearch@bcd0c59b37
This commit is contained in:
parent
c33dfe7dbe
commit
6bae50a228
|
@ -142,7 +142,7 @@ public final class Debug {
|
|||
OUTPUT_CACHE.put(out, log);
|
||||
OUTPUT_REFS.put(out, Integer.valueOf(0));
|
||||
} catch (Exception ex) {
|
||||
throw new JdbcException(ex, "Cannot open debug output %s", out);
|
||||
throw new JdbcException(ex, "Cannot open debug output [" + out + "]");
|
||||
}
|
||||
}
|
||||
OUTPUT_REFS.put(out, Integer.valueOf(OUTPUT_REFS.get(out).intValue() + 1));
|
||||
|
|
|
@ -60,7 +60,7 @@ abstract class DebuggingInvoker implements InvocationHandler {
|
|||
} catch (Exception ex) {
|
||||
// should not occur
|
||||
log.logException(method, args, ex);
|
||||
throw new JdbcException(ex, "Debugging failed for %s ", method);
|
||||
throw new JdbcException(ex, "Debugging failed for [" + method + "]");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
*/
|
||||
package org.elasticsearch.xpack.sql.jdbc.jdbc;
|
||||
|
||||
import org.elasticsearch.xpack.sql.jdbc.util.Assert;
|
||||
import org.elasticsearch.xpack.sql.net.client.ConnectionConfiguration;
|
||||
import org.elasticsearch.xpack.sql.net.client.util.StringUtils;
|
||||
|
||||
|
@ -77,7 +76,7 @@ public class JdbcConfiguration extends ConnectionConfiguration {
|
|||
String url = u;
|
||||
String format = "jdbc:es://[host[:port]]*/[prefix]*[?[option=value]&]*";
|
||||
if (!canAccept(u)) {
|
||||
throw new JdbcException("Expected %s url, received %s", URL_PREFIX, u);
|
||||
throw new JdbcException("Expected [" + URL_PREFIX + "] url, received [" + u +"]");
|
||||
}
|
||||
|
||||
try {
|
||||
|
@ -94,7 +93,7 @@ public class JdbcConfiguration extends ConnectionConfiguration {
|
|||
u = u.substring(URL_PREFIX.length(), u.length());
|
||||
|
||||
if (!u.startsWith("//")) {
|
||||
throw new JdbcException("Invalid URL %s, format should be %s", url, format);
|
||||
throw new JdbcException("Invalid URL [" + url + "], format should be [" + format + "]");
|
||||
}
|
||||
|
||||
// remove //
|
||||
|
@ -110,7 +109,7 @@ public class JdbcConfiguration extends ConnectionConfiguration {
|
|||
int pIndex = u.indexOf("?");
|
||||
if (pIndex > 0) {
|
||||
if (index < 0) {
|
||||
throw new JdbcException("Invalid URL %s, format should be %s", url, format);
|
||||
throw new JdbcException("Invalid URL [" + url + "], format should be [" + format + "]");
|
||||
}
|
||||
if (pIndex + 1 < u.length()) {
|
||||
params = u.substring(pIndex + 1);
|
||||
|
@ -155,10 +154,12 @@ public class JdbcConfiguration extends ConnectionConfiguration {
|
|||
List<String> prms = StringUtils.tokenize(params, "&");
|
||||
for (String param : prms) {
|
||||
List<String> args = StringUtils.tokenize(param, "=");
|
||||
Assert.isTrue(args.size() == 2, "Invalid parameter %s, format needs to be key=value", param);
|
||||
if (args.size() != 2) {
|
||||
throw new JdbcException("Invalid parameter [" + param + "], format needs to be key=value");
|
||||
}
|
||||
String pName = args.get(0);
|
||||
if (!KNOWN_OPTIONS.contains(pName)) {
|
||||
throw new JdbcException("Unknown parameter [%s] ; did you mean %s", pName,
|
||||
throw new JdbcException("Unknown parameter [" + pName + "] ; did you mean " +
|
||||
StringUtils.findSimiliar(pName, KNOWN_OPTIONS));
|
||||
}
|
||||
|
||||
|
@ -178,7 +179,7 @@ public class JdbcConfiguration extends ConnectionConfiguration {
|
|||
try {
|
||||
return new URL(isSSLEnabled() ? "https" : "http", hostAndPort.ip, port(), urlFile);
|
||||
} catch (MalformedURLException ex) {
|
||||
throw new JdbcException(ex, "Cannot connect to server %s", originalUrl);
|
||||
throw new JdbcException(ex, "Cannot connect to server [" + originalUrl + "]");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -5,17 +5,12 @@
|
|||
*/
|
||||
package org.elasticsearch.xpack.sql.jdbc.jdbc;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import static java.lang.String.format;
|
||||
|
||||
public class JdbcException extends RuntimeException {
|
||||
public JdbcException(String message, Object... args) {
|
||||
// NOCOMMIT we very rarely use this on new classes in core, instead appending strings.
|
||||
super(format(Locale.ROOT, message, args));
|
||||
public JdbcException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public JdbcException(Throwable cause, String message, Object... args) {
|
||||
super(format(Locale.ROOT, message, args), cause);
|
||||
public JdbcException(Throwable cause, String message) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -119,7 +119,7 @@ public abstract class JdbcUtils {
|
|||
return DECIMAL;
|
||||
}
|
||||
|
||||
throw new JdbcException("Unrecognized class %s", clazz);
|
||||
throw new JdbcException("Unrecognized class [" + clazz + "]");
|
||||
}
|
||||
|
||||
static boolean isSigned(int type) {
|
||||
|
|
|
@ -32,14 +32,14 @@ class PreparedQuery {
|
|||
|
||||
ParamInfo getParam(int param) {
|
||||
if (param < 1 || param > params.length) {
|
||||
throw new JdbcException("Invalid parameter index %s", param);
|
||||
throw new JdbcException("Invalid parameter index [" + param + "]");
|
||||
}
|
||||
return params[param - 1];
|
||||
}
|
||||
|
||||
void setParam(int param, Object value, JDBCType type) {
|
||||
if (param < 1 || param > params.length) {
|
||||
throw new JdbcException("Invalid parameter index %s", param);
|
||||
throw new JdbcException("Invalid parameter index [" + param + "]");
|
||||
}
|
||||
params[param - 1].value = value;
|
||||
params[param - 1].type = type;
|
||||
|
|
|
@ -44,7 +44,7 @@ class HttpClient {
|
|||
try {
|
||||
return new URL(baseUrl, subPath);
|
||||
} catch (MalformedURLException ex) {
|
||||
throw new JdbcException(ex, "Invalid subpath %s", subPath);
|
||||
throw new JdbcException(ex, "Invalid subpath [" + subPath + "]");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -136,7 +136,7 @@ public class JdbcHttpClient implements Closeable {
|
|||
}
|
||||
if (response.responseType() == ResponseType.ERROR) {
|
||||
ErrorResponse error = (ErrorResponse) response;
|
||||
throw new JdbcException("Server returned error: %s", error.stack);
|
||||
throw new JdbcException("Server returned error: [" + error.stack + "]");
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@ abstract class ArrayUtils {
|
|||
|
||||
if (minTargetSize < 0) {
|
||||
// catch usage that accidentally overflows int
|
||||
throw new JdbcException("invalid array size %d", minTargetSize);
|
||||
throw new JdbcException("invalid array size [" + minTargetSize + "]");
|
||||
}
|
||||
|
||||
if (minTargetSize == 0) {
|
||||
|
|
|
@ -1,48 +0,0 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
package org.elasticsearch.xpack.sql.jdbc.util;
|
||||
|
||||
import org.elasticsearch.xpack.sql.jdbc.jdbc.JdbcException;
|
||||
import org.elasticsearch.xpack.sql.net.client.util.StringUtils;
|
||||
|
||||
public class Assert {
|
||||
|
||||
public static void hasText(CharSequence sequence, String message) {
|
||||
if (!StringUtils.hasText(sequence)) {
|
||||
throw new JdbcException(message);
|
||||
}
|
||||
}
|
||||
|
||||
public static void hasText(CharSequence sequence, String message, Object... values) {
|
||||
if (!StringUtils.hasText(sequence)) {
|
||||
throw new JdbcException(message, values);
|
||||
}
|
||||
}
|
||||
|
||||
public static void isTrue(boolean expression, String message, Object... values) {
|
||||
if (!expression) {
|
||||
throw new JdbcException(message, values);
|
||||
}
|
||||
}
|
||||
|
||||
public static void isTrue(boolean expression, String message) {
|
||||
if (!expression) {
|
||||
throw new JdbcException(message);
|
||||
}
|
||||
}
|
||||
|
||||
public static void notNull(Object object, String message) {
|
||||
if (object == null) {
|
||||
throw new JdbcException(message);
|
||||
}
|
||||
}
|
||||
|
||||
public static void notNull(Object object, String message, Object... values) {
|
||||
if (object == null) {
|
||||
throw new JdbcException(message, values);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -19,7 +19,7 @@ public class JdbcConfigurationTests extends ESTestCase {
|
|||
|
||||
public void testJustThePrefix() throws Exception {
|
||||
Exception e = expectThrows(JdbcException.class, () -> ci("jdbc:es:"));
|
||||
assertEquals("Invalid URL jdbc:es:, format should be jdbc:es://[host[:port]]*/[prefix]*[?[option=value]&]*", e.getMessage());
|
||||
assertEquals("Invalid URL [jdbc:es:], format should be [jdbc:es://[host[:port]]*/[prefix]*[?[option=value]&]*]", e.getMessage());
|
||||
}
|
||||
|
||||
public void testJustTheHost() throws Exception {
|
||||
|
|
Loading…
Reference in New Issue