Search in sources :

Example 1 with JsonGenerationException

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());
    }
}
Also used : RequestFailedException(com.cribbstechnologies.clients.mandrill.exception.RequestFailedException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) BaseMandrillRequest(com.cribbstechnologies.clients.mandrill.model.BaseMandrillRequest) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) Test(org.junit.Test)

Example 2 with JsonGenerationException

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());
    }
}
Also used : JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) Test(org.junit.Test)

Example 3 with JsonGenerationException

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();
}
Also used : PropertyBindingException(com.fasterxml.jackson.databind.exc.PropertyBindingException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) ErrorMessage(io.dropwizard.jersey.errors.ErrorMessage) InvalidFormatException(com.fasterxml.jackson.databind.exc.InvalidFormatException)

Example 4 with JsonGenerationException

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"));
}
Also used : JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException)

Example 5 with JsonGenerationException

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"));
}
Also used : JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException)

Aggregations

JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)17 IOException (java.io.IOException)9 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)8 Test (org.junit.Test)3 RequestFailedException (com.cribbstechnologies.clients.mandrill.exception.RequestFailedException)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 BaseMandrillRequest (com.cribbstechnologies.clients.mandrill.model.BaseMandrillRequest)1 MandrillError (com.cribbstechnologies.clients.mandrill.model.MandrillError)1 BaseMandrillStringResponse (com.cribbstechnologies.clients.mandrill.model.response.BaseMandrillStringResponse)1 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 InvalidFormatException (com.fasterxml.jackson.databind.exc.InvalidFormatException)1 PropertyBindingException (com.fasterxml.jackson.databind.exc.PropertyBindingException)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 JaxbAnnotationModule (com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule)1 JsonLdError (com.github.jsonldjava.core.JsonLdError)1 ErrorMessage (io.dropwizard.jersey.errors.ErrorMessage)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 StringWriter (java.io.StringWriter)1