I'm interested in writing a python client for a web-service, and for testing purposes it would be very interesting also to have a simple stub server. I'm using python 2.3, and ZSI 2.0.
My problem is that I do not manage to return an exception from the server.
If I raise an exception of the type used for the soap fault in the wsdl, I get the TypeError 'exceptions must be classes, instances, or strings (deprecated), not EmptyStringException_Def'. I thought this meant that the fault object was not a subclass of Exception, but modifying the generated code in this way did not help - and of course, not having to modify the generated code would be much better :)
If I return the fault object as part of the response, it is just ignored.
I couldn't find any documentation about faults handling in ZSI. Any hints?
Here's a sample code for a server of a very simple service with just one method, spellBackwards, which should return a soap fault if the input string is empty:
#!/usr/bin/env python
from ZSI.ServiceContainer import AsServer
from SpellBackwardsService_services_server import *
from SpellBackwardsService_services_types import *
class SpellBackwardsServiceImpl(SpellBackwardsService):
def soap_spellBackwards(self, ps):
response = SpellBackwardsService.soap_spellBackwards(self, ps)
input = self.request._in
if len(input) != 0:
response._out = input[::-1]
else:
e = ns0.EmptyStringException_Def("fault")
e._reason = "Empty input string"
# The following just produces an empty return message:
# response._fault = e
# The following causes TypeError
# raise e
return response
AsServer(port=8666, services=[SpellBackwardsServiceImpl(),])
I've found the answer in this ZSI Cookbook, by Chris Hoobs, linked at the bottom of the ZSI home page:
5.4 Exceptions
A thorny question is how to generate the faults at the server. With the ZSI v2.0 code as it is provided, this is not possible.
I assume this to be correct since the paper is linked from the project home page.
This paper also suggests a workaround, which consists in patching the Fault.py file in the ZSI distribution.
I tested the workaround and it works as promised; patching the library is as acceptable solution for me since I need to generate a server for test purposes only (i.e. I'll not need to distribute the patched library).