use of org.jdbi.v3.core.statement.UnableToExecuteStatementException in project irontest by zheng-wang.
the class IronTestLoggingExceptionMapper method toResponse.
@Override
public Response toResponse(Throwable exception) {
long id = logException(exception);
int status = Response.Status.INTERNAL_SERVER_ERROR.getStatusCode();
String errorDetails = exception.getMessage();
// change error details if the exception is a known DB constraint violation
if (exception instanceof UnableToExecuteStatementException) {
SQLException se = (SQLException) exception.getCause();
if (se.getErrorCode() == ErrorCode.DUPLICATE_KEY_1 && se.getMessage().contains("_" + DB_UNIQUE_NAME_CONSTRAINT_NAME_SUFFIX)) {
errorDetails = "Duplicate name.";
} else if (se.getErrorCode() == ErrorCode.CHECK_CONSTRAINT_VIOLATED_1) {
if (se.getMessage().contains("_" + DB_PROPERTY_NAME_CONSTRAINT_NAME_SUFFIX)) {
errorDetails = "Property/column name can not be same as preserved names and can only contain letter, digit," + " $ and _ characters, beginning with letter, _ or $.";
} else if (se.getMessage().contains("_" + DB_USERNAME_CONSTRAINT_NAME_SUFFIX)) {
errorDetails = "Please enter a valid username: 3+ characters long, characters \"A-Za-z0-9_\".";
}
}
}
ErrorMessage errorMessage = new ErrorMessage(status, formatErrorMessage(id, exception), errorDetails);
return Response.status(status).type(MediaType.APPLICATION_JSON_TYPE).entity(errorMessage).build();
}
use of org.jdbi.v3.core.statement.UnableToExecuteStatementException in project jdbi by jdbi.
the class TestPreparedBatch method testContextGetsBinding.
@Test
public void testContextGetsBinding() {
Handle h = h2Extension.openHandle();
try {
h.prepareBatch("insert into something (id, name) values (:id, :name)").bind("id", 0).bind("name", "alice").add().bind("id", 0).bind("name", "bob").add().execute();
fail("expected exception");
} catch (UnableToExecuteStatementException e) {
final StatementContext ctx = e.getStatementContext();
assertThat(ctx.getBinding().findForName("name", ctx).toString()).contains("bob");
}
}
use of org.jdbi.v3.core.statement.UnableToExecuteStatementException in project consent by DataBiosphere.
the class LibraryCardResourceTest method testUpdateLibraryCardThrowsUniqueViolation.
@Test
public void testUpdateLibraryCardThrowsUniqueViolation() {
UnableToExecuteStatementException exception = generateUniqueViolationException();
when(userService.findUserByEmail(anyString())).thenReturn(user);
when(libraryCardService.updateLibraryCard(any(LibraryCard.class), anyInt(), anyInt())).thenThrow(exception);
String payload = new Gson().toJson(mockLibraryCardSetup());
initResource();
Response response = resource.updateLibraryCard(authUser, 1, payload);
assertEquals(HttpStatusCodes.STATUS_CODE_CONFLICT, response.getStatus());
}
use of org.jdbi.v3.core.statement.UnableToExecuteStatementException in project consent by DataBiosphere.
the class LibraryCardResourceTest method generateUniqueViolationException.
private UnableToExecuteStatementException generateUniqueViolationException() {
PSQLState uniqueViolationEnum = PSQLState.UNIQUE_VIOLATION;
PSQLException uniqueViolationException = new PSQLException("Error", uniqueViolationEnum);
return new UnableToExecuteStatementException(uniqueViolationException, null);
}
use of org.jdbi.v3.core.statement.UnableToExecuteStatementException in project consent by DataBiosphere.
the class DatasetAssociationService method createDatasetUsersAssociation.
public List<DatasetAssociation> createDatasetUsersAssociation(Integer dataSetId, List<Integer> userIds) {
verifyUsers(userIds);
DataSet d = dsDAO.findDataSetById(dataSetId);
if (Objects.isNull(d)) {
throw new NotFoundException("Invalid DatasetId");
}
Integer datasetId = d.getDataSetId();
try {
dsAssociationDAO.insertDatasetUserAssociation(DatasetAssociation.createDatasetAssociations(datasetId, userIds));
} catch (UnableToExecuteStatementException e) {
if (e.getCause().getClass().equals(BatchUpdateException.class)) {
throw new BadRequestException("Duplicate entry for an Association");
}
}
return dsAssociationDAO.getDatasetAssociation(datasetId);
}
Aggregations