Search in sources :

Example 1 with RestFailureMarshaller

use of com.peterphi.std.guice.web.rest.jaxrs.exception.RestFailureMarshaller in project stdlib by petergeneric.

the class GuicedResteasy method tryHandleException.

/**
 * @param ctx
 * @param response
 * @param t
 *
 * @throws ServletException
 * @throws IOException
 * @throws RuntimeException
 * @throws Error
 */
private void tryHandleException(HttpCallContext ctx, HttpServletResponse response, Throwable t) throws ServletException, IOException, RuntimeException, Error {
    if (suppressClientAbortExceptions && t instanceof UnhandledException && t.getCause() != null) {
        // Only look 20 exceptions deep
        int maxDepth = 20;
        Throwable cause = t.getCause();
        while (cause != null && --maxDepth > 0) {
            if (cause.getClass().getName().equals("org.apache.catalina.connector.ClientAbortException")) {
                ignoredAborts.mark();
                if (log.isTraceEnabled())
                    log.trace("Client aborted during request. Ignoring. Detail: " + ctx.getRequestInfo(), t);
                return;
            } else {
                cause = cause.getCause();
            }
        }
    }
    log.warn("Failure during " + ctx.getRequestInfo(), t);
    // If the response is already committed we can't render the exception elegantly
    if (response.isCommitted())
        rethrow(t);
    try {
        RestFailureMarshaller marshaller = new RestFailureMarshaller();
        RestFailure failure = marshaller.renderFailure(t);
        TwitterBootstrapRestFailurePageRenderer renderer = new TwitterBootstrapRestFailurePageRenderer(failure);
        // internal error
        response.setStatus(500);
        // Render the HTML
        final StringBuilder sb = new StringBuilder(4096);
        renderer.writeHTML(sb);
        response.getWriter().print(sb);
    } catch (Throwable newException) {
        log.warn("Error trying to present exception elegantly: ", newException);
        // Rethrow the original exception, it's not our problem anymore
        rethrow(t);
    }
}
Also used : UnhandledException(org.jboss.resteasy.spi.UnhandledException) RestFailure(com.peterphi.std.guice.restclient.jaxb.RestFailure) TwitterBootstrapRestFailurePageRenderer(com.peterphi.std.guice.web.rest.pagewriter.TwitterBootstrapRestFailurePageRenderer) RestFailureMarshaller(com.peterphi.std.guice.web.rest.jaxrs.exception.RestFailureMarshaller)

Aggregations

RestFailure (com.peterphi.std.guice.restclient.jaxb.RestFailure)1 RestFailureMarshaller (com.peterphi.std.guice.web.rest.jaxrs.exception.RestFailureMarshaller)1 TwitterBootstrapRestFailurePageRenderer (com.peterphi.std.guice.web.rest.pagewriter.TwitterBootstrapRestFailurePageRenderer)1 UnhandledException (org.jboss.resteasy.spi.UnhandledException)1