You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Joachim Rohde <jr...@solvit.de> on 2016/09/07 09:36:34 UTC
How to correctly implement a custom error page
Hi,
I need to enrich the ExceptionErrorPage with some additional information. Currently I have a working solution but I'm
not sure, if this is the correct way.
My first try was (in my Application-class):
/**
* Define our own exception mapper.
*/
@Override
public IProvider<IExceptionMapper> getExceptionMapperProvider() {
return new IProvider<IExceptionMapper>() {
@Override
public IExceptionMapper get() {
return new MyCustomExceptionMapper();
}
};
}
/**
* Our own exception mapper, so that we can display further information on the error page
* in case of an exception.
*/
public class MyCustomExceptionMapper extends DefaultExceptionMapper {
@Override
public IRequestHandler map(Exception e) {
return new RenderPageRequestHandler(new PageProvider(new MyErrorPage(e, super.extractCurrentPage())));
}
}
MyErrorPage extends from ExceptionErrorPage and overwrites getErrorMessage(Throwable throwable).
During development this works just fine but it was not feeling right. Therefore I proposed a patch to implement a
hook-method (https://issues.apache.org/jira/browse/WICKET-6240) where Martin mentioned:
"The recommended way to do this is to register custom IRequestCycleListener and override its #onException() method.
If the exception is IWicketInternalException then return null, otherwise return RenderPageRequestHandler with a custom
page that renders all the information you need."
So, at the moment I'm having in my init-method:
getRequestCycleListeners().add(new AbstractRequestCycleListener() {
@Override
public IRequestHandler onException(RequestCycle cycle, Exception ex) {
if (ex instanceof IWicketInternalException) {
return null;
} else {
Page page = null;
IRequestHandler handler = cycle.getActiveRequestHandler();
if (handler == null) {
handler = cycle.getRequestHandlerScheduledAfterCurrent();
}
if (handler instanceof IPageRequestHandler) {
IPageRequestHandler pageRequestHandler = (IPageRequestHandler) handler;
page = (Page) pageRequestHandler.getPage();
}
return new RenderPageRequestHandler(new PageProvider(new MyErrorPage(ex, page)));
}
}
});
The code to retrieve the page comes basically from DefaultExceptionMapper#extractCurrentPage(). My question: is this the
way to go? Or can I simplify the code any further?
Joachim
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: How to correctly implement a custom error page
Posted by Joachim Rohde <jr...@solvit.de>.
Thanks. Again I learned something new.
On 09/07/2016 12:47 PM, Martin Grigorov wrote:
> Hi,
>
> You can also use PageRequestHandlerTracker to get a reference to the
> requested page (first handler) and to the response page (last handler).
> Everything else looks good to me!
>
> Martin Grigorov
> Wicket Training and Consulting
> https://twitter.com/mtgrigorov
>
> On Wed, Sep 7, 2016 at 11:36 AM, Joachim Rohde <jr...@solvit.de> wrote:
>
>> Hi,
>>
>> I need to enrich the ExceptionErrorPage with some additional information.
>> Currently I have a working solution but I'm
>> not sure, if this is the correct way.
>>
>> My first try was (in my Application-class):
>>
>> /**
>> * Define our own exception mapper.
>> */
>> @Override
>> public IProvider<IExceptionMapper> getExceptionMapperProvider() {
>> return new IProvider<IExceptionMapper>() {
>>
>> @Override
>> public IExceptionMapper get() {
>> return new MyCustomExceptionMapper();
>> }
>> };
>> }
>>
>> /**
>> * Our own exception mapper, so that we can display further
>> information on the error page
>> * in case of an exception.
>> */
>> public class MyCustomExceptionMapper extends DefaultExceptionMapper {
>>
>> @Override
>> public IRequestHandler map(Exception e) {
>> return new RenderPageRequestHandler(new PageProvider(new
>> MyErrorPage(e, super.extractCurrentPage())));
>> }
>>
>> }
>>
>> MyErrorPage extends from ExceptionErrorPage and overwrites
>> getErrorMessage(Throwable throwable).
>>
>> During development this works just fine but it was not feeling right.
>> Therefore I proposed a patch to implement a
>> hook-method (https://issues.apache.org/jira/browse/WICKET-6240) where
>> Martin mentioned:
>>
>> "The recommended way to do this is to register custom
>> IRequestCycleListener and override its #onException() method.
>> If the exception is IWicketInternalException then return null, otherwise
>> return RenderPageRequestHandler with a custom
>> page that renders all the information you need."
>>
>> So, at the moment I'm having in my init-method:
>>
>> getRequestCycleListeners().add(new AbstractRequestCycleListener() {
>> @Override
>> public IRequestHandler onException(RequestCycle cycle,
>> Exception ex) {
>> if (ex instanceof IWicketInternalException) {
>> return null;
>> } else {
>> Page page = null;
>>
>> IRequestHandler handler =
>> cycle.getActiveRequestHandler();
>>
>> if (handler == null) {
>> handler = cycle.getRequestHandlerScheduledAfte
>> rCurrent();
>> }
>>
>> if (handler instanceof IPageRequestHandler) {
>> IPageRequestHandler pageRequestHandler =
>> (IPageRequestHandler) handler;
>> page = (Page) pageRequestHandler.getPage();
>> }
>>
>> return new RenderPageRequestHandler(new
>> PageProvider(new MyErrorPage(ex, page)));
>> }
>> }
>>
>> });
>>
>> The code to retrieve the page comes basically from DefaultExceptionMapper#extractCurrentPage().
>> My question: is this the
>> way to go? Or can I simplify the code any further?
>>
>> Joachim
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: How to correctly implement a custom error page
Posted by Martin Grigorov <mg...@apache.org>.
Hi,
You can also use PageRequestHandlerTracker to get a reference to the
requested page (first handler) and to the response page (last handler).
Everything else looks good to me!
Martin Grigorov
Wicket Training and Consulting
https://twitter.com/mtgrigorov
On Wed, Sep 7, 2016 at 11:36 AM, Joachim Rohde <jr...@solvit.de> wrote:
> Hi,
>
> I need to enrich the ExceptionErrorPage with some additional information.
> Currently I have a working solution but I'm
> not sure, if this is the correct way.
>
> My first try was (in my Application-class):
>
> /**
> * Define our own exception mapper.
> */
> @Override
> public IProvider<IExceptionMapper> getExceptionMapperProvider() {
> return new IProvider<IExceptionMapper>() {
>
> @Override
> public IExceptionMapper get() {
> return new MyCustomExceptionMapper();
> }
> };
> }
>
> /**
> * Our own exception mapper, so that we can display further
> information on the error page
> * in case of an exception.
> */
> public class MyCustomExceptionMapper extends DefaultExceptionMapper {
>
> @Override
> public IRequestHandler map(Exception e) {
> return new RenderPageRequestHandler(new PageProvider(new
> MyErrorPage(e, super.extractCurrentPage())));
> }
>
> }
>
> MyErrorPage extends from ExceptionErrorPage and overwrites
> getErrorMessage(Throwable throwable).
>
> During development this works just fine but it was not feeling right.
> Therefore I proposed a patch to implement a
> hook-method (https://issues.apache.org/jira/browse/WICKET-6240) where
> Martin mentioned:
>
> "The recommended way to do this is to register custom
> IRequestCycleListener and override its #onException() method.
> If the exception is IWicketInternalException then return null, otherwise
> return RenderPageRequestHandler with a custom
> page that renders all the information you need."
>
> So, at the moment I'm having in my init-method:
>
> getRequestCycleListeners().add(new AbstractRequestCycleListener() {
> @Override
> public IRequestHandler onException(RequestCycle cycle,
> Exception ex) {
> if (ex instanceof IWicketInternalException) {
> return null;
> } else {
> Page page = null;
>
> IRequestHandler handler =
> cycle.getActiveRequestHandler();
>
> if (handler == null) {
> handler = cycle.getRequestHandlerScheduledAfte
> rCurrent();
> }
>
> if (handler instanceof IPageRequestHandler) {
> IPageRequestHandler pageRequestHandler =
> (IPageRequestHandler) handler;
> page = (Page) pageRequestHandler.getPage();
> }
>
> return new RenderPageRequestHandler(new
> PageProvider(new MyErrorPage(ex, page)));
> }
> }
>
> });
>
> The code to retrieve the page comes basically from DefaultExceptionMapper#extractCurrentPage().
> My question: is this the
> way to go? Or can I simplify the code any further?
>
> Joachim
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>