Catching Python xmlrpclib Exceptions
When using Zope, you don't want to have bare "except:" clauses lying around [1] and when using xmlrpclib in Zope, you will have to guard against whatever connection trouble there may be. Problem is, xmlrpclib seems to sometimes report errors by raising some nonstandard looking exceptions that show up in a traceback like this:
error: (61, 'Connection refused')
Uhm, yeah. In order to catch "error" with a try-except, I should know where it comes from. Lucky me, the traceback mentions that this one is from:
Module httplib, line 630, in connect
Looking there, I see that the code is raising socket.error.
xmlrpclib also has a few errors of its own. I should decide which ones I want to catch, but in general I can do something like this:
from socket import error as socket_error
#... lots of stuff...
try:
result = server.xmlrpc_update_address_company(id, data)
except (socket_error, xmlrpclib.Fault, \
xmlrpclib.ProtocolError, xmlrpclib.ResponseError), error_code:
handle_error(error_code) # whatever you do...
This will get most tracebacks off the back of my users, but of course now the burden of handling the errors is on me. That's the life of the programmer.
| [1] | because those broad "except:" clauses will also catch ConflictErrors, which Zope should be let to handle by itself. It's also bad style even in general Python. |