Search in sources :

Example 1 with ExceptionHandlerAssistant

use of org.apache.tapestry5.ExceptionHandlerAssistant in project tapestry-5 by apache.

the class DefaultRequestExceptionHandlerTest method setup_tests.

@BeforeMethod
public void setup_tests() throws Exception {
    mockConfiguration.clear();
    pageCache = mockRequestPageCache();
    renderer = mockPageResponseRenderer();
    logger = mockLogger();
    request = mockRequest();
    response = mockResponse();
    componentClassResolver = mockComponentClassResolver();
    linkSource = mockLinkSource();
    serviceResources = mockServiceResources();
    mockConfiguration.put(AccessControlException.class, MyPage.class);
    mockConfiguration.put(MyContextAwareException.class, new ExceptionHandlerAssistant() {

        public Object handleRequestException(Throwable exception, List<Object> exceptionContext) throws IOException {
            return null;
        }
    });
    ExceptionReporter noopExceptionReporter = new ExceptionReporter() {

        @Override
        public void reportException(Throwable exception) {
        }
    };
    exceptionHandler = new DefaultRequestExceptionHandler(pageCache, renderer, logger, "exceptionpage", request, response, componentClassResolver, linkSource, serviceResources, noopExceptionReporter, false, mockConfiguration);
}
Also used : ExceptionHandlerAssistant(org.apache.tapestry5.ExceptionHandlerAssistant) ExceptionReporter(org.apache.tapestry5.services.ExceptionReporter) IOException(java.io.IOException) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 2 with ExceptionHandlerAssistant

use of org.apache.tapestry5.ExceptionHandlerAssistant in project tapestry-5 by apache.

the class DefaultRequestExceptionHandler method handleRequestException.

/**
 * Handles the exception thrown at some point the request was being processed
 *
 * First checks if there was a specific exception handler/page configured for this exception type, it's super class or super-super class.
 * Renders the default exception page if none was configured.
 *
 * @param exception
 *         The exception that was thrown
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public void handleRequestException(Throwable exception) throws IOException {
    // skip handling of known exceptions if there are none configured
    if (configuration.isEmpty()) {
        renderException(exception);
        return;
    }
    Throwable cause = exception;
    // Throw away the wrapped exceptions first
    while (cause instanceof TapestryException) {
        if (cause.getCause() == null)
            break;
        cause = cause.getCause();
    }
    Class<?> causeClass = cause.getClass();
    if (!configuration.containsKey(causeClass)) {
        // try at most two level of superclasses before delegating back to the default exception handler
        causeClass = causeClass.getSuperclass();
        if (causeClass == null || !configuration.containsKey(causeClass)) {
            causeClass = causeClass.getSuperclass();
            if (causeClass == null || !configuration.containsKey(causeClass)) {
                renderException(exception);
                return;
            }
        }
    }
    Object[] exceptionContext = formExceptionContext(cause);
    Object value = configuration.get(causeClass);
    Object page = null;
    ExceptionHandlerAssistant assistant = null;
    if (value instanceof ExceptionHandlerAssistant) {
        assistant = (ExceptionHandlerAssistant) value;
        // in case the assistant changes the context
        List context = Arrays.asList(exceptionContext);
        page = assistant.handleRequestException(exception, context);
        exceptionContext = context.toArray();
    } else if (!(value instanceof Class)) {
        renderException(exception);
        return;
    } else
        page = value;
    if (page == null)
        return;
    try {
        if (page instanceof Class)
            page = componentClassResolver.resolvePageClassNameToPageName(((Class) page).getName());
        Link link = page instanceof Link ? (Link) page : linkSource.createPageRenderLink(page.toString(), false, exceptionContext);
        if (request.isXHR()) {
            OutputStream os = response.getOutputStream("application/json;charset=UTF-8");
            JSONObject reply = new JSONObject();
            reply.in(InternalConstants.PARTIAL_KEY).put("redirectURL", link.toRedirectURI());
            os.write(reply.toCompactString().getBytes("UTF-8"));
            os.close();
            return;
        }
        // Normal behavior is just a redirect.
        response.sendRedirect(link);
    }// user's responsibility not to abuse the mechanism
     catch (Exception e) {
        logger.warn("A new exception was thrown while trying to handle an instance of {}.", exception.getClass().getName(), e);
        // Nothing to do but delegate
        renderException(exception);
    }
}
Also used : ExceptionHandlerAssistant(org.apache.tapestry5.ExceptionHandlerAssistant) JSONObject(org.apache.tapestry5.json.JSONObject) OutputStream(java.io.OutputStream) JSONObject(org.apache.tapestry5.json.JSONObject) List(java.util.List) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException) Link(org.apache.tapestry5.http.Link) OperationException(org.apache.tapestry5.ioc.internal.OperationException) IOException(java.io.IOException) ComponentEventException(org.apache.tapestry5.runtime.ComponentEventException) ContextAwareException(org.apache.tapestry5.ContextAwareException) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException)

Example 3 with ExceptionHandlerAssistant

use of org.apache.tapestry5.ExceptionHandlerAssistant in project tapestry-5 by apache.

the class DefaultRequestExceptionHandlerTest method handleRequestExceptionWithConfiguredAssistant.

@Test
public void handleRequestExceptionWithConfiguredAssistant() throws IOException {
    ExceptionHandlerAssistant assistant = new ExceptionHandlerAssistant() {

        public Object handleRequestException(Throwable exception, List<Object> exceptionContext) throws IOException {
            return null;
        }
    };
    mockConfiguration.put(MyContextAwareException.class, assistant);
    replay();
    exceptionHandler.handleRequestException(new MyContextAwareException(new Object[] {}));
}
Also used : ExceptionHandlerAssistant(org.apache.tapestry5.ExceptionHandlerAssistant) List(java.util.List) Test(org.testng.annotations.Test)

Aggregations

ExceptionHandlerAssistant (org.apache.tapestry5.ExceptionHandlerAssistant)3 IOException (java.io.IOException)2 List (java.util.List)2 OutputStream (java.io.OutputStream)1 ContextAwareException (org.apache.tapestry5.ContextAwareException)1 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)1 Link (org.apache.tapestry5.http.Link)1 OperationException (org.apache.tapestry5.ioc.internal.OperationException)1 JSONObject (org.apache.tapestry5.json.JSONObject)1 ComponentEventException (org.apache.tapestry5.runtime.ComponentEventException)1 ExceptionReporter (org.apache.tapestry5.services.ExceptionReporter)1 BeforeMethod (org.testng.annotations.BeforeMethod)1 Test (org.testng.annotations.Test)1