From: W. Trevor King Date: Fri, 24 Aug 2012 12:07:46 +0000 (-0400) Subject: util:http: convert urllib2.URLError into HTTPError in get_post_url. X-Git-Tag: 1.1.0~130 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=f0fe11190de73c153d7fb66734434ade4a21dbc2;p=be.git util:http: convert urllib2.URLError into HTTPError in get_post_url. Also rework liburl2.HTTPError handling to get both the reason and the error code into the HTTPError message. --- diff --git a/libbe/util/http.py b/libbe/util/http.py index 2f15b15..b9b1765 100644 --- a/libbe/util/http.py +++ b/libbe/util/http.py @@ -83,12 +83,18 @@ def get_post_url(url, get=True, data_dict=None, headers=[], agent=None): try: response = urllib2.urlopen(req) except urllib2.HTTPError, e: + lines = [ + 'We failed to connect to the server (HTTPError).', + 'URL: {}'.format(url), + ] if hasattr(e, 'reason'): - msg = ('We failed to connect to the server.\nURL: {}\n' - 'Reason: {}').format(url, e.reason) - elif hasattr(e, 'code'): - msg = ("The server couldn't fulfill the request.\nURL: {}\n" - 'Error code: {}').format(url, e.code) + lines.append('Reason: {}'.format(e.reason)) + lines.append('Error code: {}'.format(e.code)) + msg = '\n'.join(lines) + raise HTTPError(error=e, url=url, msg=msg) + except urllib2.URLError, e: + msg = ('We failed to connect to the server (URLError).\nURL: {}\n' + 'Reason: {}').format(url, e.reason) raise HTTPError(error=e, url=url, msg=msg) page = response.read() final_url = response.geturl()