Search in sources :

Example 1 with PropertyBindingException

use of com.fasterxml.jackson.databind.exc.PropertyBindingException 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)

Aggregations

JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 InvalidFormatException (com.fasterxml.jackson.databind.exc.InvalidFormatException)1 PropertyBindingException (com.fasterxml.jackson.databind.exc.PropertyBindingException)1 ErrorMessage (io.dropwizard.jersey.errors.ErrorMessage)1