Elffuss BraidHub

kikocis/java-driver public

Merge pull request #607 from datastax/java1021

JAVA-1021: Improve error message when connect() is called with an inv…
Changeset8ae5bc7181fe93f28e0ffbf5ea3f0e200741b75af381011f1f2ca50691071446
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2016-02-26 19:29 +01:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2016-02-26 19:29 +01:00
Parents3439c5c67fc1, 9bbd20a64b85

Browse the files at this changeset

3 files changed

modified changelog/README.md +1 −0
@@ -20,6 +20,7 @@
2020 - [improvement] JAVA-974: Validate accessor parameter types against bound statement.
2121 - [bug] JAVA-1068: Unwrap StatementWrappers when hashing the paging state.
2222 - [bug] JAVA-831: Mapper can't load an entity where the PK is a UDT.
23+- [improvement] JAVA-1021: Improve error message when connect() is called with an invalid keyspace name.
2324
2425 Merged from 2.0 branch:
2526
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +15 −8
@@ -15,16 +15,14 @@
1515 */
1616 package com.datastax.driver.core;
1717
18-import com.datastax.driver.core.exceptions.AuthenticationException;
19-import com.datastax.driver.core.exceptions.DriverInternalError;
20-import com.datastax.driver.core.exceptions.InvalidQueryException;
21-import com.datastax.driver.core.exceptions.NoHostAvailableException;
18+import com.datastax.driver.core.exceptions.*;
2219 import com.datastax.driver.core.policies.*;
2320 import com.datastax.driver.core.utils.MoreFutures;
2421 import com.google.common.annotations.VisibleForTesting;
2522 import com.google.common.base.Functions;
2623 import com.google.common.base.Predicates;
2724 import com.google.common.base.Strings;
25+import com.google.common.base.Throwables;
2826 import com.google.common.collect.*;
2927 import com.google.common.util.concurrent.*;
3028 import org.slf4j.Logger;
@@ -334,19 +332,28 @@
334332 if (keyspace == null) {
335333 return sessionInitialized;
336334 } else {
335+ final String useQuery = "USE " + keyspace;
337336 ListenableFuture<ResultSet> keyspaceSet = Futures.transform(sessionInitialized, new AsyncFunction<Session, ResultSet>() {
338337 @Override
339338 public ListenableFuture<ResultSet> apply(Session session) throws Exception {
340- return session.executeAsync("USE " + keyspace);
339+ return session.executeAsync(useQuery);
341340 }
342341 });
343- Futures.addCallback(keyspaceSet, new MoreFutures.FailureCallback<ResultSet>() {
342+ ListenableFuture<ResultSet> withErrorHandling = Futures.withFallback(keyspaceSet, new FutureFallback<ResultSet>() {
344343 @Override
345- public void onFailure(Throwable t) {
344+ public ListenableFuture<ResultSet> create(Throwable t) throws Exception {
346345 session.closeAsync();
346+ if (t instanceof SyntaxError) {
347+ // Give a more explicit message, because it's probably caused by a bad keyspace name
348+ SyntaxError e = (SyntaxError) t;
349+ t = new SyntaxError(e.getAddress(),
350+ String.format("Error executing \"%s\" (%s). Check that your keyspace name is valid",
351+ useQuery, e.getMessage()));
352+ }
353+ throw Throwables.propagate(t);
347354 }
348355 });
349- return Futures.transform(keyspaceSet, Functions.<Session>constant(session));
356+ return Futures.transform(withErrorHandling, Functions.<Session>constant(session));
350357 }
351358 }
352359
modified driver-core/src/test/java/com/datastax/driver/core/SessionTest.java +59 −23
@@ -15,18 +15,17 @@
1515 */
1616 package com.datastax.driver.core;
1717
18+import com.datastax.driver.core.exceptions.SyntaxError;
1819 import com.datastax.driver.core.utils.CassandraVersion;
20+import com.google.common.util.concurrent.ListenableFuture;
1921 import org.testng.annotations.Test;
2022
2123 import java.util.List;
2224 import java.util.Locale;
23-import java.util.concurrent.CountDownLatch;
24-import java.util.concurrent.ExecutorService;
25-import java.util.concurrent.Executors;
26-import java.util.concurrent.TimeUnit;
25+import java.util.concurrent.*;
2726
27+import static com.datastax.driver.core.Assertions.*;
2828 import static com.datastax.driver.core.TestUtils.nonQuietClusterCloseOptions;
29-import static org.testng.Assert.*;
3029
3130 /**
3231 * Simple test of the Sessions methods against a one node cluster.
@@ -47,11 +46,11 @@
4746 }
4847
4948 @Test(groups = "short")
50- public void executeTest() throws Exception {
49+ public void should_execute_simple_statements() throws Exception {
5150 // Simple calls to all versions of the execute/executeAsync methods
5251 String key = "execute_test";
5352 ResultSet rs = session().execute(String.format(Locale.US, TestUtils.INSERT_FORMAT, TABLE1, key, "foo", 42, 24.03f));
54- assertTrue(rs.isExhausted());
53+ assertThat(rs.isExhausted()).isTrue();
5554
5655 // execute
5756 checkExecuteResultSet(session().execute(String.format(TestUtils.SELECT_ALL_FORMAT, TABLE1)), key);
@@ -63,12 +62,12 @@
6362 }
6463
6564 @Test(groups = "short")
66- public void executePreparedTest() throws Exception {
65+ public void should_execute_prepared_statements() throws Exception {
6766 // Simple calls to all versions of the execute/executeAsync methods for prepared statements
6867 // Note: the goal is only to exercice the Session methods, PreparedStatementTest have better prepared statement tests.
6968 String key = "execute_prepared_test";
7069 ResultSet rs = session().execute(String.format(Locale.US, TestUtils.INSERT_FORMAT, TABLE2, key, "foo", 42, 24.03f));
71- assertTrue(rs.isExhausted());
70+ assertThat(rs.isExhausted()).isTrue();
7271
7372 PreparedStatement p = session().prepare(String.format(TestUtils.SELECT_ALL_FORMAT + " WHERE k = ?", TABLE2));
7473 BoundStatement bs = p.bind(key);
@@ -83,17 +82,17 @@
8382 }
8483
8584 private static void checkExecuteResultSet(ResultSet rs, String key) {
86- assertTrue(!rs.isExhausted());
85+ assertThat(rs.isExhausted()).isFalse();
8786 Row row = rs.one();
88- assertTrue(rs.isExhausted());
89- assertEquals(row.getString("k"), key);
90- assertEquals(row.getString("t"), "foo");
91- assertEquals(row.getInt("i"), 42);
92- assertEquals(row.getFloat("f"), 24.03f, 0.1f);
87+ assertThat(rs.isExhausted()).isTrue();
88+ assertThat(row.getString("k")).isEqualTo(key);
89+ assertThat(row.getString("t")).isEqualTo("foo");
90+ assertThat(row.getInt("i")).isEqualTo(42);
91+ assertThat(row.getFloat("f")).isEqualTo(24.03f, offset(0.1f));
9392 }
9493
9594 @Test(groups = "short")
96- public void executePreparedCounterTest() throws Exception {
95+ public void should_execute_prepared_counter_statement() throws Exception {
9796 PreparedStatement p = session().prepare("UPDATE " + COUNTER_TABLE + " SET c = c + ? WHERE k = ?");
9897
9998 session().execute(p.bind(1L, "row"));
@@ -101,8 +100,8 @@
101100
102101 ResultSet rs = session().execute("SELECT * FROM " + COUNTER_TABLE);
103102 List<Row> rows = rs.all();
104- assertEquals(rows.size(), 1);
105- assertEquals(rows.get(0).getLong("c"), 2L);
103+ assertThat(rows).hasSize(1);
104+ assertThat(rows.get(0).getLong("c")).isEqualTo(2L);
106105 }
107106
108107 /**
@@ -113,7 +112,7 @@
113112 * @expected_result session established and queries made successfully using it.
114113 */
115114 @Test(groups = "short")
116- public void session_should_function_with_snappy_compression() throws Exception {
115+ public void should_function_with_snappy_compression() throws Exception {
117116 compressionTest(ProtocolOptions.Compression.SNAPPY);
118117 }
119118
@@ -126,7 +125,7 @@
126125 */
127126 @Test(groups = "short")
128127 @CassandraVersion(major = 2.0)
129- public void session_should_function_with_lz4_compression() throws Exception {
128+ public void should_function_with_lz4_compression() throws Exception {
130129 compressionTest(ProtocolOptions.Compression.LZ4);
131130 }
132131
@@ -138,7 +137,7 @@
138137 // Simple calls to all versions of the execute/executeAsync methods
139138 String key = "execute_compressed_test_" + compression;
140139 ResultSet rs = compressedSession.execute(String.format(Locale.US, TestUtils.INSERT_FORMAT, TABLE3, key, "foo", 42, 24.03f));
141- assertTrue(rs.isExhausted());
140+ assertThat(rs.isExhausted()).isTrue();
142141
143142 String SELECT_ALL = String.format(TestUtils.SELECT_ALL_FORMAT + " WHERE k = '%s'", TABLE3, key);
144143
@@ -159,7 +158,7 @@
159158 * Checks for deadlocks when a session shutdown races with the initialization of the cluster (JAVA-418).
160159 */
161160 @Test(groups = "short")
162- public void closeDuringClusterInitTest() throws InterruptedException {
161+ public void should_close_properly_when_racing_with_cluster_init() throws InterruptedException {
163162 for (int i = 0; i < 500; i++) {
164163
165164 // Use our own cluster and session (not the ones provided by the parent class) because we want an uninitialized cluster
@@ -206,7 +205,7 @@
206205
207206 executor.shutdown();
208207 boolean normalShutdown = executor.awaitTermination(5, TimeUnit.SECONDS);
209- assertTrue(normalShutdown);
208+ assertThat(normalShutdown).isTrue();
210209
211210 } finally {
212211 // The deadlock occurred here before JAVA-418
@@ -214,4 +213,41 @@
214213 }
215214 }
216215 }
216+
217+ /**
218+ * Ensures that if an attempt is made to create a {@link Session} via {@link Cluster#connect} with an invalid
219+ * keyspace that the returned exception is decorated with an indication to check that your keyspace name is valid
220+ * and includes the original {@link SyntaxError}.
221+ */
222+ @Test(groups = "short")
223+ public void should_give_explicit_error_message_when_keyspace_name_invalid() {
224+ try {
225+ cluster().connect("%!;");
226+ fail("Expected a SyntaxError");
227+ } catch (SyntaxError e) {
228+ assertThat(e.getMessage())
229+ .contains("Error executing \"USE %!;\"")
230+ .contains("Check that your keyspace name is valid");
231+ }
232+ }
233+
234+ /**
235+ * Ensures that if an attempt is made to create a {@link Session} via {@link Cluster#connectAsync} with an invalid
236+ * keyspace that the returned exception is decorated with an indication to check that your keyspace name is valid
237+ * and includes the original {@link SyntaxError}.
238+ */
239+ @Test(groups = "short")
240+ public void should_give_explicit_error_message_when_keyspace_name_invalid_async() {
241+ ListenableFuture<Session> sessionFuture = cluster().connectAsync("");
242+ try {
243+ sessionFuture.get();
244+ } catch (ExecutionException e) {
245+ assertThat(e.getCause()).isInstanceOf(SyntaxError.class);
246+ assertThat(e.getCause().getMessage())
247+ .contains("no viable alternative at input '<EOF>'")
248+ .contains("Check that your keyspace name is valid");
249+ } catch (Exception e) {
250+ fail("Did not expect Exception", e);
251+ }
252+ }
217253 }