Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Python
Musings: Using decorators to reduce duplicate exception handling
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="J Kenneth King, post: 3797077"] I recently started a project called TracShell ([URL]http://code.google.com/p/tracshell[/URL]) where I make heavy use of the xmlrpclib core module. When the number of RPC calls was small, wrapping each call in try/except was acceptable. However, this obviously will duplicate code all over the place. There are only ever two exceptions that the module itself will throw: xmlrpclib.ProtocolError and xmlrpclib.Fault -- both very useful, but not show stoppers. To combat the duplication, my clever idea was to use a function decorator to wrap any function that used xmlrpclib calls: def catch_errors(fn): """ A decorator to catch typical xmlrpclib exceptions """ def wrapped(*args, **kwargs): try: return fn(*args, **kwargs) except xmlrpclib.ProtocolError, e: print "There was a problem communicating with the server." print "URL: %s" % e.url print "Headers: %s" % e.headers print "Error code: %d" % e.errcode print "Error message: %s" % e.errmsg print "Please file a report with the TracShell developers." pass except xmlrpclib.Fault, e: print "A fault ocurred" print "Fault code: %d" % e.faultCode print "Fault string: %s" % e.faultString print "If you think this message is the result of an error," print "please file a report with the TracShell developers." pass return wrapped Maybe I could rename the decorator to something meaningful, but besides that it works pretty well. Now any function I write in my class that uses RPC calls can be wrapped by this decorator and have the exception handling done in a uniform way for these particular exceptions. I was just curious if other Pythonistas out there used the same idea or have a better one. Thoughts? [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Python
Musings: Using decorators to reduce duplicate exception handling
Top