3

I have a JAX-WS web service with a web method that may throw an exception.

@WebMethod
public Folder getTree() throws UnauthorizedException {
    //...
    // Get authorization data..
    //...
    if (!authorized) {
        throw new UnauthorizedException();
    }
    //...
}

It works all right if user is authorized, but when the exception is thrown it doesn't generate SOAP message with a fault, it just crashes web service with

SEVERE: Unauthorized
    ru.cos.xdoc.storage.UnauthorizedException: Unauthorized
    at ru.cos.xdoc.storage.Storage.getTree(Storage.java:136)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    ...

and closes connection

HTTP/1.1 500 Internal Server Error
X-Powered-By: Servlet/2.5
Server: Sun GlassFish Enterprise Server v2.1.1
Content-Type: text/xml;charset="utf-8"
Transfer-Encoding: chunked
Date: Mon, 20 Sep 2010 15:43:59 GMT
Connection: close  

I feel like I miss something simple

EDIT The problem has proven to arise only in the case an exception is thrown not long after the beginning of web method. When a delay is introduced before throwing an exception, like

try {
    Thread.sleep(3000);
} catch (InterruptedException e) {
    e.printStackTrace();
}

throw new UnauthorizedException();

everything works fine.

Does anybody have a clue what may cause such a strange behaviour?

greycat
  • 41
  • 1
  • 1
  • 3

2 Answers2

1

Have you mapped your exception to a Fault? See JAX-WS - Map Exceptions to faults

Community
  • 1
  • 1
gpeche
  • 21,974
  • 5
  • 38
  • 51
  • Yes, I've tried mapping it to a Fault with different kinds of constructors and additional methods. It had no effect in my case. I think the problem is that container doesn't even try to create a Fault out of the exception since I've got no error messages like "Class ... not found" – greycat Sep 20 '10 at 19:53
  • Is `UnauthorizedException` a subclass of `RuntimeException`? If that is the case, maybe the WS servlet is thinking that your WS has a bug. – gpeche Sep 20 '10 at 20:11
  • No, it isn't. It's inherited directly from Exception. – greycat Sep 20 '10 at 20:34
-1

maybe it would be simpler just to build a message telling "Permission denied", or something like that. In my opinion, a web service should never throw an exception, it should always return a response.

emas
  • 668
  • 8
  • 17
  • The problem is that the method is supposed to return folder tree, that's what a web service client should be expecting. Of course there's an option to pass an error message in an OUT parameter, but I suppose exception suits better in this case. – greycat Sep 20 '10 at 16:03