You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Pascal Ognibene <pa...@libertysurf.fr> on 2008/11/01 11:42:24 UTC

Custom handling of exceptions and SOAP faults with CXF

Hi,

I've written a web service implementation (wsdl first) using CXF. This 
service can throw exceptions. Currently they're automatically caught by 
CXF and mapped to a SOAP fault, but I want to include additional 
informations in the SOAP fault message.

I tried with CXF Soap11FaultOutInterceptor, but I only have a Message as 
input; the original exception is not available. And I need it to extract 
informations from this exception and build a custom Fault message.

Basically what I have now is something like:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
       <soap:Fault>
          <faultcode>soap:Server</faultcode>
          <faultstring>Fault occurred while processing.</faultstring>
       </soap:Fault>
    </soap:Body>
</soap:Envelope>

And what I would like to get is:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
       <soap:Fault>
          <faultcode>soap:Server</faultcode>
          <faultstring>1234|param1|param2|param3</faultstring>
       </soap:Fault>
    </soap:Body>
</soap:Envelope>

Where the fault string is custom built using data extracted from the 
initial exception.

BTW I'm using CXF with Spring; don't know if it has an impact.

Any help is welcome.

Pascal

Re: Custom handling of exceptions and SOAP faults with CXF

Posted by Pascal Ognibene <pa...@libertysurf.fr>.
Daniel Kulp a écrit :
> On Saturday 01 November 2008 6:42:24 am Pascal Ognibene wrote:
>> Hi,
>>
>> I've written a web service implementation (wsdl first) using CXF. This
>> service can throw exceptions. Currently they're automatically caught by
>> CXF and mapped to a SOAP fault, but I want to include additional
>> informations in the SOAP fault message.
>>
>> I tried with CXF Soap11FaultOutInterceptor, but I only have a Message as
>> input; the original exception is not available. 
> 
> It probably is.   If you do message.getContent(Exception.class), it's probably 
> a Fault which has the cause set to your original exception.   It MAY be 
> a "SoapFault" which would have a setter for the faultstring.   If not, you 
> can call SoapFault.createSoapFault(fault) to convert the fault into a 
> soapfault and then set things as needed.   You can do this in the faultOut 
> chain as early as possible.
> 
> However, there is a JAX-WS "standard" way of doing this, although it's a bit 
> convoluted.   When you throw your exception, if you set the cause of your 
> exception to a javax.xml.ws.soap.SOAPFaultException, you can set all kinds of 
> things in the SOAPFault thing in there.   The faultstring is one of them.
> 
> Dan
> 

Thank you Dan. I'm going to explore a bit more if the message content 
can take me back to the exception, in which case the CXF api is rich 
enough to build a custom fault message.

For the second suggestion, I don't think it's applicable because my 
exception can't be a SOAPFaultException : it is thrown by a specific 
POJO layer that knows nothing about soap (but is called by the WS 
layer). I don't want to mix transport specific classes with business 
classes that may be reused with a different protocol. For the same 
reason I don't want to introduce jax-ws annotations in this layer.

Pascal


Re: Custom handling of exceptions and SOAP faults with CXF

Posted by Daniel Kulp <dk...@apache.org>.
On Saturday 01 November 2008 6:42:24 am Pascal Ognibene wrote:
> Hi,
>
> I've written a web service implementation (wsdl first) using CXF. This
> service can throw exceptions. Currently they're automatically caught by
> CXF and mapped to a SOAP fault, but I want to include additional
> informations in the SOAP fault message.
>
> I tried with CXF Soap11FaultOutInterceptor, but I only have a Message as
> input; the original exception is not available. 

It probably is.   If you do message.getContent(Exception.class), it's probably 
a Fault which has the cause set to your original exception.   It MAY be 
a "SoapFault" which would have a setter for the faultstring.   If not, you 
can call SoapFault.createSoapFault(fault) to convert the fault into a 
soapfault and then set things as needed.   You can do this in the faultOut 
chain as early as possible.

However, there is a JAX-WS "standard" way of doing this, although it's a bit 
convoluted.   When you throw your exception, if you set the cause of your 
exception to a javax.xml.ws.soap.SOAPFaultException, you can set all kinds of 
things in the SOAPFault thing in there.   The faultstring is one of them.

Dan


> And I need it to extract 
> informations from this exception and build a custom Fault message.
>
> Basically what I have now is something like:
>
> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
>     <soap:Body>
>        <soap:Fault>
>           <faultcode>soap:Server</faultcode>
>           <faultstring>Fault occurred while processing.</faultstring>
>        </soap:Fault>
>     </soap:Body>
> </soap:Envelope>
>
> And what I would like to get is:
>
> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
>     <soap:Body>
>        <soap:Fault>
>           <faultcode>soap:Server</faultcode>
>           <faultstring>1234|param1|param2|param3</faultstring>
>        </soap:Fault>
>     </soap:Body>
> </soap:Envelope>
>
> Where the fault string is custom built using data extracted from the
> initial exception.
>
> BTW I'm using CXF with Spring; don't know if it has an impact.
>
> Any help is welcome.
>
> Pascal



-- 
Daniel Kulp
dkulp@apache.org
http://dankulp.com/blog