title: CXF interceptors, SoapFaults, and custom exceptions
body: <p>I'm trying to modify my CXF/Spring based SOAP web service into using interceptors to
  capture SoapFaults and convert them into custom exceptions. The goal here is to
  present a web client with only instances of my custom exception class when things go wrong,
  never a SoapFault.</p>
  
  <p>This is what I've managed to scrape together so far. You'd think there'd be more on
  proper error handling in the various tuts and books , oh well.</p>
  
  <p>FormsEndpointException</p>
  
  <pre><code>@XmlAccessorType(XmlAccessType.FIELD)
  public class FormsEndpointException extends Exception
  {
      @XmlElement(name="errMsg")
      private String myMsg
  ...
  </code></pre>
  
  <p>FormsEndpointImpl</p>
  
  <pre><code>@WebService( endpointInterface="mycorp.forms.web.endpoint.FormsEndpoint")
  @org.apache.cxf.interceptor.InInterceptors (interceptors = {"mycorp.forms.web.resolver.MyExceptionInterceptor"})
  
  public class FormsEndpointImpl implements FormsEndpoint
  {
      @Resource
      WebServiceContext context;
  
      FormTemplateService  formTemplateService;   //injected from bean
  
      // must have default constructor per JAXB
      public FormsEndpointImpl() {}
  
      //only need setters
      public void setFormTemplateService(FormTemplateService formTemplateService) {
          this.formTemplateService = formTemplateService;
      }
  
      //methods    
      @WebMethod public void insertFormTemplate( FormTemplate formTemplate) throws FormsEndpointException
      {
          try {
              formTemplateService.persist(formTemplate);
          } catch ( Exception e) {
              throw new FormsEndpointException( e.getMessage());
          }
      }
      ....
  }
  </code></pre>
  
  <p>MyExceptionInterceptor</p>
  
  <pre><code>public class MyExceptionInterceptor extends AbstractSoapInterceptor
  {
      //special constructor
      public MyExceptionInterceptor()
      {
          super( Phase.USER_LOGICAL);
      }
  
      @Override
      public void handleMessage(SoapMessage arg0) throws Fault
      {
          //left blank for normal stuff    
      }
  
      //only gets called when unwinding a chain when an exception is called on that chain
      // Every exception will be wrapped into a Fault object by CXF
      @Override
      public void handleFault(SoapMessage arg0)
      {
          Fault aNewFault = new Fault( arg0.getContent(Exception.class));
          aNewFault.setFaultCode( new QName("SNAP"));
          aNewFault.setMessage("DAMMIT");
          // TODO Auto-generated method stub
          // Throwable FormsEndpointException = new FormsEndpointException("Awww");
      }
  }
  </code></pre>
  
  <p>application-context.xml</p>
  
  <pre><code>...
  &lt;!-- Spring manage ServiceBean --&gt;
  &lt;bean id="formsEndpoint" class="mycorp.forms.web.endpoint.FormsEndpointImpl"&gt;
    &lt;property name="formTemplateService"  ref="formTemplateService" /&gt;
  &lt;/bean&gt;
  
  &lt;!-- JAX-WS Service Endpoint --&gt;    
  &lt;jaxws:endpoint id="formsServiceService" implementor="#formsEndpoint" address="/formsService"&gt;
    &lt;jaxws:inFaultInterceptors&gt;
      &lt;ref class="mycorp.forms.web.resolver.MyExceptionInterceptor" /&gt;
    &lt;/jaxws:inFaultInterceptors&gt;
    &lt;jaxws:outFaultInterceptors&gt;
      &lt;ref class="mycorp.forms.web.resolver.MyExceptionInterceptor" /&gt;
    &lt;/jaxws:outFaultInterceptors&gt;
  &lt;/jaxws:endpoint&gt;
  ...
  </code></pre>
  
  <p>Question is, what needs to go into handleFault() to throw a new instance of MyException?</p>
  
  <p>TIA,</p>
  
  <p>Still-learning Steve</p>