Search in sources :

Example 1 with ErrorMessage

use of io.dropwizard.jersey.errors.ErrorMessage 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.skife.jdbi.v2.exceptions.UnableToExecuteStatementException) ErrorMessage(io.dropwizard.jersey.errors.ErrorMessage)

Example 2 with ErrorMessage

use of io.dropwizard.jersey.errors.ErrorMessage in project api-core by ca-cwds.

the class CustomJsonProcessingExceptionMapper method toResponse.

@Override
public Response toResponse(JsonProcessingException exception) {
    Response response = super.toResponse(exception);
    if (!(response.getEntity() instanceof ErrorMessage)) {
        throw new IllegalStateException("ErrorMessage class is expected here");
    }
    ErrorMessage errorMessage = (ErrorMessage) response.getEntity();
    IssueDetails details = new IssueDetails();
    details.setType(IssueType.JSON_PROCESSING_EXCEPTION);
    details.setIncidentId(loggingContext.getUniqueId());
    details.setUserMessage(BASE_MESSAGE);
    details.setTechnicalMessage(StringUtils.join(new Object[] { errorMessage.getMessage(), errorMessage.getDetails() }, ". "));
    Set<IssueDetails> detailsList = new HashSet<>();
    detailsList.add(details);
    BaseExceptionResponse jsonProcessingExceptionResponse = new BaseExceptionResponse();
    jsonProcessingExceptionResponse.setIssueDetails(detailsList);
    return Response.status(response.getStatus()).type(response.getMediaType()).entity(jsonProcessingExceptionResponse).build();
}
Also used : Response(javax.ws.rs.core.Response) BaseExceptionResponse(gov.ca.cwds.rest.exception.BaseExceptionResponse) BaseExceptionResponse(gov.ca.cwds.rest.exception.BaseExceptionResponse) IssueDetails(gov.ca.cwds.rest.exception.IssueDetails) ErrorMessage(io.dropwizard.jersey.errors.ErrorMessage) HashSet(java.util.HashSet)

Example 3 with ErrorMessage

use of io.dropwizard.jersey.errors.ErrorMessage in project dropwizard by dropwizard.

the class FormsAppTest method failOnNoChunkedEncoding.

/**
 * Test confirms that chunked encoding has to be disabled in order for
 * sending forms to work. Maybe someday this requirement will be relaxed and
 * this test can be updated for the new behavior. For more info, see issues
 * #1013 and #1094
 */
@Test
void failOnNoChunkedEncoding() throws IOException {
    final Client client = new JerseyClientBuilder(RULE.getEnvironment()).using(config).build("test client 2");
    try (final FormDataMultiPart fdmp = new FormDataMultiPart()) {
        final MultiPart mp = fdmp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("file").fileName("fileName").build(), "CONTENT"));
        final String url = String.format("http://localhost:%d/uploadFile", RULE.getLocalPort());
        final Response response = client.target(url).register(MultiPartFeature.class).request().post(Entity.entity(mp, mp.getMediaType()));
        assertThat(response.getStatus()).isEqualTo(400);
        assertThat(response.readEntity(ErrorMessage.class)).isEqualTo(new ErrorMessage(400, "HTTP 400 Bad Request"));
    }
}
Also used : Response(javax.ws.rs.core.Response) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) Client(javax.ws.rs.client.Client) ErrorMessage(io.dropwizard.jersey.errors.ErrorMessage) JerseyClientBuilder(io.dropwizard.client.JerseyClientBuilder) Test(org.junit.jupiter.api.Test)

Example 4 with ErrorMessage

use of io.dropwizard.jersey.errors.ErrorMessage in project dropwizard by dropwizard.

the class JsonProcessingExceptionMapper method toResponse.

@Override
public Response toResponse(JsonProcessingException exception) {
    /*
         * If the error is in the JSON generation or an invalid definition, it's a server error.
         */
    if (exception instanceof JsonGenerationException || exception instanceof InvalidDefinitionException) {
        // LoggingExceptionMapper will log exception
        return super.toResponse(exception);
    }
    /*
         * Otherwise, it's those pesky users.
         */
    logger.debug("Unable to process JSON", exception);
    final String message = exception.getOriginalMessage();
    final ErrorMessage errorMessage = new ErrorMessage(Response.Status.BAD_REQUEST.getStatusCode(), "Unable to process JSON", showDetails ? message : null);
    return Response.status(Response.Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON_TYPE).entity(errorMessage).build();
}
Also used : InvalidDefinitionException(com.fasterxml.jackson.databind.exc.InvalidDefinitionException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) ErrorMessage(io.dropwizard.jersey.errors.ErrorMessage)

Example 5 with ErrorMessage

use of io.dropwizard.jersey.errors.ErrorMessage in project dropwizard by dropwizard.

the class SizeParamTest method badValueThrowsException.

@Test
public void badValueThrowsException() throws Exception {
    final Throwable exn = catchThrowable(() -> new SizeParam("10 kelvins", "degrees"));
    assertThat(exn).isInstanceOf(WebApplicationException.class);
    final Response response = ((WebApplicationException) exn).getResponse();
    assertThat(response.getStatus()).isEqualTo(400);
    assertThat((ErrorMessage) response.getEntity()).isEqualTo(new ErrorMessage(400, "degrees is not a valid size."));
}
Also used : Response(javax.ws.rs.core.Response) WebApplicationException(javax.ws.rs.WebApplicationException) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) ErrorMessage(io.dropwizard.jersey.errors.ErrorMessage) Test(org.junit.Test)

Aggregations

ErrorMessage (io.dropwizard.jersey.errors.ErrorMessage)7 Response (javax.ws.rs.core.Response)3 WebApplicationException (javax.ws.rs.WebApplicationException)2 Test (org.junit.jupiter.api.Test)2 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)1 InvalidDefinitionException (com.fasterxml.jackson.databind.exc.InvalidDefinitionException)1 BaseExceptionResponse (gov.ca.cwds.rest.exception.BaseExceptionResponse)1 IssueDetails (gov.ca.cwds.rest.exception.IssueDetails)1 JerseyClientBuilder (io.dropwizard.client.JerseyClientBuilder)1 SQLException (java.sql.SQLException)1 HashSet (java.util.HashSet)1 Nullable (javax.annotation.Nullable)1 Client (javax.ws.rs.client.Client)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)1 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)1 FormDataBodyPart (org.glassfish.jersey.media.multipart.FormDataBodyPart)1 FormDataMultiPart (org.glassfish.jersey.media.multipart.FormDataMultiPart)1 MultiPart (org.glassfish.jersey.media.multipart.MultiPart)1 Test (org.junit.Test)1