struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Hubert Rabago <>
Subject Re: Exceptions and ActionErrors
Date Fri, 25 Mar 2005 21:33:26 GMT
I can't remember personally running across this situation, and maybe
that's because what I try to communicate changes between levels/tiers.

In the business tier, I tend to log messages that would help me debug
problems should any arise.  Hopefully, I would log parameters and
values calculated/discovered during method execution.

On the Action class level, I usually don't log messages when the
execption class is specific to my application.  That is, if I catch
MyException or any subclass, the method that threw that exception
should've logged it already, so I don't need to log it.  If I get
something else, say, RemoteException or some other exception that my
business tier didn't catch (or didn't cause), then I log it.

This not only reduces code duplication, it also reduces the size of my
log files.

The error messages I show to the user is entirely different.  In fact,
they could be so different that the user wouldn't be able to tell the
two messages were related (if the user ever saw the app logs).

In your case, if you needed to log messages in the business tier, and
you're using the same messages anyway, why not just log the message
carried by the exception object you caught?

If what you want to log in the Action is what's being shown to the
user, there are methods you can use to have the message evaluated into
a concrete String you can log.

If they all really need to be the same, you can share resource files
between the two tiers, then let the business tier access its messages
from the resource file instead of hardcoding them.


On Fri, 25 Mar 2005 15:12:24 -0500, Matt Hughes <> wrote:
> In my current application, business logic is often scattered throughout
> my Action classes.  I am trying desperately to undo this 8th deadly sin,
> and while I have, I've noticed how Exception(s) and ActionError(s) are
> really being used with the exact same functionality.
> You might see something like this:
> <CODE>
> try {
>    user = session.dbGetUser(user_id);
> }catch (SQLException sqlE) {
>    __logger.error("Could not find user with user_id: " + user_id);
>    errors.add(ActionErrors.GLOBAL_ERROR, new
> ActionError("global.error.not_found.user", user_id);
>    return mapping.findForward(FAILURE);
> }
> </CODE>
> My first gripe looking at this is that I basically have to log the same
> error as I am creating with the ActionError.  Isn't there anyway to just
> log the message of an ActionError when it is created without coupling
> too much to the ActionError class?
> Ok now I rewrite the code to use a more suitable Exception and untie the
> Action class from DB access and SQLExceptions:
> -------------------------UserDAO ---------------------
> <CODE>
> public User getUser(int userId) throws ObjectNotFoundException {
>    try {
>       return dbGetUser(userId);
>    } catch (SQLException e) {
>       throw new ObjectNotFoundException("Could not find user with
> userId: " + userId);
>    }
> }
> </CODE>
> ------------Action class ----------------
> <CODE>
> try {
>    user = session.getUser(user_id);
> }catch (ObjectNotFoundException sqlE) {
>    __logger.error("Could not find user with user_id: " + user_id);
>    errors.add(ActionErrors.GLOBAL_ERROR, new
> ActionError("global.error.not_found.user", user_id);
>    return mapping.findForward(FAILURE);
> }
> </CODE>
> Now I am replicating the error message THREE times instead of TWO!
> Notice that I passed in ObjectNotFoundException the relevant message.
> So I guess my question is, if you are able to refactor your Actions so
> they are only calling business logic classes and those business logic
> classes throw well-messaged Exceptions, why not just have a super class
> that combines these three messages in one place (assuming the Exception
> is an exit condition, which is almost always is for me).
> public class SuperAction {
>    public ActionForward performAction(ActionMapping mapping, ActionForm
> form, HttpServletRequest request, HttpServletResponse response){
>       try {
>          //call perform method on subclasses
>       } catch (FddException fddE) {
>          //catch any fatal exceptions
>          __logger.error(fddE.getMessage(), fddE);
>          errors.add(ActionErrors.GLOBAL_ERROR, new
> ActionError(fddE.getMessage));
>          return mapping.findForward(FAILURE);
>        }
>    }
> }
> If the Exception isn't fatal, than you catch it in the sub-Action and
> handle it accordingly.  But this way, every fatal method call in your
> Actions will just be handled in one place no matter what the message.
> Doing this in my code would reduce my Actions by 20-30 lines easily.
> The only problem I see is that when the Exception is
> being thrown, it isn't throwing a Localized message, but that should be
> relatively easy to take care of.  Any thoughts?
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message