You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@beehive.apache.org by Adam Jenkins <ma...@adamjenkins.net> on 2005/07/02 03:14:37 UTC

catching exceptions from all over the place?

Hi Guys,

Hope everyone is well.  I was wondering if there is a way to set a
default handler method for an exception regardless of from where it's
thrown

i.e.  I have the following controllers:

/admin/changepassword/Controller.java
/certs/requests/Controller.java
/Controller.java

And I have a handler method in /Controller.java such:

    @Jpf.ExceptionHandler(
        forwards={
            @Jpf.Forward(name="handled",
tilesDefinition="datasystem_exception")
        }
    )
    public Forward handleDataSystemException(
            final DataSystemException exception,
            final String actionName,
            final String message,
            final Object formBean){
        log.error("Error executing action " + actionName, exception);
        return new Forward("handled");
    }

I have a method in /certs/requests/Controller.java such:


        @Jpf.Action(
                forwards={
                   @Jpf.Forward(name="success",
tilesDefinition="requests")
                }
        )
        protected Forward begin() throws DataSystemException {
        log.debug("/certs/requests/Controller.begin()");
        final Collection<Request> currentRequests =
OJBUtils.<Request>loadAll(Request.class, DBKEY);
        if(log.isDebugEnabled()){
            log.debug("Loaded Request: " + currentRequests);
        }
        final Forward forward = new Forward("success");
        forward.addActionOutput("requests", currentRequests);
        return forward;
        }


Now...DataSystemException can pretty much be thrown from anywhere in the
app that is doing anything with the database (ojb actually)...is there
any way I can make them all route to the method above
in /Controller.java (for example, above if thrown
from /certs/requests/Controller.begin() I want it handled
by /Controller.handleDataSystemException(...))?

Cheers
Adam


Re: catching exceptions from all over the place?

Posted by Rich Feit <ri...@gmail.com>.
Hi Adam,

There are two main ways to share an exception handler:

    1) put a @Jpf.Catch in a base class, or

    2) put a @Jpf.Catch in a shared flow (which, if referenced by a page 
flow, will automatically catch the exception):

        @Jpf.Controller(
            catches={
                @Jpf.Catch(type=DataSystemException.class, 
method="handleDataSystemException")
            }
        )
        public class ExceptionsSharedFlow
            extends SharedFlowController
        {
            @Jpf.ExceptionHandler...
        }

If you put it in a shared flow, then you can add the reference to an 
individual page flow like this:
        @Jpf.Controller(
            sharedFlowRefs={
                @Jpf.SharedFlowRef(name="exceptionsSharedFlow", 
type=example.ExceptionsSharedFlow.class)
            }
        )

...or you can make the shared flow apply for *all* page flows by adding 
it in beehive-netui-config.xml 
(http://incubator.apache.org/beehive/pageflow/config/beehive-netui-config.html#structure):

        <default-shared-flow-refs>
            <shared-flow-ref>
                <name>exceptionsSharedFlow</name>
                <type>example.ExceptionsSharedFlow</type>
            </shared-flow-ref>
        </default-shared-flow-refs>


Doing exception-handling through a shared flow is useful when the 
exception handler needs to access shared state (since a single instance 
of the shared flow class is created/accessed in the session).  It's also 
useful when adding a base class is impossible/impractical.

Let me know if you need more information on any of this.

Rich

Adam Jenkins wrote:

>Hi Guys,
>
>Hope everyone is well.  I was wondering if there is a way to set a
>default handler method for an exception regardless of from where it's
>thrown
>
>i.e.  I have the following controllers:
>
>/admin/changepassword/Controller.java
>/certs/requests/Controller.java
>/Controller.java
>
>And I have a handler method in /Controller.java such:
>
>    @Jpf.ExceptionHandler(
>        forwards={
>            @Jpf.Forward(name="handled",
>tilesDefinition="datasystem_exception")
>        }
>    )
>    public Forward handleDataSystemException(
>            final DataSystemException exception,
>            final String actionName,
>            final String message,
>            final Object formBean){
>        log.error("Error executing action " + actionName, exception);
>        return new Forward("handled");
>    }
>
>I have a method in /certs/requests/Controller.java such:
>
>
>        @Jpf.Action(
>                forwards={
>                   @Jpf.Forward(name="success",
>tilesDefinition="requests")
>                }
>        )
>        protected Forward begin() throws DataSystemException {
>        log.debug("/certs/requests/Controller.begin()");
>        final Collection<Request> currentRequests =
>OJBUtils.<Request>loadAll(Request.class, DBKEY);
>        if(log.isDebugEnabled()){
>            log.debug("Loaded Request: " + currentRequests);
>        }
>        final Forward forward = new Forward("success");
>        forward.addActionOutput("requests", currentRequests);
>        return forward;
>        }
>
>
>Now...DataSystemException can pretty much be thrown from anywhere in the
>app that is doing anything with the database (ojb actually)...is there
>any way I can make them all route to the method above
>in /Controller.java (for example, above if thrown
>from /certs/requests/Controller.begin() I want it handled
>by /Controller.handleDataSystemException(...))?
>
>Cheers
>Adam
>
>
>  
>