Throwing Exceptions from Web Services

Web Services do not provide for Exceptions; probably because not all languages include them, and web services should be accessible by any language.

Instead there is a special message component called a 'SOAP Fault' that is returned (instead of? along with?) the normal return message. This is an XML element containing a few defined subelements.

We want to be able to transport information about the original error back to the client, especially the stack trace, so we can understand the problem more completely.

Axis Services

Axis provides a convenient AxisFault class for representing the SOAP Fault. This is a RemoteException which works in quite well with the existing client stuff. It even provides a factory method for making a SOAP Fault from an exception; unfortunately this doesn't seem to contain the stack trace of the given exception, and it appears to wrap an extra AxisFault into the details bit.

However you can manually build it like this:

protected AxisFault makeFault(boolean blameClient, String message, Throwable cause) {

   AxisFault fault = new AxisFault(message);

   //set fault code according to standard (?) - should the client check what was sent
   //or is the problem on the server.
   if (blameClient) {
     fault.setFaultCode("Client");
   } else {
     fault.setFaultCode("Server");
   }

   fault.clearFaultDetails();  
   if (cause != null) {
      StringWriter writer = new StringWriter();
      cause.printStackTrace(new PrintWriter(writer));
      fault.addFaultDetailString(writer.toString());
   }

   return fault;
} 

This might be extended to extract the cause from common exceptions such as InvocationTargetException, not rewrapping if the cause is an AxisFault, etc, but I like as much information as I can get.

Other Soap Services

See also a .Net-related article and a JAX-RPC article (low level)

-- MartinHill - 08 Mar 2004 with contributions from Noel.

Topic revision: r1 - 2004-03-08 - 20:38:00 - MartinHill
 
AstroGrid Service Click here for the
AstroGrid Service Web
This is the AstroGrid
Development Wiki

This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback