On 3/10/08, James Carman <[EMAIL PROTECTED]> wrote:
> On 3/10/08, Igor Vaynberg <[EMAIL PROTECTED]> wrote:
> > On Mon, Mar 10, 2008 at 7:39 PM, James Carman
> > <[EMAIL PROTECTED]> wrote:
> > > If I'm developing a Hibernate-based application and I want to install
> > > some global StateObjectStateException handling code, what's the best
> > > way to do it? I could override Application.newRequestCycle()
> > > providing my own request cycle implementation which overrides the
> > > onRuntimeException() method. Is there a way to plug in logic which
> > > says "if you see exception type X, use this handler"?
> >
> >
> > notice requestcycle.onruntimeexception() has access to the exception,
> > and returns a page, so
> >
> > myrc.onruntimexception(runtimeexception e) {
> > if (e.getrootcause() instanceof hibernateexception) {
> > return new hibernateerrorpage(e);
> > }
> > }
>
>
> Okay, so this is the way to handle it, eh? I just wanted to make sure
> there was nothing out there already for this. I may make up a
> Spring-based solution that allows me to "register" an exception
> handler for specific types of runtime exceptions. That way, my forms
> don't need to know I'm using Hibernate. They can just deal with my
> domain interface (a repository). Thanks for the tip!
>
What if we changed IRequestCycleSettings to include these methods:
public void addRuntimeExceptionHandler(Class exceptionClass,
IRuntimeExceptionHandler handler);
public IRuntimeExceptionHandler getRuntimeExceptionHandler(RuntimeException e);
Then, add the IRuntimeExceptionHandler interface:
public interface IRuntimeExceptionHandler
{
public Page onRuntimeException(Page page, RuntimeException e);
}
Then, RequestCycle's onRuntimeException() method as follows:
public void onRuntimeException(Page page, RuntimeException e)
{
IRuntimeExceptionHandler handler =
Application.get().getRequestCycleSettings().getRuntimeExceptionHandler(e);
if( handler != null )
{
return handler.onRuntimeException(page,e);
}
return null;
}
This way, folks could install their own exception handlers very
easily. The getRuntimeExceptionHandler() method would do a search up
the class hierarchy if necessary, so you could install a handler for
IOException which would cover FIleNotFoundException, for instance.
What do you think?
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]