5

Its my first time posting here so please be patient and correct me whenever necessary...

I am building simple web service-based application using NetBeans with GlassFish. NetBeans does help a lot in terms of generating code for new web services and their operations, one thing drives me mad though - web service exception handling. Operation like:

@WebMethod(operationName = "checkUserExist")
public String checkUserExist(@WebParam(name = "login") String login, @WebParam(name = "password") String password) throws LoginException {

    String auth_code = "";
    bk_end.Validate val = new bk_end.Validate();

    boolean isValidated = val.check(login, password);

    if(isValidated)
    {
        auth_code = val.return_AuthCode();
        bank_services.CustomerSerice.setAuth_Code(auth_code);
        return auth_code;
    }

    throw new LoginException("Something is wrong!");

}

and the exception class:

public class LoginException extends Exception
{
    String message;


    public LoginException(String message)
    {
        super();
        this.message = message;
    }

    public String getErrorMessage()
    {
        return this.message;
    }
}

throws a massive Exceptions details : java.lang.reflect.InvocationTargetException plus tons of other exceptions.. I realise it is a very much so nooby question, but after many hours of trying various things I just do not know what tot do. I've read about the @WebFault thing but got no idea how to specify this correctly (attach my exception to a particular method..)

Please help, all ideas are more than welcome!

Exceptions that I'm getting: Service invocation threw an exception with message : null; Refer to the server log for more details

Exceptions details : java.lang.reflect.InvocationTargetException
javax.servlet.ServletException: java.lang.reflect.InvocationTargetException
    at org.glassfish.webservices.monitoring.WebServiceTesterServlet.doPost(WebServiceTesterServlet.java:330)
    at org.glassfish.webservices.monitoring.WebServiceTesterServlet.invoke(WebServiceTesterServlet.java:106)
    at org.glassfish.webservices.EjbWebServiceServlet.service(EjbWebServiceServlet.java:114)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
    at com.sun.grizzly.http.servlet.ServletAdapter$FilterChainImpl.doFilter(ServletAdapter.java:1002)
    at com.sun.grizzly.http.servlet.ServletAdapter$FilterChainImpl.invokeFilterChain(ServletAdapter.java:942) 
    at com.sun.grizzly.http.servlet.ServletAdapter.doService(ServletAdapter.java:404)
    ...
Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.glassfish.webservices.monitoring.WebServiceTesterServlet.doPost(WebServiceTesterServlet.java:301)
    ... 24 more
Caused by: bank_services.LoginException_Exception: excepts.LoginException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at com.sun.xml.ws.fault.SOAPFaultBuilder.createException(SOAPFaultBuilder.java:145)
    at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:123)
    at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:93)
    at com.sun.xml.ws.client.sei.SEIStub.invoke(SEIStub.java:144)
    at $Proxy383.checkUserExist(Unknown Source) ... 29 more             
Mike
  • 53
  • 1
  • 5
  • You said that when you throw LoginException, you get "tens of other exceptions but the one I want." Could you please show us what exceptions are getting thrown instead? – Jack Edmonds Dec 06 '11 at 14:22
  • true that, updated my question with required details ! – Mike Dec 06 '11 at 14:36
  • anybody... ? I'm on it whole day already, there must be solution, its a beginners problem... – Mike Dec 06 '11 at 16:37
  • What exception do you "want"? I don't understand what he problem is. The cause of the `InvocationTargetException` should be the exception that caused the problem. – Gray Dec 06 '11 at 16:39
  • ok, my mistake above - not that I 'want' any exceptions, but what I would like to achieve is to be able (on the client side) to get the "Something is wrong" exception message when caught... – Mike Dec 06 '11 at 16:44

1 Answers1

4

Oh ok. You want to see the "Something is wrong" message.

So I think the ultimate problem here is that you are not using the standard detailMessage field in the Throwable. If you just pass the message to the base class (super(message);) then I bet you would see the exception in the trace. Did you try another Exception type such as just Exception?

You could also define the LoginException.toString() to be something like:

@Override
public String toString() {
    String s = getClass().getName();
    return (message != null) ? (s + ": " + message) : s;
}

Alternatively, you will need to do something like this:

try {
    ...
} catch (Exception e) {
    if (e instanceof ServletException) {
        e = e.getCause();
    }
    if (e instanceof InvocationTargetException) {
        e = e.getCause();
    }
    if (e instanceof LoginException) {
        System.err.println("Login exception returned message: "
            + ((LoginException)e). getErrorMessage());
    } else {
        System.err.println("Exception returned message: " + e);
    }
}

But I think my recommendation is to use super(message); in your constructor.

Gray
  • 115,027
  • 24
  • 293
  • 354
  • As I already mentioned, my web service knowledge is very basic... What I'm trying to achieve: my client app sends two strings, web service operation checks them, if correct fine, if incorrect -> LoginException is thrown. If thrown on the server side (and worked properly) message ("Something is wrong") would be displayed to the user trying to log in... that is all... – Mike Dec 06 '11 at 16:55
  • And the client code has a copy of the class `LoginException` that is up-to-date? You should add a `serialVersionUID` field to your exception and update it with a number. Your client will throw an exception then if it doesn't have the right version of `LoginException`. – Gray Dec 06 '11 at 16:58
  • "Does the client have this class ?" - not sure if understand correctly - I'm using NetBeans to generate client-server related code... – Mike Dec 06 '11 at 17:01
  • Have you tried to just set the standard message? How about changing your constructor to just call `super(message);`? Can you print out the `exception.getCause().getCause().getErrorMessage()`? – Gray Dec 06 '11 at 17:03
  • My machine just died on me, on the top of everything else... will continue tomorrow - Gray -> thank you for your input mate ! – Mike Dec 06 '11 at 18:15
  • Sure thing dude. Best of luck. I suspect that the message is in there just that it won't be in the stack trace unless you use the `super(message)` constructor. – Gray Dec 06 '11 at 18:17
  • +1 could be appreciated for the work. You can accept if it helped later. – Gray Dec 06 '11 at 18:17
  • ok, finally got it right - Gray, your suggestions put me on the right track somehow :D rather than throwing LoginException, I've tried the Exception class (still not sure why custom exception did not work, but for now...), caught it on the client side and boom ! works ! cheers Gray ! – Mike Dec 06 '11 at 19:18
  • Good. I've just edited my answer. I think the problem with your `LoginException` was your custom `message` field. Did you try just doing `super(message);`? – Gray Dec 06 '11 at 19:24