Skip to content

Commit dca3a6e

Browse files
committed
Use constants for most error messages
1 parent 81445fc commit dca3a6e

8 files changed

+67
-16
lines changed

Diff for: src/main/java/com/ing/data/cassandra/jdbc/CassandraDatabaseMetaData.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import static com.ing.data.cassandra.jdbc.utils.DriverUtil.buildMetadataList;
4343
import static com.ing.data.cassandra.jdbc.utils.DriverUtil.getDriverProperty;
4444
import static com.ing.data.cassandra.jdbc.utils.DriverUtil.safeParseVersion;
45+
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.INVALID_CATALOG_NAME;
4546
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.NOT_SUPPORTED;
4647
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.NO_INTERFACE;
4748

@@ -724,7 +725,7 @@ public ResultSet getSchemas() throws SQLException {
724725
public ResultSet getSchemas(final String catalog, final String schemaPattern) throws SQLException {
725726
checkStatementClosed();
726727
if (!(catalog == null || catalog.equals(this.statement.connection.getCatalog()))) {
727-
throw new SQLSyntaxErrorException("Catalog name must exactly match or be null.");
728+
throw new SQLSyntaxErrorException(INVALID_CATALOG_NAME);
728729
}
729730
return new SchemaMetadataResultSetBuilder(this.statement).buildSchemas(schemaPattern);
730731
}

Diff for: src/main/java/com/ing/data/cassandra/jdbc/CassandraDriver.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import static com.ing.data.cassandra.jdbc.utils.DriverUtil.getDriverProperty;
3636
import static com.ing.data.cassandra.jdbc.utils.DriverUtil.safeParseVersion;
37+
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.CONNECTION_CREATION_FAILED;
3738
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.NOT_SUPPORTED;
3839
import static com.ing.data.cassandra.jdbc.utils.JdbcUrlUtil.PROTOCOL;
3940
import static com.ing.data.cassandra.jdbc.utils.JdbcUrlUtil.TAG_CONTACT_POINTS;
@@ -101,7 +102,7 @@ public Connection connect(final String url, final Properties properties) throws
101102
if (cause instanceof SQLException) {
102103
throw (SQLException) cause;
103104
}
104-
throw new SQLNonTransientConnectionException("Unexpected error while creating connection.", e);
105+
throw new SQLNonTransientConnectionException(CONNECTION_CREATION_FAILED, e);
105106
}
106107
}
107108
// Signal it is the wrong driver for this <protocol:sub_protocol>.

Diff for: src/main/java/com/ing/data/cassandra/jdbc/CassandraMetadataResultSet.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.BAD_FETCH_DIR;
6868
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.BAD_FETCH_SIZE;
6969
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.FORWARD_ONLY;
70+
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.ILLEGAL_FETCH_DIRECTION_FOR_FORWARD_ONLY;
7071
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.MALFORMED_URL;
7172
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.MUST_BE_POSITIVE;
7273
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.NOT_SUPPORTED;
@@ -524,7 +525,7 @@ public void setFetchDirection(final int direction) throws SQLException {
524525
checkNotClosed();
525526
if (direction == FETCH_FORWARD || direction == FETCH_REVERSE || direction == FETCH_UNKNOWN) {
526527
if (getType() == TYPE_FORWARD_ONLY && direction != FETCH_FORWARD) {
527-
throw new SQLSyntaxErrorException("Attempt to set an illegal direction: " + direction);
528+
throw new SQLSyntaxErrorException(String.format(ILLEGAL_FETCH_DIRECTION_FOR_FORWARD_ONLY, direction));
528529
}
529530
this.fetchDirection = direction;
530531
}

Diff for: src/main/java/com/ing/data/cassandra/jdbc/CassandraPreparedStatement.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,11 @@
7878
import static com.ing.data.cassandra.jdbc.utils.ConversionsUtil.convertToInstant;
7979
import static com.ing.data.cassandra.jdbc.utils.ConversionsUtil.convertToLocalDate;
8080
import static com.ing.data.cassandra.jdbc.utils.ConversionsUtil.convertToLocalTime;
81+
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.MUST_BE_POSITIVE_BINDING_INDEX;
8182
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.NO_RESULT_SET;
83+
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.OUT_OF_BOUNDS_BINDING_INDEX;
8284
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.TOO_MANY_QUERIES;
85+
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.UNSUPPORTED_CONVERSION_TO_JSON;
8386
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.UNSUPPORTED_JDBC_TYPE;
8487
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.VECTOR_ELEMENTS_NOT_NUMBERS;
8588
import static com.ing.data.cassandra.jdbc.utils.JsonUtil.getObjectMapper;
@@ -151,12 +154,10 @@ public class CassandraPreparedStatement extends CassandraStatement
151154

152155
private void checkIndex(final int index) throws SQLException {
153156
if (index > this.count) {
154-
throw new SQLRecoverableException(String.format(
155-
"The column index: %d is greater than the count of bound variable markers in the CQL: %d", index,
156-
this.count));
157+
throw new SQLRecoverableException(String.format(OUT_OF_BOUNDS_BINDING_INDEX, index, this.count));
157158
}
158159
if (index < 1) {
159-
throw new SQLRecoverableException(String.format("The column index must be a positive number: %d", index));
160+
throw new SQLRecoverableException(String.format(MUST_BE_POSITIVE_BINDING_INDEX, index));
160161
}
161162
}
162163

@@ -764,8 +765,7 @@ public <T> void setJson(final int parameterIndex, final T x) throws SQLException
764765
setString(parameterIndex, json);
765766
} catch (final JsonProcessingException e) {
766767
throw new SQLException(
767-
String.format("Unable to convert the object of type %s to bind the column of index %d",
768-
x.getClass().getName(), parameterIndex));
768+
String.format(UNSUPPORTED_CONVERSION_TO_JSON, x.getClass().getName(), parameterIndex));
769769
}
770770
}
771771

Diff for: src/main/java/com/ing/data/cassandra/jdbc/CassandraResultSet.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,12 @@
9595
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.BAD_FETCH_DIR;
9696
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.BAD_FETCH_SIZE;
9797
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.FORWARD_ONLY;
98+
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.ILLEGAL_FETCH_DIRECTION_FOR_FORWARD_ONLY;
9899
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.MALFORMED_URL;
99100
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.MUST_BE_POSITIVE;
100101
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.NOT_SUPPORTED;
101102
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.NO_INTERFACE;
102103
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.UNABLE_TO_READ_VALUE;
103-
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.UNABLE_TO_RETRIEVE_METADATA;
104104
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.UNSUPPORTED_JSON_TYPE_CONVERSION;
105105
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.UNSUPPORTED_TYPE_CONVERSION;
106106
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.VALID_LABELS;
@@ -676,7 +676,7 @@ public void setFetchDirection(final int direction) throws SQLException {
676676

677677
if (direction == FETCH_FORWARD || direction == FETCH_REVERSE || direction == FETCH_UNKNOWN) {
678678
if (getType() == TYPE_FORWARD_ONLY && direction != FETCH_FORWARD) {
679-
throw new SQLSyntaxErrorException("attempt to set an illegal direction: " + direction);
679+
throw new SQLSyntaxErrorException(String.format(ILLEGAL_FETCH_DIRECTION_FOR_FORWARD_ONLY, direction));
680680
}
681681
this.fetchDirection = direction;
682682
}

Diff for: src/main/java/com/ing/data/cassandra/jdbc/ColumnDefinitions.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import java.util.List;
2626
import java.util.Map;
2727

28+
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.VALID_LABELS;
29+
2830
/**
2931
* Metadata describing the columns returned in a {@link CassandraResultSet} or a {@link CassandraPreparedStatement}.
3032
* <p>
@@ -277,7 +279,7 @@ int[] findAllIdx(final String name) {
277279
int[] getAllIdx(final String name) {
278280
final int[] indexes = findAllIdx(name);
279281
if (indexes == null) {
280-
throw new IllegalArgumentException(name + " is not a column defined in these metadata.");
282+
throw new IllegalArgumentException(String.format(VALID_LABELS, name));
281283
}
282284
return indexes;
283285
}

Diff for: src/main/java/com/ing/data/cassandra/jdbc/metadata/MetadataRow.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.UUID;
3939

4040
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.UNABLE_TO_POPULATE_METADATA_ROW;
41+
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.VALID_LABELS;
4142

4243
/**
4344
* The content of a metadata row returned in a {@link CassandraMetadataResultSet}.
@@ -680,7 +681,7 @@ public String toString() {
680681
private Integer getIndex(final String name) {
681682
final Integer idx = this.names.get(name);
682683
if (idx == null) {
683-
throw new IllegalArgumentException(name + " is not a column defined in this row.");
684+
throw new IllegalArgumentException(String.format(VALID_LABELS, name));
684685
}
685686
return idx;
686687
}

Diff for: src/main/java/com/ing/data/cassandra/jdbc/utils/ErrorConstants.java

+48-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import java.net.URI;
2424
import java.sql.Connection;
25+
import java.sql.PreparedStatement;
2526
import java.sql.ResultSet;
2627
import java.sql.Statement;
2728

@@ -168,9 +169,25 @@ public final class ErrorConstants {
168169
"Index must be a positive number less or equal the count of returned columns: %d";
169170

170171
/**
171-
* Error message used in any SQL exception thrown when the specified column name in a {@link ResultSet}
172-
* is invalid. This message is a template expecting the value of the invalid column name as placeholder (example:
173-
* {@code String.format(VALID_LABELS, "invalid_column")}).
172+
* Error message used in any SQL exception thrown when the specified index for a variable binding in a
173+
* {@link PreparedStatement} is greater than the number of binding variable markers in the CQL query. This message
174+
* is a template expecting the value of the invalid index and the number of markers as placeholders (example:
175+
* {@code String.format(OUT_OF_BOUNDS_BINDING_INDEX, 5, 3)}).
176+
*/
177+
public static final String OUT_OF_BOUNDS_BINDING_INDEX =
178+
"The index %d is greater than the count of bound variable markers in the CQL: %d";
179+
180+
/**
181+
* Error message used in any SQL exception thrown when the specified index for a variable binding in a
182+
* {@link PreparedStatement} is not strictly positive. This message is a template expecting the value of the
183+
* invalid index value as placeholder (example: {@code String.format(MUST_BE_POSITIVE_BINDING_INDEX, 0)}).
184+
*/
185+
public static final String MUST_BE_POSITIVE_BINDING_INDEX = "The binding index must be a positive number: %d";
186+
187+
/**
188+
* Error message used in any exception thrown when the specified column name in a {@link ResultSet} or a row
189+
* definition is invalid. This message is a template expecting the value of the invalid column name as placeholder
190+
* (example: {@code String.format(VALID_LABELS, "invalid_column")}).
174191
*/
175192
public static final String VALID_LABELS = "Name provided was not in the list of valid column labels: %s";
176193

@@ -275,6 +292,13 @@ public final class ErrorConstants {
275292
public static final String UNSUPPORTED_JSON_TYPE_CONVERSION =
276293
"Unable to convert the column of index %d to an instance of %s";
277294

295+
/**
296+
* Error message used in any SQL exception thrown when the conversion to JSON for the specified object in the method
297+
* {@link CassandraPreparedStatement#setJson(int, Object)} is not supported.
298+
*/
299+
public static final String UNSUPPORTED_CONVERSION_TO_JSON =
300+
"Unable to convert the object of type %s to bind the column of index %d";
301+
278302
/**
279303
* Error message used in any SQL exception thrown when it is not possible to retrieve some metadata of any
280304
* {@link ResultSet}.
@@ -297,6 +321,27 @@ public final class ErrorConstants {
297321
public static final String TOO_MANY_QUERIES =
298322
"Too many queries at once (%d). You must split your queries into more batches!";
299323

324+
/**
325+
* Error message used in any SQL exception thrown when the fetch direction specified on a ResultSet is not
326+
* supported for the type {@code TYPE_FORWARD_ONLY}. This message is a template expecting the illegal fetch
327+
* direction as placeholder (example:
328+
* {@code String.format(ILLEGAL_FETCH_DIRECTION_FOR_FORWARD_ONLY, FETCH_UNKNOWN)}).
329+
*/
330+
public static final String ILLEGAL_FETCH_DIRECTION_FOR_FORWARD_ONLY =
331+
"Attempt to set an illegal fetch direction for TYPE_FORWARD_ONLY: %d";
332+
333+
/**
334+
* Error message used in any SQL exception thrown when retrieving metadata related to a catalog and the given one
335+
* is not {@code null} or does not match the one of the current connection.
336+
*/
337+
public static final String INVALID_CATALOG_NAME = "Catalog name must exactly match or be null.";
338+
339+
/**
340+
* Error message used in any SQL exception thrown when the creation of the connection to the database fails for
341+
* any reason. The underlying exception should be logged in this case.
342+
*/
343+
public static final String CONNECTION_CREATION_FAILED = "Unexpected error while creating connection.";
344+
300345
private ErrorConstants() {
301346
// Private constructor to hide the public one.
302347
}

0 commit comments

Comments
 (0)