Search in sources :

Example 6 with ErrorMessage

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

the class BooleanParamTest method booleanParamNegativeTest.

private void booleanParamNegativeTest(@Nullable String input) {
    final ErrorMessage expected = new ErrorMessage(400, "Parameter must be \"true\" or \"false\".");
    assertThatExceptionOfType(WebApplicationException.class).isThrownBy(() -> new BooleanParam(input)).satisfies(e -> assertThat(e.getResponse().getStatus()).isEqualTo(400)).satisfies(e -> assertThat(e.getResponse().getEntity()).isEqualTo(expected));
}
Also used : Test(org.junit.jupiter.api.Test) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) WebApplicationException(javax.ws.rs.WebApplicationException) ErrorMessage(io.dropwizard.jersey.errors.ErrorMessage) Nullable(javax.annotation.Nullable) WebApplicationException(javax.ws.rs.WebApplicationException) ErrorMessage(io.dropwizard.jersey.errors.ErrorMessage)

Example 7 with ErrorMessage

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

the class AbstractParam method error.

/**
 * Given a string representation which was unable to be parsed and the exception thrown, produce
 * a {@link Response} to be sent to the client.
 *
 * By default, generates a {@code 400 Bad Request} with a plain text entity generated by
 * {@link #errorMessage(Exception)}.
 *
 * @param input the raw input value
 * @param e the exception thrown while parsing {@code input}
 * @return the {@link Response} to be sent to the client
 */
protected Response error(@Nullable String input, Exception e) {
    LOGGER.debug("Invalid input received: {}", input);
    String errorMessage = errorMessage(e);
    if (errorMessage.contains("%s")) {
        errorMessage = String.format(errorMessage, parameterName);
    }
    return Response.status(getErrorStatus()).entity(new ErrorMessage(getErrorStatus().getStatusCode(), errorMessage)).type(mediaType()).build();
}
Also used : ErrorMessage(io.dropwizard.jersey.errors.ErrorMessage)

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