Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you catch a thrown soap exception from a web service?

I throw a few soap exceptions in my web service successfully. I would like to catch the exceptions and access the string and ClientFaultCode that are called with the exception. Here is an example of one of my exceptions in the web service:

throw new SoapException("You lose the game.", SoapException.ClientFaultCode);

In my client, I try to run the method from the web service that may throw an exception, and I catch it. The problem is that my catch blocks don't do anything. See this example:

try
{
     service.StartGame();
}
catch
{
     // missing code goes here
}

How can I access the string and ClientFaultCode that are called with the thrown exception?

like image 777
Lou Avatar asked Nov 26 '09 19:11

Lou


People also ask

How are errors handled in SOAP?

SOAP faults are generated by receivers to report business logic errors or unexpected conditions. In JAX-WS, Java exceptions ( java. lang. Exception ) that are thrown by your Java Web service are mapped to a SOAP fault and returned to the client to communicate the reason for failure.

How does spring boot handle SOAP fault exception?

One way is writing your custom interceptor which implements Spring WS's ClientInterceptor interface. You should override handleFault method to handle SOAP faults with your custom logic. Then you need to register your custom Interceptor class as an interceptor at your SOAP client config class.

What is SOAP exception error?

A SOAP fault is an error in a SOAP (Simple Object Access Protocol) communication resulting from incorrect message format, header-processing problems, or incompatibility between applications.


2 Answers

Catch the SoapException instance. That way you can access its information:

try {
     service.StartGame();
} catch (SoapException e)  {
    // The variable 'e' can access the exception's information.
}
like image 93
Ben S Avatar answered Oct 18 '22 12:10

Ben S


You may want to catch the specific exceptions.

try
{
     service.StartGame();
}
catch(SoapHeaderException)
{
// soap fault in the header e.g. auth failed
}
catch(SoapException x)
{
// general soap fault  and details in x.Message
}
catch(WebException)
{
// e.g. internet is down
}
catch(Exception)
{
// handles everything else
}
like image 32
Ray Lu Avatar answered Oct 18 '22 12:10

Ray Lu