Search in sources :

Example 1 with UnableToExecuteStatementException

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();
}
Also used : SQLException(java.sql.SQLException) UnableToExecuteStatementException(org.jdbi.v3.core.statement.UnableToExecuteStatementException) ErrorMessage(io.dropwizard.jersey.errors.ErrorMessage)

Example 2 with UnableToExecuteStatementException

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");
    }
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.jupiter.api.Test)

Example 3 with UnableToExecuteStatementException

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());
}
Also used : Response(javax.ws.rs.core.Response) LibraryCard(org.broadinstitute.consent.http.models.LibraryCard) UnableToExecuteStatementException(org.jdbi.v3.core.statement.UnableToExecuteStatementException) Gson(com.google.gson.Gson) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 4 with UnableToExecuteStatementException

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);
}
Also used : PSQLState(org.postgresql.util.PSQLState) PSQLException(org.postgresql.util.PSQLException) UnableToExecuteStatementException(org.jdbi.v3.core.statement.UnableToExecuteStatementException)

Example 5 with UnableToExecuteStatementException

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);
}
Also used : DataSet(org.broadinstitute.consent.http.models.DataSet) NotFoundException(javax.ws.rs.NotFoundException) UnableToExecuteStatementException(org.jdbi.v3.core.statement.UnableToExecuteStatementException) BadRequestException(javax.ws.rs.BadRequestException) BatchUpdateException(java.sql.BatchUpdateException)

Aggregations

UnableToExecuteStatementException (org.jdbi.v3.core.statement.UnableToExecuteStatementException)8 Test (org.junit.Test)3 Gson (com.google.gson.Gson)2 SQLException (java.sql.SQLException)2 BadRequestException (javax.ws.rs.BadRequestException)2 NotFoundException (javax.ws.rs.NotFoundException)2 Response (javax.ws.rs.core.Response)2 AuthUser (org.broadinstitute.consent.http.models.AuthUser)2 LibraryCard (org.broadinstitute.consent.http.models.LibraryCard)2 User (org.broadinstitute.consent.http.models.User)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ErrorMessage (io.dropwizard.jersey.errors.ErrorMessage)1 BatchUpdateException (java.sql.BatchUpdateException)1 Connection (java.sql.Connection)1 LinkedList (java.util.LinkedList)1 DataSet (org.broadinstitute.consent.http.models.DataSet)1 Handle (org.jdbi.v3.core.Handle)1