This is an older problem that I had back in 2011 but got no further than promoting the Salesforce Idea: SOAPFault Information for Apex. It came up again today with users of a managed package wanting better error messages when a web service callout fails.
I'm calling a web service that will return a Fault and FaultMessage(s) if there is an issue with the request. E.g.
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body> <s:Fault> <faultcode>s:Client</faultcode> <faultstring xml:lang="en-US">The creator of this fault did not specify a Reason.</faultstring> <detail> <DataAccessFault xmlns="http://www.example.com/api" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <FaultMessges xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"> <a:string>Validation Error(s) occurred during Save.</a:string> <a:string>Meaningful error message appears here</a:string> </FaultMessges> <FaultObjectType>Order</FaultObjectType> <FaultOperationType>Insert</FaultOperationType> </DataAccessFault> </detail> </s:Fault> </s:Body> </s:Envelope>
When this gets handled by Apex it gets turned into a CalloutException:
Message: Web service callout failed: WebService returned a SOAP Fault: The creator of this fault did not specify a Reason. faultcode=s:Client faultactor=
The issue I have is that the getMessage() method doesn't return the entire SOAP reponse. It has been truncated before the useful information appears.
Is there some way to recover the entire response from the CalloutException in code so I can give a meaning error back to the user?
Currently I need to resort to capturing the Outbound SOAP message and response using the third party tool like soapUI. While this works for debugging, it doesn't help the users during normal usage. Another option during development is to pull the Callout request and response from the debug log. Again, not very useful for giving users meaningful responses in the managed package.