Skip to content

Commit

Permalink
Use constants for most error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
maximevw committed Dec 29, 2023
1 parent 81445fc commit dca3a6e
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import static com.ing.data.cassandra.jdbc.utils.DriverUtil.buildMetadataList;
import static com.ing.data.cassandra.jdbc.utils.DriverUtil.getDriverProperty;
import static com.ing.data.cassandra.jdbc.utils.DriverUtil.safeParseVersion;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.INVALID_CATALOG_NAME;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.NOT_SUPPORTED;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.NO_INTERFACE;

Expand Down Expand Up @@ -724,7 +725,7 @@ public ResultSet getSchemas() throws SQLException {
public ResultSet getSchemas(final String catalog, final String schemaPattern) throws SQLException {
checkStatementClosed();
if (!(catalog == null || catalog.equals(this.statement.connection.getCatalog()))) {
throw new SQLSyntaxErrorException("Catalog name must exactly match or be null.");
throw new SQLSyntaxErrorException(INVALID_CATALOG_NAME);
}
return new SchemaMetadataResultSetBuilder(this.statement).buildSchemas(schemaPattern);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import static com.ing.data.cassandra.jdbc.utils.DriverUtil.getDriverProperty;
import static com.ing.data.cassandra.jdbc.utils.DriverUtil.safeParseVersion;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.CONNECTION_CREATION_FAILED;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.NOT_SUPPORTED;
import static com.ing.data.cassandra.jdbc.utils.JdbcUrlUtil.PROTOCOL;
import static com.ing.data.cassandra.jdbc.utils.JdbcUrlUtil.TAG_CONTACT_POINTS;
Expand Down Expand Up @@ -101,7 +102,7 @@ public Connection connect(final String url, final Properties properties) throws
if (cause instanceof SQLException) {
throw (SQLException) cause;
}
throw new SQLNonTransientConnectionException("Unexpected error while creating connection.", e);
throw new SQLNonTransientConnectionException(CONNECTION_CREATION_FAILED, e);
}
}
// Signal it is the wrong driver for this <protocol:sub_protocol>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.BAD_FETCH_DIR;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.BAD_FETCH_SIZE;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.FORWARD_ONLY;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.ILLEGAL_FETCH_DIRECTION_FOR_FORWARD_ONLY;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.MALFORMED_URL;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.MUST_BE_POSITIVE;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.NOT_SUPPORTED;
Expand Down Expand Up @@ -524,7 +525,7 @@ public void setFetchDirection(final int direction) throws SQLException {
checkNotClosed();
if (direction == FETCH_FORWARD || direction == FETCH_REVERSE || direction == FETCH_UNKNOWN) {
if (getType() == TYPE_FORWARD_ONLY && direction != FETCH_FORWARD) {
throw new SQLSyntaxErrorException("Attempt to set an illegal direction: " + direction);
throw new SQLSyntaxErrorException(String.format(ILLEGAL_FETCH_DIRECTION_FOR_FORWARD_ONLY, direction));
}
this.fetchDirection = direction;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@
import static com.ing.data.cassandra.jdbc.utils.ConversionsUtil.convertToInstant;
import static com.ing.data.cassandra.jdbc.utils.ConversionsUtil.convertToLocalDate;
import static com.ing.data.cassandra.jdbc.utils.ConversionsUtil.convertToLocalTime;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.MUST_BE_POSITIVE_BINDING_INDEX;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.NO_RESULT_SET;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.OUT_OF_BOUNDS_BINDING_INDEX;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.TOO_MANY_QUERIES;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.UNSUPPORTED_CONVERSION_TO_JSON;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.UNSUPPORTED_JDBC_TYPE;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.VECTOR_ELEMENTS_NOT_NUMBERS;
import static com.ing.data.cassandra.jdbc.utils.JsonUtil.getObjectMapper;
Expand Down Expand Up @@ -151,12 +154,10 @@ public class CassandraPreparedStatement extends CassandraStatement

private void checkIndex(final int index) throws SQLException {
if (index > this.count) {
throw new SQLRecoverableException(String.format(
"The column index: %d is greater than the count of bound variable markers in the CQL: %d", index,
this.count));
throw new SQLRecoverableException(String.format(OUT_OF_BOUNDS_BINDING_INDEX, index, this.count));
}
if (index < 1) {
throw new SQLRecoverableException(String.format("The column index must be a positive number: %d", index));
throw new SQLRecoverableException(String.format(MUST_BE_POSITIVE_BINDING_INDEX, index));
}
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.BAD_FETCH_DIR;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.BAD_FETCH_SIZE;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.FORWARD_ONLY;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.ILLEGAL_FETCH_DIRECTION_FOR_FORWARD_ONLY;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.MALFORMED_URL;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.MUST_BE_POSITIVE;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.NOT_SUPPORTED;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.NO_INTERFACE;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.UNABLE_TO_READ_VALUE;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.UNABLE_TO_RETRIEVE_METADATA;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.UNSUPPORTED_JSON_TYPE_CONVERSION;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.UNSUPPORTED_TYPE_CONVERSION;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.VALID_LABELS;
Expand Down Expand Up @@ -676,7 +676,7 @@ public void setFetchDirection(final int direction) throws SQLException {

if (direction == FETCH_FORWARD || direction == FETCH_REVERSE || direction == FETCH_UNKNOWN) {
if (getType() == TYPE_FORWARD_ONLY && direction != FETCH_FORWARD) {
throw new SQLSyntaxErrorException("attempt to set an illegal direction: " + direction);
throw new SQLSyntaxErrorException(String.format(ILLEGAL_FETCH_DIRECTION_FOR_FORWARD_ONLY, direction));
}
this.fetchDirection = direction;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.List;
import java.util.Map;

import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.VALID_LABELS;

/**
* Metadata describing the columns returned in a {@link CassandraResultSet} or a {@link CassandraPreparedStatement}.
* <p>
Expand Down Expand Up @@ -277,7 +279,7 @@ int[] findAllIdx(final String name) {
int[] getAllIdx(final String name) {
final int[] indexes = findAllIdx(name);
if (indexes == null) {
throw new IllegalArgumentException(name + " is not a column defined in these metadata.");
throw new IllegalArgumentException(String.format(VALID_LABELS, name));
}
return indexes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.UUID;

import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.UNABLE_TO_POPULATE_METADATA_ROW;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.VALID_LABELS;

/**
* The content of a metadata row returned in a {@link CassandraMetadataResultSet}.
Expand Down Expand Up @@ -680,7 +681,7 @@ public String toString() {
private Integer getIndex(final String name) {
final Integer idx = this.names.get(name);
if (idx == null) {
throw new IllegalArgumentException(name + " is not a column defined in this row.");
throw new IllegalArgumentException(String.format(VALID_LABELS, name));
}
return idx;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import java.net.URI;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

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

/**
* Error message used in any SQL exception thrown when the specified column name in a {@link ResultSet}
* is invalid. This message is a template expecting the value of the invalid column name as placeholder (example:
* {@code String.format(VALID_LABELS, "invalid_column")}).
* Error message used in any SQL exception thrown when the specified index for a variable binding in a
* {@link PreparedStatement} is greater than the number of binding variable markers in the CQL query. This message
* is a template expecting the value of the invalid index and the number of markers as placeholders (example:
* {@code String.format(OUT_OF_BOUNDS_BINDING_INDEX, 5, 3)}).
*/
public static final String OUT_OF_BOUNDS_BINDING_INDEX =
"The index %d is greater than the count of bound variable markers in the CQL: %d";

/**
* Error message used in any SQL exception thrown when the specified index for a variable binding in a
* {@link PreparedStatement} is not strictly positive. This message is a template expecting the value of the
* invalid index value as placeholder (example: {@code String.format(MUST_BE_POSITIVE_BINDING_INDEX, 0)}).
*/
public static final String MUST_BE_POSITIVE_BINDING_INDEX = "The binding index must be a positive number: %d";

/**
* Error message used in any exception thrown when the specified column name in a {@link ResultSet} or a row
* definition is invalid. This message is a template expecting the value of the invalid column name as placeholder
* (example: {@code String.format(VALID_LABELS, "invalid_column")}).
*/
public static final String VALID_LABELS = "Name provided was not in the list of valid column labels: %s";

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

/**
* Error message used in any SQL exception thrown when the conversion to JSON for the specified object in the method
* {@link CassandraPreparedStatement#setJson(int, Object)} is not supported.
*/
public static final String UNSUPPORTED_CONVERSION_TO_JSON =
"Unable to convert the object of type %s to bind the column of index %d";

/**
* Error message used in any SQL exception thrown when it is not possible to retrieve some metadata of any
* {@link ResultSet}.
Expand All @@ -297,6 +321,27 @@ public final class ErrorConstants {
public static final String TOO_MANY_QUERIES =
"Too many queries at once (%d). You must split your queries into more batches!";

/**
* Error message used in any SQL exception thrown when the fetch direction specified on a ResultSet is not
* supported for the type {@code TYPE_FORWARD_ONLY}. This message is a template expecting the illegal fetch
* direction as placeholder (example:
* {@code String.format(ILLEGAL_FETCH_DIRECTION_FOR_FORWARD_ONLY, FETCH_UNKNOWN)}).
*/
public static final String ILLEGAL_FETCH_DIRECTION_FOR_FORWARD_ONLY =
"Attempt to set an illegal fetch direction for TYPE_FORWARD_ONLY: %d";

/**
* Error message used in any SQL exception thrown when retrieving metadata related to a catalog and the given one
* is not {@code null} or does not match the one of the current connection.
*/
public static final String INVALID_CATALOG_NAME = "Catalog name must exactly match or be null.";

/**
* Error message used in any SQL exception thrown when the creation of the connection to the database fails for
* any reason. The underlying exception should be logged in this case.
*/
public static final String CONNECTION_CREATION_FAILED = "Unexpected error while creating connection.";

private ErrorConstants() {
// Private constructor to hide the public one.
}
Expand Down

0 comments on commit dca3a6e

Please sign in to comment.