Shailesh Arvind Vaishampayan created WW-4234:
------------------------------------------------

             Summary: Ability to catch service layer exceptions at one place 
and allow adding action messages/errors before results are rendered
                 Key: WW-4234
                 URL: https://issues.apache.org/jira/browse/WW-4234
             Project: Struts 2
          Issue Type: New Feature
    Affects Versions: 2.3.15.3
            Reporter: Shailesh Arvind Vaishampayan


I am working with an application involoving Struts2 in web layer and Spring in 
business layer. I also have BusinessException class which will be used by all 
business services to create business related validation failures which must go 
up to web layer and should be show to users as validation message. I can easily 
do this by writing in my Action class:

      ClientAction extends ActionSupport throws Exception{
         ....
        try{
            clientService.searchClient();
        }catch(InvalidClientSearchCriteriaException e){
              addActionMessage("Invlid Search Criteria");
        }
        ...
And similar code in every action class. However i dont want to pollute my 
action classes with try catch blocks. Instead it would be much better if i can 
write on try catch block at one place and catch all the exceptions there as 
BusinessExceptions and create messages /errors from embedded messages/errors in 
those exceptions. One approach i could think of was to use interceptor or 
preResultListener. But I cannot use interceptor like below which catches 
business exceptions thrown from action classes...

    ExceptionInterceptor extends AbstractInterceptor(ActionInvocation 
ivocation,...){              
          try{
                    invocation.invoke();
          }catch(Exception e){
              if(e instanceof BusinessException){
                   ActionSupport as = (ActionSupport)invocation.getAction();
                   String message = extractMessagefromException()//--custom 
method to extract message embedded in exception.  
               as.addActionMessages(message);
      //-- above will not work result has already been rendered right? and 
hence it wouldn't matter if i add actoinmessages now.
           }
         }                                               
   } 
Second approach of of preResultListener is to add action messages just like 
above in preResultListener's method as result is yet to be rendered and i can 
safely change that. However not sure if preResultListener will ever execute if 
exception is thrown in action? and even if it does how i can get the exception 
object thrown by the action.?
There has to be something out of the box so that actions are not cluttered with 
try-catch blocks




--
This message was sent by Atlassian JIRA
(v6.1#6144)

Reply via email to