How can I handle exceptions from libraries imported by other libraries in Python?
For example, I have a library called "pycontrol" that I import into my main program. "pycontrol" imports the "suds" library. The suds library, in turn, imports the urllib2 library. I noticed that when the suds library has problems connecting to remote resources accessed via urllib2, these exceptions leak into my main program.
My best guess is to import urllib2 and suds into my global namespace and catch typical exceptions that throw them and are not handled in "pycontrol".
Is there any other best practice on how to approach this?
The basic idea of ββwhat the code snippet looks like (without importing suds or urllib2 into the global namespace):
import pycontrol.pycontrol as pc print "Connecting to iControl API on LTM %s..." % ltm try: b = pc.BIGIP(hostname=ltm, username=user, password=pw, wsdls=wsdl_list, fromurl=True, debug=soap_debug) except (<whattocatch>), detail: print "Error: could not connect to iControl API on LTM %s... aborting!" % ltm print "Details: %s" % detail exitcode = 1 else: print "Connection successfully established."
Here is a traceback example:
Connecting to iControl API on LTM s0-bigip1-lb2.lab.zynga.com... Traceback (most recent call last): File "./register.py", line 507, in <module> main() File "./register.py", line 415, in main b = build_bigip_object(ltm, user, pw, WSDLS, soap_debug = False) File "./register.py", line 85, in build_bigip_object debug=soap_debug) File "build/bdist.macosx-10.6-universal/egg/pycontrol/pycontrol.py", line 81, in __init__ File "build/bdist.macosx-10.6-universal/egg/pycontrol/pycontrol.py", line 103, in _get_clients File "build/bdist.macosx-10.6-universal/egg/pycontrol/pycontrol.py", line 149, in _get_suds_client File "/Library/Python/2.6/site-packages/suds/client.py", line 111, in __init__ self.wsdl = reader.open(url) File "/Library/Python/2.6/site-packages/suds/reader.py", line 136, in open d = self.fn(url, self.options) File "/Library/Python/2.6/site-packages/suds/wsdl.py", line 136, in __init__ d = reader.open(url) File "/Library/Python/2.6/site-packages/suds/reader.py", line 73, in open d = self.download(url) File "/Library/Python/2.6/site-packages/suds/reader.py", line 88, in download fp = self.options.transport.open(Request(url)) File "/Library/Python/2.6/site-packages/suds/transport/https.py", line 60, in open return HttpTransport.open(self, request) File "/Library/Python/2.6/site-packages/suds/transport/http.py", line 62, in open return self.u2open(u2request) File "/Library/Python/2.6/site-packages/suds/transport/http.py", line 118, in u2open return url.open(u2request, timeout=tm) File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 383, in open response = self._open(req, data) File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 401, in _open '_open', req) File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 361, in _call_chain result = func(*args) File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 1138, in https_open return self.do_open(httplib.HTTPSConnection, req) File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 1105, in do_open raise URLError(err) urllib2.URLError: <urlopen error [Errno 8] nodename nor servname provided, or not known>