How to get SOAP fault's faultcode when handling custom fault exception
Asked Answered
R

1

10

Our system consumes SOAP Web Service, using JAX-WS client stubs generated based on service's WSDL. In case of error server returns SOAP faults like this:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header />
  <s:Body>
    <s:Fault>
      <faultcode>SomeErrorCode</faultcode>
      <faultstring xml:lang="en-US">Some error message</faultstring>
      <detail>
        <ApiFault xmlns="http://somenamespace.com/v1.0" xmlns:a="http://somenamespace.com/v1.0" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
          <a:RequestId>123456789</a:RequestId>
          <a:CanRetry>true</a:CanRetry>
        </ApiFault>
      </detail>
    </s:Fault>
  </s:Body>

Based on WSDL SomeCustomFault exception class is generated and all service methods are declared to throw this (see below) exception.

@WebFault(name = "ApiFault", targetNamespace = "http://services.altasoft.ge/orders/v1.0")
public class SomeCustomFault
    extends Exception
{
    private ApiFault faultInfo;

    public SomeCustomFault(String message, ApiFault faultInfo) {
        super(message);
        this.faultInfo = faultInfo;
    }

    public SomeCustomFault(String message, ApiFault faultInfo, Throwable cause) {
        super(message, cause);
        this.faultInfo = faultInfo;
    }

    public ApiFault getFaultInfo() {
        return faultInfo;
    }
}

As you can see this custom fault exception extends Exception and not SOAPFaultException. Hovewer I need to get SOAP fault's faultcode which could be retrieved only from SOAPFaultException using getFaultCode method. Could you tell me how can I reach SOAPFaultException or SOAP fault's faultcode in place where I catch above mentioned custom fault exception?

Rosalie answered 20/11, 2013 at 13:26 Comment(2)
don't you have access to the returned SOAPMessage as such?Latoya
No. I have only thrown exception which is caught in my codeRosalie
N
11

You could implement a JAX-WS handler and add it to your client web service reference. This will be given opportunity to handle the request message and response message OR notified of a fault.

Create a SOAPHandler<SOAPMessageContext> and your handleFault() method will be passed the SOAPMessageContext. From that you can getMessage().getSOAPBody().getFault() to get the SOAPFault, which contains getFaultCode() and getDetail().

Assign your new fault handler to your web service ref. One way is via @HandlerChain. It will be invoked prior to your catch clause.

Nipple answered 23/3, 2014 at 2:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.