Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.yammer.dropwizard.jersey;
 
 
// TODO: 10/12/11 <coda> -- write tests for LoggingExceptionMapper
// TODO: 10/12/11 <coda> -- write docs for LoggingExceptionMapper
public class LoggingExceptionMapper<E extends Throwableimplements ExceptionMapper<E> {
    private static final Logger LOGGER = LoggerFactory.getLogger(LoggingExceptionMapper.class);
    private static final Random RANDOM = new Random();
    @Context
    private HttpServletRequest request;
    private final UnbrandedErrorHandler errorHandler = new UnbrandedErrorHandler();
    @Override
    public Response toResponse(E exception) {
        if (exception instanceof WebApplicationException) {
            return ((WebApplicationExceptionexception).getResponse();
        }
        final StringWriter writer = new StringWriter(4096);
        try {
            final long id = randomId();
            logException(idexception);
            .writeErrorPage(,
                                        writer,
                                        500,
                                        formatResponseEntity(idexception),
                                        false);
        } catch (IOException e) {
            .warn("Unable to generate error page"e);
        }
        return Response.serverError()
                       .type(.)
                       .entity(writer.toString())
                       .build();
    }
    protected void logException(long id, E exception) {
        .error(formatLogMessage(idexception), exception);
    }
    @SuppressWarnings("UnusedParameters")
    protected String formatResponseEntity(long idThrowable exception) {
        return String.format("There was an error processing your request. It has been logged (ID %016x).\n"id);
    }
    @SuppressWarnings("UnusedParameters")
    protected String formatLogMessage(long idThrowable exception) {
        return String.format("Error handling a request: %016x"id);
    }
    protected static long randomId() {
        return .nextLong();
    }
New to GrepCode? Check out our FAQ X