You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by dkundo <dk...@yahoo.com> on 2012/04/30 13:17:59 UTC

generic exception handling

In my project the web services tier wraps the business logic tier. The BL
tier generates many types of exceptions. I don't want to enclose all the WS
-> BL tiers interaction with try..catch blocks, so I'm thinking about
creating an interceptor which will catch all exceptions and transform them
into a proper WebFault class. 
While it's possible to provide an interceptor for the outfault chain, it
seems like at this point the original exception data has been lost already
as I'm getting the general SOAPFault. 
Any ideas on how to achieve what I want?


--
View this message in context: http://cxf.547215.n5.nabble.com/generic-exception-handling-tp5675691.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: generic exception handling

Posted by dkundo <dk...@yahoo.com>.
Thanks Dan, 
you were right - it was "hiding" under the getCause. 
Not sure, though, it puts me closer to the target. I'm trying it accomplish
the following:
Instead of coding like:
@WebMethod void foo() throws MyCustomGenericWebFault
{
   try {
       ... do something
   }
   catch (Exception e) {
       translate e to MyCustomGenericWebFault and throw 
   }
}

i'd like to provide an interceptor which does the same. But how? Should I
tamper with the SoapMessage? Or throw another Fault?





--
View this message in context: http://cxf.547215.n5.nabble.com/generic-exception-handling-tp5675691p5677519.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: generic exception handling

Posted by Daniel Kulp <dk...@apache.org>.
On Monday, April 30, 2012 04:17:59 AM dkundo wrote:
> In my project the web services tier wraps the business logic tier. The BL
> tier generates many types of exceptions. I don't want to enclose all the
> WS -> BL tiers interaction with try..catch blocks, so I'm thinking about
> creating an interceptor which will catch all exceptions and transform
> them into a proper WebFault class.
> While it's possible to provide an interceptor for the outfault chain, it
> seems like at this point the original exception data has been lost already
> as I'm getting the general SOAPFault.
> Any ideas on how to achieve what I want?

Likely it's a SoapFault, (not SOAPFault).   In anycase, did you check the 
"getCause" of the fault?   Most likely the original exception is set as the 
cause. 

Dan



> 
> 
> --
> View this message in context:
> http://cxf.547215.n5.nabble.com/generic-exception-handling-tp5675691.html
> Sent from the cxf-user mailing list archive at Nabble.com.
-- 
Daniel Kulp
dkulp@apache.org - http://dankulp.com/blog
Talend Community Coder - http://coders.talend.com


Re: generic exception handling

Posted by Mark Streit <mc...@gmail.com>.
Not sure this is exactly what you're looking for but we adopted the
practice of having all @WebMethods throw a ServiceException following the
patterns outlined in this helpful article:

http://io.typepad.com/eben_hewitt_on_java/2009/07/using-soap-faults-and-exceptions-in-java-jaxws-web-services.html

Whatever business logic exception gets thrown, say from DAO classes, etc is
wrapped in that *ServiceException *that contains another bean class
called *ServiceFaultBean
*- it carries back the information on the real cause of the exception.

package com.acme.book.util;

import javax.xml.bind.annotation.XmlType;
import javax.xml.ws.WebFault;

/**
 * Class: ServiceException
 *
 * Custom Exception that is thrown from JAX-WS @WebMethod annotated methods
in a service
 * implementation bean (SIB).
 */
@XmlType(name = "ServiceException", namespace = "http://util.book.acme.com/
")
@WebFault(name = "ServiceException", targetNamespace = "
http://util.book.acme.com/")
public class ServiceException extends BaseException {

   private  ServiceFaultBean faultBean;
....
....
....


package com.acme.book.util;

import javax.xml.bind.annotation.XmlType;
import javax.xml.ws.WebFault;

/**
 * Class: BaseException
 *
 * Custom Exception that others will extend
 *
 */
@XmlType(name = "BaseException", namespace = "http://util.book.acme.com/")
@WebFault(name = "BaseException", targetNamespace = "
http://util.book.acme.com/")
public class BaseException extends Exception {


   /**
    * Java type that transports as soapenv:Fault detail element.
    */
   private ServiceFaultBean faultInfo;

   // Used with ExceptionConstants enum
   private String errorCode;
   private String errorMessage;

   // Source exception - root cause and associated details message
   private String source;
   private String details;
....
....



package com.acme.book.util;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;

/**
 * Class: ServiceFaultBean
 *
 * The Java type that is passed as a soapenv:Fault detail element. Used in
JAX-WS web services
 * exceptions, fault beans hold the details of the SOAP fault. This one is
used by the { @link
 * WebServiceException ).
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ServiceFaultBean", namespace = "http://util.book.acme.com/
")
public class ServiceFaultBean {
   private String faultMessage;
   private String faultCause;

   private String errorCodeKey;
   private String errorMessageKey;
....
....


Usage is something like this (logging statements removed to shorten):  (and
we were not using CXF at the time this was originally done and don't use
Interceptors so I can't speak to that).

   *@WebMethod*
   public Book getBook(@WebParam(name = "bookID") String bookID) *throws
ServiceException* {


      Book book;

      try {
         book = BookOrderServiceDAO.getBook(bookID);
      }
      catch (DataValidationException e) {

  *       throw new ServiceException(e.getMessage(), e.getErrorCode(), e);*
      }
      catch (NotFoundException e) {

*         throw new ServiceException(e.getMessage(), e.getErrorCode(), e);*
      }

HTH

On Mon, Apr 30, 2012 at 7:17 AM, dkundo <dk...@yahoo.com> wrote:

> In my project the web services tier wraps the business logic tier. The BL
> tier generates many types of exceptions. I don't want to enclose all the WS
> -> BL tiers interaction with try..catch blocks, so I'm thinking about
> creating an interceptor which will catch all exceptions and transform them
> into a proper WebFault class.
> While it's possible to provide an interceptor for the outfault chain, it
> seems like at this point the original exception data has been lost already
> as I'm getting the general SOAPFault.
> Any ideas on how to achieve what I want?
>
>
> --
> View this message in context:
> http://cxf.547215.n5.nabble.com/generic-exception-handling-tp5675691.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>



*Mark
*