BadRequestExceptionMapper implements ExceptionMapper<BadRequestException> { @Override public Response toResponse(final BadRequestException exception) { logger.debug("Bad Request", exception); final JsonObjectBuilder builder = createObjectBuilder().add("error", exception.getMessage()); if (exception.getCause() instanceof JsonSchemaValidationException) { builder.add("validationErrors", jsonValidationLoggerHelper.toJsonObject((JsonSchemaValidationException) exception.getCause())); } return status(BAD_REQUEST) .entity(builder.build().toString()) .build(); }  @Override Response toResponse(final BadRequestException exception);  }
@Test public void shouldReturn400ResponseForBadRequestException() throws Exception { final Response response = exceptionMapper.toResponse(new BadRequestException(TEST_ERROR_MESSAGE)); assertThat(response.getStatus(), is(BAD_REQUEST.getStatusCode())); assertThat(response.getEntity(), notNullValue()); assertThat(response.getEntity().toString(), hasJsonPath("$.error", equalTo(TEST_ERROR_MESSAGE))); }
@Test public void shouldAddJsonValidationErrorsToResponse() { final ValidationException validationException = new ValidationException(schema, "Test Json"); final JsonSchemaValidationException jsonSchemaValidationException = new JsonSchemaValidationException(validationException.getMessage(), validationException); final BadRequestException badRequestException = new BadRequestException(TEST_ERROR_MESSAGE, jsonSchemaValidationException); final Response response = exceptionMapper.toResponse(badRequestException); final String body = response.getEntity().toString(); assertThat(body, hasJsonPath("$.validationErrors.message", equalTo("#: Test Json"))); }