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();
}
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();
}
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"));
}
}
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();
}
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."));
}
Aggregations