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);
}
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);
}
}
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[] {}));
}
Aggregations