UncheckedSQLException extends RuntimeException { private ErrorType errorType(SQLException e) { String state = e.getSQLState(); if (state == null) return null; if (state.startsWith("23")) return ErrorType.INTEGRITY_CONSTRAINT_VIOLATION; if (state.startsWith("08")) return ErrorType.CONNECTION_ERROR; return null; } UncheckedSQLException(SQLException e);  final ErrorType errorType; final String sqlSate; }
@Test void errorType() { var exception = new UncheckedSQLException(new SQLException("connection abort", "08S01", 1152)); assertThat(exception.errorType).isEqualTo(UncheckedSQLException.ErrorType.CONNECTION_ERROR); var cause = new SQLIntegrityConstraintViolationException("Duplicate entry", "23000", 1062); exception = new UncheckedSQLException(new BatchUpdateException(cause.getMessage(), cause.getSQLState(), cause.getErrorCode(), new int[0], cause)); assertThat(exception.errorType).isEqualTo(UncheckedSQLException.ErrorType.INTEGRITY_CONSTRAINT_VIOLATION); }