Skip to content

Commit

Permalink
Replace ApplicationErrorJdbcException with UncheckedSQLException
Browse files Browse the repository at this point in the history
Wrap any SQLException that we catch with UncheckedSQLException.

Closes #391
  • Loading branch information
sleberknight committed Aug 4, 2024
1 parent 44f8e90 commit fa3a9ea
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@

import org.kiwiproject.dropwizard.error.dao.ApplicationErrorDao;
import org.kiwiproject.dropwizard.error.dao.ApplicationErrorJdbc;
import org.kiwiproject.dropwizard.error.dao.ApplicationErrorJdbc.ApplicationErrorJdbcException;
import org.kiwiproject.dropwizard.error.dao.ApplicationErrorStatus;
import org.kiwiproject.dropwizard.error.model.ApplicationError;
import org.kiwiproject.jdbc.UncheckedSQLException;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
Expand All @@ -27,6 +26,8 @@
import java.util.List;
import java.util.Optional;

import javax.sql.DataSource;

/**
* Implementation of {@link ApplicationErrorDao} that uses plain JDBC, and therefore does not require
* any additional dependencies outside the JDK. It might be useful when an application is using a
Expand Down Expand Up @@ -58,7 +59,7 @@ public Optional<ApplicationError> getById(long id) {
return Optional.empty();
}
} catch (SQLException e) {
throw new ApplicationErrorJdbcException(e);
throw new UncheckedSQLException(e);
}
}

Expand All @@ -82,7 +83,7 @@ private long countUsingQuery(String sql) {
nextOrThrow(rs);
return rs.getLong(1);
} catch (SQLException e) {
throw new ApplicationErrorJdbcException(e);
throw new UncheckedSQLException(e);
}
}

Expand All @@ -98,7 +99,7 @@ public long countUnresolvedErrorsSince(ZonedDateTime since) {
return rs.getLong(1);
}
} catch (SQLException e) {
throw new ApplicationErrorJdbcException(e);
throw new UncheckedSQLException(e);
}
}

Expand All @@ -117,7 +118,7 @@ public long countUnresolvedErrorsOnHostSince(ZonedDateTime since, String hostNam
return rs.getLong(1);
}
} catch (SQLException e) {
throw new ApplicationErrorJdbcException(e);
throw new UncheckedSQLException(e);
}
}

Expand All @@ -130,7 +131,7 @@ public List<ApplicationError> getAllErrors(int pageNumber, int pageSize) {
try (var conn = connection(); var stmt = conn.createStatement(); var rs = stmt.executeQuery(sql)) {
return collectErrors(rs, pageSize);
} catch (SQLException e) {
throw new ApplicationErrorJdbcException(e);
throw new UncheckedSQLException(e);
}
}

Expand All @@ -154,7 +155,7 @@ private List<ApplicationError> getErrors(boolean resolved, int pageNumber, int p
ps.setBoolean(1, resolved);
return collectErrors(ps, pageSize);
} catch (SQLException e) {
throw new ApplicationErrorJdbcException(e);
throw new UncheckedSQLException(e);
}
}

Expand All @@ -171,7 +172,7 @@ public List<ApplicationError> getUnresolvedErrorsByDescription(String descriptio
ps.setString(1, description);
return collectErrors(ps, DEFAULT_PAGE_SIZE);
} catch (SQLException e) {
throw new ApplicationErrorJdbcException(e);
throw new UncheckedSQLException(e);
}
}

Expand All @@ -185,7 +186,7 @@ public List<ApplicationError> getUnresolvedErrorsByDescriptionAndHost(String des
ps.setString(2, hostName);
return collectErrors(ps, DEFAULT_PAGE_SIZE);
} catch (SQLException e) {
throw new ApplicationErrorJdbcException(e);
throw new UncheckedSQLException(e);
}
}

Expand Down Expand Up @@ -232,7 +233,7 @@ public long insertError(ApplicationError newError) {
return generatedKeys.getLong(1);
}
} catch (SQLException e) {
throw new ApplicationErrorJdbcException(e);
throw new UncheckedSQLException(e);
}
}

Expand All @@ -248,7 +249,7 @@ public void incrementCount(long id) {
var count = ps.executeUpdate();
checkState(count == 1, "Unable to increment count. No ApplicationError found with id %s", id);
} catch (SQLException e) {
throw new ApplicationErrorJdbcException(e);
throw new UncheckedSQLException(e);
}
}

Expand Down Expand Up @@ -277,7 +278,7 @@ public ApplicationError resolve(long id) {
var count = ps.executeUpdate();
checkState(count == 1, "Unable to resolve. No ApplicationError found with id %s", id);
} catch (SQLException e) {
throw new ApplicationErrorJdbcException(e);
throw new UncheckedSQLException(e);
}

return getById(id).orElseThrow();
Expand All @@ -290,7 +291,7 @@ public int resolveAllUnresolvedErrors() {
try (var conn = connection(); var stmt = conn.createStatement()) {
return stmt.executeUpdate(sql);
} catch (SQLException e) {
throw new ApplicationErrorJdbcException(e);
throw new UncheckedSQLException(e);
}
}

Expand All @@ -311,7 +312,7 @@ private int deleteResolvedErrorsBeforeUsingQuery(String sql, ZonedDateTime expir
ps.setTimestamp(1, timestampFromZonedDateTime(expirationDate));
return ps.executeUpdate();
} catch (SQLException e) {
throw new ApplicationErrorJdbcException(e);
throw new UncheckedSQLException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@
import lombok.extern.slf4j.Slf4j;
import org.kiwiproject.dropwizard.error.dao.ApplicationErrorJdbc;
import org.kiwiproject.dropwizard.error.dao.ApplicationErrorJdbc.ApplicationErrorJdbcException;
import org.kiwiproject.jdbc.UncheckedSQLException;
import org.kiwiproject.test.jdbc.SimpleSingleConnectionDataSource;
import org.testcontainers.containers.JdbcDatabaseContainer;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.utility.DockerImageName;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.sql.DataSource;

@UtilityClass
@Slf4j
public class TestHelpers {
Expand Down Expand Up @@ -58,7 +60,7 @@ public static void migrateDatabase(JdbcDatabaseContainer<?> container, String mi
try (var conn = DriverManager.getConnection(jdbcUrl, container.getUsername(), container.getPassword())) {
ApplicationErrorJdbc.migrateDatabase(conn, migrationsFilename);
} catch (SQLException e) {
throw new ApplicationErrorJdbcException(e);
throw new UncheckedSQLException(e);
}

LOG.info("Completed migrating {} container database using file {}", dockerImageName, migrationsFilename);
Expand All @@ -84,7 +86,7 @@ public static void migrateDatabase(SimpleSingleConnectionDataSource dataSource,
try {
ApplicationErrorJdbc.migrateDatabase(dataSource.getConnection(), migrationsFilename);
} catch (SQLException e) {
throw new ApplicationErrorJdbcException(e);
throw new UncheckedSQLException(e);
}
LOG.info("Completed migrating database using file {}", migrationsFilename);
}
Expand Down

0 comments on commit fa3a9ea

Please sign in to comment.