use of com.fasterxml.jackson.core.JsonGenerationException in project Java-Mandrill-Wrapper by cribbstechnologies.
the class MandrillRESTRequestTest method testPostRequestMapperExceptions.
@Test
public void testPostRequestMapperExceptions() throws ClientProtocolException, IOException {
this.request = new MandrillRESTRequest();
this.request.setHttpClient(this.client);
this.request.setConfig(this.config);
this.request.setObjectMapper(this.mapper);
doThrow(new JsonGenerationException("Mockito!")).when(this.mapper).writeValueAsString(isA(BaseMandrillRequest.class));
try {
this.request.postRequest(this.emptyBaseRequest, "test", null);
fail("Exception not thrown");
} catch (RequestFailedException e) {
assertEquals("Json Generation Exception", e.getMessage());
}
doThrow(new JsonMappingException("Mockito!")).when(this.mapper).writeValueAsString(isA(BaseMandrillRequest.class));
try {
this.request.postRequest(this.emptyBaseRequest, "test", null);
fail("Exception not thrown");
} catch (RequestFailedException e) {
assertEquals("Json Mapping Exception", e.getMessage());
}
}
use of com.fasterxml.jackson.core.JsonGenerationException in project Java-Mandrill-Wrapper by cribbstechnologies.
the class MandrillRESTRequestTest method testGetPostDataJsonGenerationException.
@Test
public void testGetPostDataJsonGenerationException() throws Exception {
this.initRequestWithMockedMapper();
Mockito.when(this.mapper.writeValueAsString(this.emptyBaseRequest)).thenThrow(new JsonGenerationException("Mockito!"));
try {
this.request.getPostData(this.emptyBaseRequest);
fail("Exception not thrown");
} catch (JsonGenerationException jge) {
assertEquals("Mockito!", jge.getMessage());
}
}
use of com.fasterxml.jackson.core.JsonGenerationException in project dropwizard by dropwizard.
the class JsonProcessingExceptionMapper method toResponse.
@Override
public Response toResponse(JsonProcessingException exception) {
/*
* If the error is in the JSON generation, it's a server error.
*/
if (exception instanceof JsonGenerationException) {
// LoggingExceptionMapper will log exception
return super.toResponse(exception);
}
final String message = exception.getOriginalMessage();
/*
* If we can't deserialize the JSON because someone forgot a no-arg
* constructor, or it is not known how to serialize the type it's
* a server error and we should inform the developer.
*/
if (exception instanceof JsonMappingException) {
final JsonMappingException ex = (JsonMappingException) exception;
final Throwable cause = Throwables.getRootCause(ex);
// Exceptions that denote an error on the client side
final boolean clientCause = cause instanceof InvalidFormatException || cause instanceof PropertyBindingException;
// Until completely foolproof mechanism can be worked out in coordination
// with Jackson on how to communicate client vs server fault, compare
// start of message with known server faults.
final boolean beanError = cause.getMessage() == null || (cause.getMessage().startsWith("No suitable constructor found") || cause.getMessage().startsWith("No serializer found for class") || (cause.getMessage().startsWith("Can not construct instance") && !WRONG_TYPE_REGEX.matcher(cause.getMessage()).find()));
if (beanError && !clientCause) {
// LoggingExceptionMapper will log exception
return super.toResponse(exception);
}
}
/*
* Otherwise, it's those pesky users.
*/
LOGGER.debug("Unable to process JSON", exception);
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 com.fasterxml.jackson.core.JsonGenerationException in project elasticsearch by elastic.
the class BaseXContentTestCase method expectArrayException.
private static void expectArrayException(ThrowingRunnable runnable) {
JsonGenerationException e = expectThrows(JsonGenerationException.class, runnable);
assertThat(e.getMessage(), containsString("Current context not Array"));
}
use of com.fasterxml.jackson.core.JsonGenerationException in project elasticsearch by elastic.
the class BaseXContentTestCase method expectValueException.
private static void expectValueException(ThrowingRunnable runnable) {
JsonGenerationException e = expectThrows(JsonGenerationException.class, runnable);
assertThat(e.getMessage(), containsString("expecting a value"));
}
Aggregations