Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.yammer.dropwizard.jersey;
 
 
    private static final Logger LOGGER = LoggerFactory.getLogger(JsonProcessingExceptionMapper.class);
    private static final Splitter LINE_SPLITTER = Splitter.on("\n").trimResults();
    @Context
    private HttpServletRequest request;
    private final UnbrandedErrorHandler errorHandler;
    public JsonProcessingExceptionMapper() {
        this. = new UnbrandedErrorHandler();
    }
    @Override
    public Response toResponse(JsonProcessingException exception) {
        /*
         * If the error is in the JSON generation, it's a server error.
         */
        if (exception instanceof JsonGenerationException) {
            .warn("Error generating JSON"exception);
            return Response.serverError().build();
        }
        final String message = exception.getMessage();
        /*
         * If we can't deserialize the JSON because someone forgot a no-arg constructor, it's a
         * server error and we should inform the developer.
         */
        if (message.startsWith("No suitable constructor found")) {
            .error("Unable to deserialize the specific type"exception);
            return Response.serverError().build();
        }
        /*
         * Otherwise, it's those pesky users.
         */
        try {
            .debug("Unable to process JSON"exception);
            final StringWriter writer = new StringWriter(4096);
            .writeErrorPage(,
                                        writer,
                                        400,
                                        stripLocation(message),
                                        false);
            return Response.status(..)
                           .type(.)
                           .entity(writer.toString())
                           .build();
        } catch (IOException e) {
            .debug("Unable to output error message"e);
            return Response.serverError().build();
        }
    }
    @SuppressWarnings("LoopStatementThatDoesntLoop")
    private String stripLocation(String message) {
        for (String s : .split(message)) {
            return s;
        }
        return message;
    }
New to GrepCode? Check out our FAQ X