Search code examples
javaweb-servicestomcatjax-ws

JAX-WS retrieving the thrown custom exception on client side


I have a web service that I consume from a client(JavaFX desktop application). For this purpose I have also created a custom exception class which will return defined message and error code. The method I'm trying to consume is the following:

public void insertUserDataService(String mailAddress, String userName, String name, String lastName, String password) throws ThrowExceptionToClient {
    try {
        String hashPassword = BCrypt.hashpw(password, BCrypt.gensalt());
        User user = new User(mailAddress, userName, name, lastName, hashPassword);
        controller.insertUser(user, cdba);
    } catch (ClassNotFoundException | SQLException e  ) {
        if(e instanceof SQLException) { 
            SQLException sqlE = (SQLException) e;
            throw new ThrowExceptionToClient(sqlE);
        }
        else {
            throw new ThrowExceptionToClient(e);
        }
    }
}

Observe the instance that is thrown if the exception is of the type SQLException. The custom class looks like the following:

public class ThrowExceptionToClient extends Exception{
/**
 * 
 */
private static final long serialVersionUID = 1L;
private MessageWrapper mWrapper;
private String errorCode;
private String errorMessage;
public ThrowExceptionToClient(Exception e) {
    if(e instanceof SQLException ) {
        if(((SQLException) e).getErrorCode() == 19) {
            this.errorCode = "1";
            this.errorMessage = "_rae_";
        }
    }else {
        this.errorCode = "99";
        this.errorMessage = "_grc_";
    }

}   
public String getErrorCode() {
    return this.errorCode;
}
public void setErrorCode(String errorCode) {
    this.errorCode = errorCode;
}
public String getErrorMessage() {
    return this.errorMessage;
}
public void setErrorMessage(String errorMessage) {
    this.errorMessage = errorMessage;
}

}

I do get the right error code(ns1:errorCode) and the error message(ns1:errorMessage) when I try to consume the method in via the Web Services Explorer:

<detail>
    <ns1:fault xmlns:ns1="http://endpointLayer">
      <ns1:errorCode>1</ns1:errorCode>
      <ns1:errorMessage>_rae_</ns1:errorMessage>
    </ns1:fault>
    <ns2:hostname xmlns:ns2="http://xml.apache.org/axis/">Mnemonics-MacBook-Air.local</ns2:hostname>
  </detail>

But when I try to catch the error on the client side the value of the errorCode and errorMessage are null.

try {
            endpointService.insertUserDataService(mailTxtField.getText(), userNameTxtField.getText(),
                                                    nameTxtField.getText(), lastNameTxtField.getText(),
                                                    passwordField.getText());
            mailTxtField.clear();
            userNameTxtField.clear();
            nameTxtField.clear();
            lastNameTxtField.clear();
            passwordField.clear();
            reEnterPasswordField.clear();
        } catch (ThrowExceptionToClient_Exception e) {
            Alert onThrowExceptionToClientAlert = new Alert(Alert.AlertType.ERROR);
            onThrowExceptionToClientAlert.setTitle("Error Dialog");
            onThrowExceptionToClientAlert.setHeaderText("Server error detected");
            onThrowExceptionToClientAlert.setContentText(e.getFaultInfo().getErrorCode());
            onThrowExceptionToClientAlert.showAndWait();
            e.printStackTrace();
        }
e.getFaultInfo().getErrorCode()

When debugging I can see that the values in the constructor ThrowExceptionToClient are populated. These values doesn't follow all the way to the client. Two exceptions classes are generated when creating the client code from the wsdl file: ThrowExceptionToClient and ThrowExceptionToClient_Exception. Am I doing something wrong in creating the custom exception class or is the catch clause in the client invalid?

EDIT: The class MessageWrapper:

public class MessageWrapper {
private String errorMessage;//This variable was used in the client
private String errorCode;

public MessageWrapper(){}

public String getErrorMessage() { return errorMessage; }

public void setErrorMessage(String message) {
    this.errorMessage = message;
}

public String getErrorCode() { return errorCode; }

public void setErrorCode(String errorCode) {
    this.errorCode = errorCode;
}

}

Solution

  • Well I rewrote the web service to use Jax-ws as a framework instead of apache axis. The problem became more clear. What I haven't shown in the code above is that the ThrowExceptionToClient had the following annotation @WebFault(faultBean = "exceptionLayer.MessageWrapper") which used the member variables in the class MessageWrapper instead of the getters and the setters. Thus returning a methodNotFoundException getErrorMessage(). Removing the annotation @WebFault(faultBean = "exceptionLayer.MessageWrapper") solved the problem.