Wicket: Example implementation of IExceptionMapper
Posted by ozizka in Ondrej Zizka's Blog on Sep 26, 2012 10:16:19 PMimport org.apache.wicket.protocol.http.WebApplication; import org.apache.wicket.request.IExceptionMapper; import org.apache.wicket.request.IRequestHandler; import org.apache.wicket.request.handler.PageProvider; import org.apache.wicket.request.handler.RenderPageRequestHandler; import org.apache.wicket.request.mapper.parameter.PageParameters; import org.apache.wicket.util.IProvider; public class MyApplication extends WebApplication { /** * Maps exceptions to pages. */ @Override public IProvider<IExceptionMapper> getExceptionMapperProvider() { return new IProvider<IExceptionMapper>() { @Override public IExceptionMapper get() { return new IExceptionMapper() { final DefaultExceptionMapper def = new DefaultExceptionMapper(); @Override public IRequestHandler map( Exception ex ) { PageParameters par = new PageParameters().add("ex", ex); // Possible workaround for AS7-4554, WICKET-4785 - only for stateful pages. // java.lang.ClassNotFoundException: org.jboss.msc.service.ServiceName // at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:190) if( ClassNotFoundException.class.isInstance(ex) ){ if("org.jboss.msc.service.ServiceName".equals( ((ClassNotFoundException)ex).getMessage() ) ) return new RenderPageRequestHandler(new PageProvider(HomePage.class, par) ); } return def.map( ex ); } }; } }; } }
I plan to create some convenience MapExceptionMapper which would work like
new MapExceptionMapper() // Type, Destination .add( Exception.class, ExceptionPage.class ) .add( ... )
Sources:
https://cwiki.apache.org/WICKET/migration-to-wicket-15.html#MigrationtoWicket1.5-Exceptionhandling
Exception handling
In Wicket 1.4 it was needed to extend org.apache.wicket.RequestCycle.onRuntimeException(Page, RuntimeException).
Wicket 1.5 gives even better control:
- add your own org.apache.wicket.request.cycle.IRequestCycleListener (AbstractRequestCycleListener) with org.apache.wicket.Application.getRequestCycleListeners().add() and implement its #onException(RequestCycle, Exception)
- or override org.apache.wicket.Application.getExceptionMapperProvider() - the IExceptionMapper is used if none of the configured IRequestCycleListeners doesn't know how to handle the exception.
For information on how the request cycle handles exceptions see RequestCycle in Wicket 1.5 for more information
Comments