Search in sources :

Example 1 with TwitterBootstrapRestFailurePageRenderer

use of com.peterphi.std.guice.web.rest.pagewriter.TwitterBootstrapRestFailurePageRenderer 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)

Example 2 with TwitterBootstrapRestFailurePageRenderer

use of com.peterphi.std.guice.web.rest.pagewriter.TwitterBootstrapRestFailurePageRenderer in project stdlib by petergeneric.

the class HTMLFailureRenderer method render.

@Override
public Response render(RestFailure failure) {
    if (!enabled)
        // don't run if we have been disabled
        return null;
    if (HttpCallContext.peek() == null)
        // Don't run unless we have an HttpCallContext
        return null;
    else if (!shouldRenderHtmlForRequest(HttpCallContext.get().getRequest()))
        // Don't run unless we should be rendering HTML for this request
        return null;
    else if (requireLoggedIn && isLoggedIn(requireRole))
        return null;
    TwitterBootstrapRestFailurePageRenderer writer = new TwitterBootstrapRestFailurePageRenderer(failure);
    // Optionally enable highlighting
    if (highlightEnabled) {
        if (StringUtils.equals(highlightTerms, GuiceConstants.JAXRS_EXCEPTION_HIGHLIGHT_SCAN_PACKAGES)) {
            // Read scan.packages and use this instead
            writer.setHighlightTerms(config.getList(GuiceProperties.SCAN_PACKAGES, Collections.emptyList()));
        } else {
            writer.setHighlightTerms(Arrays.asList(highlightTerms.split(",")));
        }
    }
    // Optionally enable JIRA integration
    if (jiraEnabled) {
        writer.enableJIRA(jiraEndpoint, jiraProjectId, jiraIssueType);
    }
    if (jvmInfoEnabled) {
        writer.enableJVMInfo();
    }
    if (jvmInfoEnvironmentVariablesEnabled) {
        writer.enableEnvironmentVariables();
    }
    if (stackTraceEnabled) {
        writer.enableStackTrace();
    }
    if (requestInfoEnabled) {
        writer.enableRequestInfo();
    }
    StringBuilder sb = new StringBuilder();
    writer.writeHTML(sb);
    ResponseBuilder builder = Response.status(failure.httpCode);
    builder.type(MediaType.TEXT_HTML_TYPE);
    builder.entity(sb.toString());
    return builder.build();
}
Also used : TwitterBootstrapRestFailurePageRenderer(com.peterphi.std.guice.web.rest.pagewriter.TwitterBootstrapRestFailurePageRenderer) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder)

Aggregations

TwitterBootstrapRestFailurePageRenderer (com.peterphi.std.guice.web.rest.pagewriter.TwitterBootstrapRestFailurePageRenderer)2 RestFailure (com.peterphi.std.guice.restclient.jaxb.RestFailure)1 RestFailureMarshaller (com.peterphi.std.guice.web.rest.jaxrs.exception.RestFailureMarshaller)1 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)1 UnhandledException (org.jboss.resteasy.spi.UnhandledException)1