Re: [Httplib2-discuss] httplib2 should provide custom exceptions for httplib failures
Status: Beta
Brought to you by:
jcgregorio
From: Simon W. <si...@si...> - 2006-12-08 11:11:32
|
On 7 Dec 2006, at 12:01, Sam Ruby wrote: > What's bad (IMO) is that httplib2, like its predecessor does both. > Do I get a 408 or a socket.timeout? Actually, you can get both. > > Since status codes are not going away, I would suggest mapping more > exceptions to status codes, and documenting the few (if any) > remaining "expected exceptions". It's interesting to note the difference here between Python's urllib and urllib2 modules: >>> import urllib, urllib2 >>> urllib.urlopen('http://simonwillison.net/nothing-here').read() '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">\n<html><head> \n<title>404 Not Found</title>\n</head><body>\n<h1>Not Found</h1> \n<p>The requested URL /nothing-here was not found on this server.</p> \n</body></html>\n' >>> urllib2.urlopen('http://simonwillison.net/nothing-here').read() Traceback (most recent call last): ... File "/Library/Frameworks/Python.framework/Versions/2.5/lib/ python2.5/urllib2.py", line 499, in http_error_default raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) urllib2.HTTPError: HTTP Error 404: Not Found >>> Whether or not this is desired behaviour depends very much on what you are trying to do. One solution might be for httplib2 to have two request methods - one that raises exceptions and one that returns the document with the non-200 status code. It would be nice if Python had a standard idiom or naming convention for pairs of methods like this, but it's not something I've seen anywhere. There might be a good reason for that of course. Cheers, Simon |