Groovy Examples

Go to list of examples

Getting SOAP Error Body

Getting SOAP Error Body

You are using SOAP adapter and when service returns SOAP Fault, you want to catch, use, or log the exception payload.

This method explained in a blog post in SAP Community.

The exception payload will be included in message property CamelExceptionCaught and has the type org.apache.cxf.binding.soap.SoapFault.

To see all available methods in this class see the Javadoc

Groovy IDE notice
You can't test this example with GroovyIDE at the moment.

Script

Try it on Groovy IDE
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import org.w3c.dom.Node;
import groovy.xml.*

def Message processData(Message message) {
    def map = message.getProperties();
    def ex = map.get("CamelExceptionCaught");
    if (ex.getClass().getCanonicalName().equals("org.apache.cxf.binding.soap.SoapFault")) {
        // log, use, or set to body

        // You can also get statusCode or Message
        // String exceptionMessage = ex.getMessage();

        // Fault Detail Element
        def xml = XmlUtil.serialize(ex.getOrCreateDetail());
        message.setBody(xml);
    }
    
    return message;
}