views:

1326

answers:

2

Hello, I'm using the mechanize module to execute some web queries from Python. I want my program to be error-resilient and handle all kinds of errors (wrong URLs, 403/404 responsese) gracefully. However, I can't find in mechanize's documentation the errors / exceptions it throws for various errors.

I just call it with:

    self.browser = mechanize.Browser()
    self.browser.addheaders = [('User-agent', browser_header)]

    self.browser.open(query_url)
    self.result_page = self.browser.response().read()

How can I know what errors / exceptions can be thrown here and handle them ?

A: 

I found this in their docs:

One final thing to note is that there are some catch-all bare except: statements in the module, which are there to handle unexpected bad input without crashing your program. If this happens, it's a bug in mechanize, so please mail me the warning text.

So I guess they don't raise any exceptions. You can also search the source code for Exception subclasses and see how they are used.

Alexander Kojevnikov
I'm not sure you're right, because mechanize seems to propagate exceptions from underlying urllib2 calls
Eli Bendersky
In that case, urllib2 exceptions are very well documented: http://docs.python.org/lib/module-urllib2.html
Alexander Kojevnikov
+3  A: 
$ perl -0777 -ne'print qq($1) if /__all__ = \[(.*?)\]/s' __init__.py | grep Error 

'BrowserStateError',
'ContentTooShortError',
'FormNotFoundError',
'GopherError',
'HTTPDefaultErrorHandler',
'HTTPError',
'HTTPErrorProcessor',
'LinkNotFoundError',
'LoadError',
'ParseError',
'RobotExclusionError',
'URLError',

Or:

>>> import mechanize
>>> filter(lambda s: "Error" in s, dir(mechanize))
['BrowserStateError', 'ContentTooShortError', 'FormNotFoundError', 'GopherError'
, 'HTTPDefaultErrorHandler', 'HTTPError', 'HTTPErrorProcessor', 'LinkNotFoundErr
or', 'LoadError', 'ParseError', 'RobotExclusionError', 'URLError']
J.F. Sebastian