Vaelyr
Vaelyr

Reputation: 3176

Spring WS: Handle connection refused and client timeout exceptions

I can't seem to find proper solution to this. I would want to handle and display different message to the user depending on what happened. I'm using Spring WS and calling marshalSendAndReceive. On the returning service method at the moment I would simply catch (WebServiceIOException e), problem is that this exception is thrown by both Connection refused (wrong credentials) and when query takes longer than specified by client side (timeout)? Is there any proper way of handling these two without providing one general message that "something went wrong".

Upvotes: 3

Views: 4741

Answers (2)

Sarvar Nishonboyev
Sarvar Nishonboyev

Reputation: 13100

One way is to implement ClientInterception class

Another way is just catching general Exception class on where you send a request to web serice:

try {
  webServiceTemplate.marshalSendAndReceive(request);
} catch(Exception e){
   log.error("your custom message");
}

Upvotes: 0

Artem Bilan
Artem Bilan

Reputation: 121337

The WebServiceTemplate does its hard work in the method doSendAndReceive, where there is try...catch block and comprehensive error-handling. One of them is triggerAfterCompletion(int interceptorIndex, MessageContext messageContext, Exception ex), which delegates to the injected ClientInterceptor.

So, you just need to implement properly afterCompletion(MessageContext messageContext, Exception ex) to throw the appropriate business exception.

Upvotes: 3

Related Questions