Skip to content

Commit

Permalink
Cleanup exception handling in ApplicationErrorJdbc (#401)
Browse files Browse the repository at this point in the history
* Catch SQLException in ApplicationErrorJdbc#createInMemoryH2Database
  instead of Exception to prevent wrapping ApplicationErrorJdbcException
  inside another ApplicationErrorJdbcException thrown by the call to
  migrateDatabase. Instead, wrap SQLException thrown by the call to
  getInMemoryH2Connection with UncheckedSQLException.
* Add catch clause in ApplicationErrorJdbc#migrateDatabase to catch
  SQLException and rethrow wrapped in UncheckedSQLException. This
  leaves only the (checked) Liquibase exceptions to be caught and
  wrapped in ApplicationErrorJdbcException.
  • Loading branch information
sleberknight authored Aug 4, 2024
1 parent 84ea5f1 commit fbfb945
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.checkerframework.checker.nullness.qual.Nullable;
import org.kiwiproject.dropwizard.error.model.ApplicationError;
import org.kiwiproject.dropwizard.error.model.DataStoreType;
import org.kiwiproject.jdbc.UncheckedSQLException;

import java.sql.Connection;
import java.sql.DriverManager;
Expand Down Expand Up @@ -69,8 +70,8 @@ public static DataSourceFactory createInMemoryH2Database() {
try (var conn = getInMemoryH2Connection()) {
migrateDatabase(conn);
return newInMemoryH2DataSourceFactory();
} catch (Exception e) {
throw new ApplicationErrorJdbcException("Error getting connection to in-memory H2 database", e);
} catch (SQLException e) {
throw new UncheckedSQLException("Error getting connection to in-memory H2 database", e);
}
}

Expand Down Expand Up @@ -118,6 +119,9 @@ public static void migrateDatabase(Connection conn, String migrationsFilename) {
conn.getAutoCommit(), originalAutoCommit);
conn.setAutoCommit(originalAutoCommit);
}
} catch (SQLException e) {
var message = format("JDBC/SQL error while migrating {} database", getDatabaseProductNameOrUnknown(conn));
throw new UncheckedSQLException(message, e);
} catch (Exception e) {
var message = format("Error migrating {} database", getDatabaseProductNameOrUnknown(conn));
throw new ApplicationErrorJdbcException(message, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.kiwiproject.jdbc.KiwiJdbc.nextOrThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import io.dropwizard.db.DataSourceFactory;
import org.h2.Driver;
Expand Down Expand Up @@ -177,6 +178,17 @@ void shouldMigrate_H2InMemoryDatabase() throws SQLException {
}
}

@Test
void shouldThrow_WhenSQLErrorOccurs() throws SQLException {
//noinspection resource
var conn = mock(Connection.class);
when(conn.getAutoCommit()).thenThrow(new SQLException("Unable to get autocommit status"));

assertThatThrownBy(() -> ApplicationErrorJdbc.migrateDatabase(conn))
.isExactlyInstanceOf(UncheckedSQLException.class)
.hasMessage("JDBC/SQL error while migrating [Unknown Error] database");
}

@Test
void shouldThrow_WhenMigrationErrorOccurs() {
//noinspection resource
Expand Down

0 comments on commit fbfb945

Please sign in to comment.