Skip to content

Commit

Permalink
Include the HTTPResponse object as an attribute of HTTPError
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Darnell committed Jun 26, 2010
1 parent 60ef8d5 commit 33c3f8d
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions tornado/httpclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,14 @@ def fetch(self, request, **kwargs):
_curl_setup_request(self._curl, request, buffer, headers)
self._curl.perform()
code = self._curl.getinfo(pycurl.HTTP_CODE)
if code < 200 or code >= 300:
raise HTTPError(code)
effective_url = self._curl.getinfo(pycurl.EFFECTIVE_URL)
buffer.seek(0)
return HTTPResponse(
response = HTTPResponse(
request=request, code=code, headers=headers,
buffer=buffer, effective_url=effective_url)
if code < 200 or code >= 300:
raise HTTPError(code, response=response)
return response
except pycurl.error, e:
buffer.close()
raise CurlError(*e)
Expand Down Expand Up @@ -546,7 +547,7 @@ def __init__(self, request, code, headers={}, buffer=None, effective_url=None,
self.effective_url = effective_url
if error is None:
if self.code < 200 or self.code >= 300:
self.error = HTTPError(self.code)
self.error = HTTPError(self.code, response=self)
else:
self.error = None
else:
Expand Down Expand Up @@ -577,9 +578,21 @@ def __del__(self):


class HTTPError(Exception):
def __init__(self, code, message=None):
"""Exception thrown for an unsuccessful HTTP request.
Attributes:
code - HTTP error integer error code, e.g. 404. Error code 599 is
used when no HTTP response was received, e.g. for a timeout.
response - HTTPResponse object, if any.
Note that if follow_redirects is False, redirects become HTTPErrors,
and you can look at error.response.headers['Location'] to see the
destination of the redirect.
"""
def __init__(self, code, message=None, response=None):
self.code = code
message = message or httplib.responses.get(code, "Unknown")
self.response = response
Exception.__init__(self, "HTTP %d: %s" % (self.code, message))


Expand Down

0 comments on commit 33c3f8d

Please sign in to comment.