You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Vaishampayan, Shailesh Arvind" <sv...@inautix.co.in> on 2013/10/29 11:15:55 UTC

How to handle business exceptions from service layer in struts 2 at only one place





I am working with an application involving 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 shown to users as validation message(s). 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 preresult listener. But I cannot use interceptor like below which catches BusinessExceptions 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 action messages now.

           }

         }

   }

Second approach of using pre-result listener is to add action messages just like above in pre-result listener's method, as result is yet to be rendered and I can safely change that. However I am not sure if pre-result listener 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 ?

Please let me know any other approach by which i don't have to clutter my classes with try-catch block




******************************************************
This message and any files or attachments sent with this message contain confidential information and is intended only for the individual named.  If you are not the named addressee, you should not disseminate, distribute, copy or use any part of this email.  If you have received this message in error, please delete it and all copies from your system and notify the sender immediately by return Email.

Email transmission cannot be guaranteed to be secure or error-free as information can be intercepted, corrupted, lost, destroyed, late, incomplete or may contain viruses.  The sender, therefore, does not accept liability for any errors or omissions in the contents of this message, which arise as a result of email transmission.
******************************************************

Re: How to handle business exceptions from service layer in struts 2 at only one place

Posted by Antonios Gkogkakis <gk...@tcd.ie>.
Have a look at the ExceptionMappingInterceptor, with a bit of tweaking I
believe you can achieve what you want.

http://struts.apache.org/release/2.0.x/struts2-core/apidocs/com/opensymphony/xwork2/interceptor/ExceptionMappingInterceptor.html

Antonios


On 29 October 2013 10:15, Vaishampayan, Shailesh Arvind <
svaishampayan@inautix.co.in> wrote:

>
>
>
>
>
> I am working with an application involving 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 shown to users as validation
> message(s). 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 preresult listener. But I cannot use interceptor like
> below which catches BusinessExceptions 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 action messages now.
>
>            }
>
>          }
>
>    }
>
> Second approach of using pre-result listener is to add action messages
> just like above in pre-result listener's method, as result is yet to be
> rendered and I can safely change that. However I am not sure if pre-result
> listener 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 ?
>
> Please let me know any other approach by which i don't have to clutter my
> classes with try-catch block
>
>
>
>
> ******************************************************
> This message and any files or attachments sent with this message contain
> confidential information and is intended only for the individual named.  If
> you are not the named addressee, you should not disseminate, distribute,
> copy or use any part of this email.  If you have received this message in
> error, please delete it and all copies from your system and notify the
> sender immediately by return Email.
>
> Email transmission cannot be guaranteed to be secure or error-free as
> information can be intercepted, corrupted, lost, destroyed, late,
> incomplete or may contain viruses.  The sender, therefore, does not accept
> liability for any errors or omissions in the contents of this message,
> which arise as a result of email transmission.
> ******************************************************

Re: How to handle business exceptions from service layer in struts 2 at only one place

Posted by Lukasz Lenart <lu...@apache.org>.
2013/10/29 Vaishampayan, Shailesh Arvind <sv...@inautix.co.in>:
>       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 preresult listener. But I cannot use interceptor like below which catches BusinessExceptions 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 action messages now.
>
>            }
>
>          }
>
>    }
>
> Second approach of using pre-result listener is to add action messages just like above in pre-result listener's method, as result is yet to be rendered and I can safely change that. However I am not sure if pre-result listener 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 ?

Result wasn't rendered yet but you must return some result from the
interceptor, e.g.:

try{
    return invocation.invoke();
} catch (Exception) {
    // handle BusinessException
    return "success";
}

It just an example, you must adjust it to your needs.


Regards
-- 
Ɓukasz
+ 48 606 323 122 http://www.lenart.org.pl/

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org