Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor exception message #19

Merged
merged 6 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/main/java/org/itsallcode/jdbc/SimpleBatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.List;
import java.util.*;
import java.util.logging.Logger;

import org.itsallcode.jdbc.SimpleParameterMetaData.Parameter;
Expand All @@ -20,9 +19,9 @@ class SimpleBatch implements AutoCloseable {
private int currentBatchSize = 0;

SimpleBatch(final SimplePreparedStatement statement, final Context context) {
this.statement = statement;
this.context = context;
this.parameterMetadata = statement.getParameterMetadata().getParameters();
this.statement = Objects.requireNonNull(statement, "statement");
this.context = Objects.requireNonNull(context, "context");
this.parameterMetadata = statement.getParameterMetadata().parameters();
}

SimpleBatch add(final Object... args) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/itsallcode/jdbc/SimpleConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private PreparedStatement prepare(final String sql) {
try {
return connection.prepareStatement(sql);
} catch (final SQLException e) {
throw new UncheckedSQLException("Error preparing statement '" + sql + "': " + e.getMessage(), e);
throw new UncheckedSQLException("Error preparing statement '" + sql + "'", e);
}
}

Expand All @@ -167,7 +167,7 @@ public void close() {
try {
connection.close();
} catch (final SQLException e) {
throw new UncheckedSQLException("Error closing connection: " + e.getMessage(), e);
throw new UncheckedSQLException("Error closing connection", e);
}
}
}
33 changes: 14 additions & 19 deletions src/main/java/org/itsallcode/jdbc/SimpleParameterMetaData.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
package org.itsallcode.jdbc;

import java.sql.ParameterMetaData;
import java.sql.SQLException;
import java.sql.*;
import java.util.*;

/**
* Wrapper for {@link ParameterMetaData} that simplifies usage.
*
* @param parameters all parameters
*/
public class SimpleParameterMetaData {
public record SimpleParameterMetaData(List<Parameter> parameters) {

private final ParameterMetaData metaData;

SimpleParameterMetaData(final ParameterMetaData parameterMetaData) {
this.metaData = parameterMetaData;
static SimpleParameterMetaData create(final ParameterMetaData parameterMetaData) {
return new SimpleParameterMetaData(getParameters(parameterMetaData));
}

/**
* Get all parameters.
*
* @return all parameters
*/
public List<Parameter> getParameters() {
private static List<Parameter> getParameters(final ParameterMetaData metaData) {
try {
final List<Parameter> parameters = new ArrayList<>(metaData.getParameterCount());
for (int i = 1; i <= metaData.getParameterCount(); i++) {
parameters.add(new Parameter(metaData.getParameterClassName(i), metaData.getParameterType(i),
metaData.getParameterTypeName(i), ParameterMode.of(metaData.getParameterMode(i)),
metaData.getPrecision(i), metaData.getScale(i), metaData.isSigned(i),
ParameterNullable.of(metaData.isNullable(i))));
parameters.add(
new Parameter(metaData.getParameterClassName(i), JDBCType.valueOf(metaData.getParameterType(i)),
metaData.getParameterTypeName(i), ParameterMode.of(metaData.getParameterMode(i)),
metaData.getPrecision(i), metaData.getScale(i), metaData.isSigned(i),
ParameterNullable.of(metaData.isNullable(i))));
}
return parameters;
} catch (final SQLException e) {
Expand Down Expand Up @@ -82,7 +77,7 @@ private ParameterNullable(final int mode) {

private static ParameterNullable of(final int mode) {
return Arrays.stream(values()).filter(m -> m.mode == mode).findAny().orElseThrow(
() -> new IllegalArgumentException("No parameter mode found for value " + mode));
() -> new IllegalArgumentException("No parameter nullable mode found for value " + mode));
}
}

Expand All @@ -98,7 +93,7 @@ private static ParameterNullable of(final int mode) {
* @param signed {@code true} if the parameter is signed
* @param nullable nullability of the parameter
*/
public static record Parameter(String className, int type, String typeName, ParameterMode mode, int precision,
public static record Parameter(String className, JDBCType type, String typeName, ParameterMode mode, int precision,
int scale, boolean signed, ParameterNullable nullable) {
}
}
14 changes: 7 additions & 7 deletions src/main/java/org/itsallcode/jdbc/SimplePreparedStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ private ResultSet doExecute() {
try {
return statement.executeQuery();
} catch (final SQLException e) {
throw new UncheckedSQLException("Error executing query '" + sql + "': " + e.getMessage(), e);
throw new UncheckedSQLException("Error executing query '" + sql + "'", e);
}
}

Expand All @@ -38,15 +38,15 @@ public void close() {
try {
statement.close();
} catch (final SQLException e) {
throw new UncheckedSQLException("Error closing statement: " + e.getMessage(), e);
throw new UncheckedSQLException("Error closing statement", e);
}
}

void setValues(final PreparedStatementSetter preparedStatementSetter) {
try {
preparedStatementSetter.setValues(statement);
} catch (final SQLException e) {
throw new UncheckedSQLException("Error setting values for prepared statement: " + e.getMessage(), e);
throw new UncheckedSQLException("Error setting values for prepared statement", e);
}

}
Expand All @@ -55,23 +55,23 @@ void executeBatch() {
try {
statement.executeBatch();
} catch (final SQLException e) {
throw new UncheckedSQLException("Error executing batch sql '" + sql + "': " + e.getMessage(), e);
throw new UncheckedSQLException("Error executing batch sql '" + sql + "'", e);
}
}

void addBatch() {
try {
this.statement.addBatch();
} catch (final SQLException e) {
throw new UncheckedSQLException("Error adding batch: " + e.getMessage(), e);
throw new UncheckedSQLException("Error adding batch", e);
}
}

SimpleParameterMetaData getParameterMetadata() {
try {
return new SimpleParameterMetaData(statement.getParameterMetaData());
return SimpleParameterMetaData.create(statement.getParameterMetaData());
} catch (final SQLException e) {
throw new UncheckedSQLException("Error getting parameter metadata: " + e.getMessage(), e);
throw new UncheckedSQLException("Error getting parameter metadata", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ public UncheckedSQLException(final SQLException cause) {
* @param cause cause
*/
public UncheckedSQLException(final String message, final SQLException cause) {
super(message, cause);
super(message + ": " + cause.getMessage(), cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ public void close() {
try {
resultSet.close();
} catch (final SQLException e) {
throw new UncheckedSQLException("Error closing resultset: " + e.getMessage(), e);
throw new UncheckedSQLException("Error closing resultset", e);
}
}

private boolean next() {
try {
return resultSet.next();
} catch (final SQLException e) {
throw new UncheckedSQLException("Error getting next row: " + e.getMessage(), e);
throw new UncheckedSQLException("Error getting next row", e);
}
}

Expand Down Expand Up @@ -131,7 +131,7 @@ private T mapRow() {
try {
return rowMapper.mapRow(context, resultSet.resultSet, currentRowIndex);
} catch (final SQLException e) {
throw new UncheckedSQLException("Error mapping row " + currentRowIndex + ": " + e.getMessage(), e);
throw new UncheckedSQLException("Error mapping row " + currentRowIndex, e);
} catch (final RuntimeException e) {
throw new IllegalStateException("Error mapping row " + currentRowIndex + ": " + e.getMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.ArrayList;
import java.util.List;

import org.itsallcode.jdbc.UncheckedSQLException;

/**
* Represents the metadata of a single column.
*
Expand All @@ -14,11 +16,15 @@
*/
public record ColumnMetaData(int columnIndex, String name, String label, ColumnType type) {

static List<ColumnMetaData> create(final ResultSet resultSet) throws SQLException {
return create(resultSet.getMetaData());
static List<ColumnMetaData> create(final ResultSet resultSet) {
try {
return create(resultSet.getMetaData());
} catch (final SQLException e) {
throw new UncheckedSQLException("Error extracting meta data", e);
}
}

static List<ColumnMetaData> create(final ResultSetMetaData metaData)
private static List<ColumnMetaData> create(final ResultSetMetaData metaData)
throws SQLException {
final int columnCount = metaData.getColumnCount();
final List<ColumnMetaData> columns = new ArrayList<>(columnCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.*;

import org.itsallcode.jdbc.UncheckedSQLException;
import org.itsallcode.jdbc.dialect.DbDialect;
Expand All @@ -27,8 +26,8 @@ public class GenericRowMapper<T> implements RowMapper<T> {
* type.
*/
public GenericRowMapper(final DbDialect dialect, final ColumnValuesConverter<T> converter) {
this.dialect = dialect;
this.converter = converter;
this.dialect = Objects.requireNonNull(dialect, "dialect");
this.converter = Objects.requireNonNull(converter, "converter");
}

@Override
Expand Down Expand Up @@ -66,8 +65,7 @@ private Object getValue(final ResultSet resultSet, final ColumnMetaData column,
try {
return resultSet.getObject(column.columnIndex());
} catch (final SQLException e) {
throw new UncheckedSQLException(
"Error extracting value for row " + rowIndex + " / column " + column + ": " + e.getMessage(),
throw new UncheckedSQLException("Error extracting value for row " + rowIndex + " / column " + column,
e);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package org.itsallcode.jdbc.resultset.generic;

import java.sql.*;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.List;

import org.itsallcode.jdbc.UncheckedSQLException;

/**
* A wrapper for {@link ResultSetMetaData} to simplify usage.
*
Expand All @@ -19,11 +18,7 @@ public record SimpleMetaData(List<ColumnMetaData> columns) {
* @return simple metadata
*/
public static SimpleMetaData create(final ResultSet resultSet) {
try {
return new SimpleMetaData(ColumnMetaData.create(resultSet));
} catch (final SQLException e) {
throw new UncheckedSQLException("Error extracting meta data", e);
}
return new SimpleMetaData(ColumnMetaData.create(resultSet));
}

/**
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/org/itsallcode/jdbc/ConnectionFactoryITest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.itsallcode.jdbc;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -19,4 +20,25 @@ void createConnection() {
assertThat(connection).isNotNull();
}
}

@Test
void createConnectionFailsUnknownDbType() {
assertThatThrownBy(() -> connectionFactory.create("jdbc:unknown"))
.isInstanceOf(UncheckedSQLException.class)
.hasMessage("Error connecting to 'jdbc:unknown': No suitable driver found for jdbc:unknown");
}

@Test
void createConnectionFailsUnknownUrl() {
assertThatThrownBy(() -> connectionFactory.create("unknown"))
.isInstanceOf(UncheckedSQLException.class)
.hasMessage("Error connecting to 'unknown': No suitable driver found for unknown");
}

@Test
void createConnectionFailsInvalidJdbcUrl() {
assertThatThrownBy(() -> connectionFactory.create("jdbc:h2:unknown")).isInstanceOf(UncheckedSQLException.class)
.hasMessageStartingWith(
"Error connecting to 'jdbc:h2:unknown': A file path that is implicitly relative to the current working directory is not allowed in the database URL");
}
}
Loading