How to Catch Errors in SimpleXMLRPCServer

G

gregpinero

I have a pretty simple XMLRPCServer, something along the lines of the
example:

server = SimpleXMLRPCServer(("localhost", 8000))
server.register_function(pow)
server.register_function(lambda x,y: x+y, 'add')
server.serve_forever()

Now what I want to do is catch any errors that happen on requests, and
ideally have them emailed to me. I have the email part all taken care
of, I just need to know how to get at the exceptions.

Thanks in advance for any help,

Greg
 
B

Bruno Desthuilliers

(e-mail address removed) a écrit :
I have a pretty simple XMLRPCServer, something along the lines of the
example:

server = SimpleXMLRPCServer(("localhost", 8000))
server.register_function(pow)
server.register_function(lambda x,y: x+y, 'add')
server.serve_forever()

Now what I want to do is catch any errors that happen on requests, and
ideally have them emailed to me. I have the email part all taken care
of, I just need to know how to get at the exceptions.

Q&D :

try:
server.serve_forever()
except Exception, e:
mail_me_the_exception(e)
raise
 
J

Jeff McNeil

Instead of register_function, use register_instance and provide a
_dispatch method in that instance that handles your exception logging.

Pseudo:

class MyCalls(object):
def _dispatch(self, method, args):
try:
self.getattr(self, method)(*args)
except:
handle_logging()

server = SimpleXMLRPCServer(("localhost", 8000))
server.register_instance(MyCalls())
server.serve_forever()

There might be an easier way... but this works for me.

-Jeff
 
J

Jeff McNeil

getattr, not self.getattr.

Instead of register_function, use register_instance and provide a
_dispatch method in that instance that handles your exception logging.

Pseudo:

class MyCalls(object):
def _dispatch(self, method, args):
try:
self.getattr(self, method)(*args)
except:
handle_logging()

server = SimpleXMLRPCServer(("localhost", 8000))
server.register_instance(MyCalls())
server.serve_forever()

There might be an easier way... but this works for me.

-Jeff
 
G

gregpinero

Instead of register_function, use register_instance and provide a
_dispatch method in that instance that handles your exception logging.

Pseudo:

class MyCalls(object):
def _dispatch(self, method, args):
try:
self.getattr(self, method)(*args)
except:
handle_logging()

server = SimpleXMLRPCServer(("localhost", 8000))
server.register_instance(MyCalls())
server.serve_forever()

There might be an easier way... but this works for me.

I wonder if there is something wrong with that. I get this error on
calling ever method:

Fault 1: 'exceptions.TypeError:cannot marshal None unless allow_none
is enabled' but I can't see anywhere None would be coming from.

-Greg
 
J

Jeff McNeil

Yeah, that code was out of memory and I didn't test it, my apologies.
Need to actually return a value from _dispatch.

class MyCalls(object):
def _dispatch(self, method, args):
try:
return getattr(self, method)(*args)
except:
handle_logging()

server = SimpleXMLRPCServer(("localhost", 8000))
server.register_instance(MyCalls())
server.serve_forever()

-Jeff
 
G

gregpinero

Yeah, that code was out of memory and I didn't test it, my apologies.
Need to actually return a value from _dispatch.

class MyCalls(object):
def _dispatch(self, method, args):
try:
return getattr(self, method)(*args)
except:
handle_logging()

server = SimpleXMLRPCServer(("localhost", 8000))
server.register_instance(MyCalls())
server.serve_forever()

Thanks, that works. I'm not sure why I didn't notice it wasn't
returning anything.

-Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top