[Twisted-Python] PB Error (printing exceptions in deferreds)
Mustafa Sakalsiz
mustafa at liqia.com
Fri Mar 4 09:40:36 MST 2005
Hi all,
I am trying to develop an application with twisted and using Pb. I have
a trouble that I couldn't fix. I tried googling to find an solution, but
didn't find an exact answer.
The problem is "I don't want exceptions be printed in server side". The
solution already exists in Pb, if I raise an exception derived from
pb.Error in a (view, execute, ...) method. However, if I raise the same
exception in a deferred or callback function, the exception is printed.
Here is an example,
server.py : Works as I expect
server2.py : Prints the exceptions, which I don't want
client.py : Catches the exception in both ways
Is there a solution which works in any way to avoid printing for
specific exceptions.
Best regards,
Musti
============== server.py (works well)============
#! /usr/bin/python
from twisted.spread import pb
from twisted.internet import reactor
class MyError(pb.Error):
pass
class One(pb.Root):
def remote_broken(self):
msg = "fall down go boom"
raise MyError(msg)
def main():
reactor.listenTCP(8800, pb.PBServerFactory(One()))
reactor.run()
if __name__ == '__main__':
main()
=======================================
=========== server2.py (prints exceptions)============
#! /usr/bin/python
from twisted.spread import pb
from twisted.internet import reactor
from twisted.internet import defer
class MyError(pb.Error):
pass
class One(pb.Root):
def badFoo(self, result):
msg = "fall down go boom"
raise MyError(msg)
def remote_broken(self):
d = defer.Deferred()
d.addCallback(self.badFoo)
d.callback(0)
return d
def main():
reactor.listenTCP(8800, pb.PBServerFactory(One()))
reactor.run()
if __name__ == '__main__':
main()
==========================================
=== client.py (works same for both server and server2)=======
#! /usr/bin/python
from twisted.spread import pb
from twisted.internet import reactor
def main():
factory = pb.PBClientFactory()
reactor.connectTCP("localhost", 8800, factory)
d = factory.getRootObject()
d.addCallbacks(got_obj)
reactor.run()
def got_obj(obj):
# change "broken" into "broken2" to demonstrate an unhandled exception
d2 = obj.callRemote("broken")
d2.addCallback(working)
d2.addErrback(broken)
def working():
print "erm, it wasn't *supposed* to work.."
def broken(reason):
print "got remote Exception"
print " .type =", reason.type
reactor.stop()
main()
=============================================
More information about the Twisted-Python
mailing list