Oliver,
I was frustrated with this issue too. I had to work this out for
myself, but finally figured out how to get it right. It's kindof
confusing, and there's not a lot of documention available.
In essence, you need to:
1) Extend XmlRpcException and override the toString() method.
2) Throw this extended XmlRpcException in your server code.
I'm attaching some test code to demonstrate why you need to do the above.
The problem essentially comes from how the XmlRpcServer must wrap any
thrown exceptions in an XmlRpcException. To do this, it calls the
toString() method of the thrown Exception and stores this as the message
field in the newly created XmlRpcException. This is mistake number one
(it should call the exception's getMessage() method instead). Then, the
XmlRpcServer must convert the XmlRpcException to a string for sending on
the wire. Again, it calls the toString() method of XmlRpcException
instead of getMessage(). This is mistake number two.
So anyway, if you do the two steps above on your server, you should get
the results you're looking for. Hope this helps.
Adam
p.s. I'm using xmlrpc-1.2-b1.jar
Oliver Cole wrote:
Yes, but I am talking about the fault string that is actually sent over
the wire, in the XMLRPC/HTTP response. I throw an exception in my server
side code, and it ends up with that name in the text of the fault that
gets returned to the client.
Now, I could be receiving that fault in a Perl RPC client, or in Java
code or whatever, the fact remains: the name of the exception class is
in the *text* of the fault.
On the client side in Java, this of course generates an XmlRpcException,
but my issue is what gets sent to the client, from my server code, in
the first place.
Can anyone help?
Oli
import java.util.Vector;
import org.apache.xmlrpc.WebServer;
import org.apache.xmlrpc.XmlRpcClient;
import org.apache.xmlrpc.XmlRpcException;
public class Test {
public static void main(String[] args) {
WebServer server = new WebServer(8080);
server.addHandler("test", new Test());
server.start();
// Test #1
try {
XmlRpcClient client = new
XmlRpcClient("http://localhost:8080");
client.execute("test.throwException", new Vector());
} catch (Exception e) {
System.err.println(e.getMessage());
}
// Test #2
try {
XmlRpcClient client = new
XmlRpcClient("http://localhost:8080");
client.execute("test.throwXmlRpcException", new
Vector());
} catch (Exception e) {
System.err.println(e.getMessage());
}
// Test #3
try {
XmlRpcClient client = new
XmlRpcClient("http://localhost:8080");
client.execute("test.throwMyOwnXmlRpcException", new
Vector());
} catch (Exception e) {
System.err.println(e.getMessage());
}
server.shutdown();
}
public boolean throwException() throws Exception {
if (true) {
throw new Exception("throwException() called. This is
a normal Exception.");
}
return true;
}
public boolean throwXmlRpcException() throws Exception {
if (true) {
throw new XmlRpcException(1, "throwXmlRpcException()
called. This is an unmodified XmlRpcException.");
}
return true;
}
public boolean throwMyOwnXmlRpcException() throws Exception {
if (true) {
throw new MyOwnXmlRpcException (2,
"throwMyOwnXmlRpcException() called. This is an extended XmlRpcException with
the overridden toString() method.");
}
return true;
}
}
class MyOwnXmlRpcException extends XmlRpcException {
protected MyOwnXmlRpcException(int code, String message) {
super(code, message);
}
public String toString() {
return super.getMessage();
}
}