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: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]