Mrusful
Mrusful

Reputation: 1503

jaxws exception/error handling

I try to build wsdl client for jre5 with JAX-WS RI 2.1.3 its my first expirience. I genereted classes with wsdl2java tool from cxf and wrote wrapper class around client looks like:

public class RequestHelper {
    private DataLoadService service = new DataLoadService();
    private DataLoadServiceSoap client;
    private static String token;

    //....my constructor....

    public void sendData(data){
     try{
       if (tokenIsExpired()){
          renewToken();
       }
       client.sendData(data, this.token);
      }catch(SOAPFaultException e){
           //...work with e
      }
    }
}

I can't understand how i can process exception in sendData method.
I mean, for example, in HTTP we have status codes, we can read status code and decide which type of error we get from server and how we want process them.
In my case i have problem with token expired time. Sometimes sendData request goes to the server for a long time. And the token is no longer valid when the request is already on the server then i get exception with text message "Token expired". And i want catch this type of exception separately, somthing like this:

public class RequestHelper {
private DataLoadService service = new DataLoadService();
private DataLoadServiceSoap client;
private static String token;

//....my constructor....

    public void sendData(data){
     try{
         if (tokenIsExpired()){
            renewToken();
         }
         client.sendData(data, this.token);
       }catch(SOAPFaultException e){
       //...work with e
       }catch(TokenExpiredException e){
         renewToken();
         client.sendData(data, this.token);
       }
    }
}

How i can achieve this with JAX-WS RI 2.1.3 library?

UPD:

} catch (SOAPFaultException e) {
 SOAPFault f = e.getFault();
 f.getFaultString() //yes here we have error description with "Token"
                    //but with locals dependency, this is not safe handle exception by this value
 f.getFaultCode() //here simply string "soap:Receiver", do not know how i can recognize only "token exceptions"
}

Upvotes: 1

Views: 2289

Answers (1)

Ramadas
Ramadas

Reputation: 510

Find out what is been returned as part of the SOAPFaultException from the server. If the Exception contains the error message then we can write something like below. note: Error code will be the best way to handle this.

    try{
         if (tokenIsExpired()){
            renewToken();
         }
         client.sendData(data, this.token);
       }catch(SOAPFaultException e){
           if(e.getFault().getFaultString().equalsIgnoreCase("Token expired") ) {
               renewToken();
               client.sendData(data, this.token);
           }
           ......
       }            

Another way is to have custom SOAP exception thrown from the sever with error code and error message and handle that in the code

Upvotes: 1

Related Questions