You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@ws.apache.org by Adam Taft <ad...@hydroblaster.com> on 2005/01/14 02:55:49 UTC

Throwing Exceptions in XmlRpcServer

Hi all,

I'm writing an XmlRpcServer, using the WebServer class as my base 
starting point.

I'm able to add a handler to process xmlrpc requests.  However, inside 
that handler, I'm unable to throw an exception that has a custom message 
passed back to the client.

Just from guessing, it looks like the XmlRpcServer is catching the 
thrown exception, then creates an xmlrpc error response to send back to 
the client using the exception's toString() method.

In my example code (pasted below), I'm getting the following message 
from the console:

org.apache.xmlrpc.XmlRpcException: java.lang.Exception: XmlRpc Failure

This message is what the client sees.  Again, it seems to me that the 
XmlRpcServer is catching the thrown exception (in the execute()) method, 
and then uses the exception's toString() method in order to craft the 
xmlrpc fault response.

I guess the question is, how do I raise an exception (or somehow 
otherwise trigger an error response by the XmlRpcServer) such that the 
error response message doesn't include (from the example) 
"java.lang.Exception".  I'm just looking to get the error message only 
passed back to the client.

Even more ideal, I'd like to actually craft the XmlRpcException to send 
back to the client, such that I can include my own xmlrpc fault code and 
fault message.

What's the appropriate way to get an xmlrpc error response back to the 
client?

Thanks.

Adam


This is my example code which reproduces the problem:


import java.util.*;
import org.apache.xmlrpc.*;

public class TestServer implements XmlRpcHandler {
	
	TestServer() {
		WebServer server = new WebServer(8080);
		server.addHandler("$default", this);
		server.start();
	}
	
	public Object execute(String method, Vector params) throws Exception {
		if (true) {
			throw new Exception ("XmlRpc Failure");
		}
		
		return "XmlRpc Success";
	}
	
	public static void main(String[] args) {
		new TestServer();
		
		try {
			Thread.currentThread().sleep(3000);
		} catch (Exception e) {}

		try {
			XmlRpcClient xmlRpcClient = new XmlRpcClient ("http://localhost:8080/");
			Vector params = new Vector();
			String results = (String) xmlRpcClient.execute("any_method", params);
			System.out.println(results);
		} catch (Exception e) {
			System.err.println(e);
		}

	}
}