What errors/exceptions do I need to handle with urllib2.Request / urlopen?
Add generic exception handler:
request = urllib2.Request('http://www.example.com', postBackData, { 'User-Agent' : 'My User Agent' })
try:
response = urllib2.urlopen(request)
except urllib2.HTTPError, e:
checksLogger.error('HTTPError = ' + str(e.code))
except urllib2.URLError, e:
checksLogger.error('URLError = ' + str(e.reason))
except httplib.HTTPException, e:
checksLogger.error('HTTPException')
except Exception:
import traceback
checksLogger.error('generic exception: ' + traceback.format_exc())
From the docs page urlopen
entry, it looks like you just need to catch URLError. If you really want to hedge your bets against problems within the urllib code, you can also catch Exception
as a fall-back. Do not just except:
, since that will catch SystemExit
and KeyboardInterrupt
also.
Edit: What I mean to say is, you're catching the errors it's supposed to throw. If it's throwing something else, it's probably due to urllib code not catching something that it should have caught and wrapped in a URLError
. Even the stdlib tends to miss simple things like AttributeError
. Catching Exception
as a fall-back (and logging what it caught) will help you figure out what's happening, without trapping SystemExit
and KeyboardInterrupt
.