Skip to content
Myr

Add ability to pass `response` to `HTTPError()`

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Just a little refactoring, but it seems nicer to me to be able to pass the response when constructing the `HTTPError` instance instead of constructing it and then changing the member variable.

authored by Dmitry Medvinsky · committed by Dmitry Medvinsky · parent d372a5b · Browse files · 4 files+18 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+18 added−5 removed

  • other:+18 added−5 removed

4 files+18 added−5 removed

modified:requests/exceptions.py+5 added-1 removed
14 unmodified lines
151617class HTTPError(RequestException):18 """An HTTP error occurred."""19 response = None1920 def __init__(self, *args, **kwargs):21 """ Initializes HTTPError with optional `response` object. """22 self.response = kwargs.pop('response', None)23 super(HTTPError, self).__init__(*args, **kwargs)242526class ConnectionError(RequestException):27 """A Connection error occurred."""2829
26 unmodified lines
modified:requests/models.py+1 added-3 removed
655 unmodified lines
656 elif 500 <= self.status_code < 600:657 http_error_msg = '%s Server Error: %s' % (self.status_code, self.reason)658659 if http_error_msg:660 http_error = HTTPError(http_error_msg)661 http_error.response = self662 raise http_error660 raise HTTPError(http_error_msg, response=self)661662 def close(self):663 return self.raw.release_conn()
modified:AUTHORS.rst+1 added
120 unmodified lines
121- Vinod Chandru122- Johnny Goodnow <j.goodnow29@gmail.com>123- Denis Ryzhkov <denisr@denisr.com>124- Wilfred Hughes <me@wilfred.me.uk> @dontYetKnow125- Dmitry Medvinsky <me@dmedvinsky.name>
modified:test_requests.py+11 added-1 removed
383 unmodified lines
384 self.assertEqual(max_retries_used, [5])385386 # undo monkey patch387 HTTPConnectionPool.urlopen = old_urlopen388 388389 def test_http_error(self):390 error = requests.exceptions.HTTPError()391 self.assertEqual(error.response, None)392 response = requests.Response()393 error = requests.exceptions.HTTPError(response=response)394 self.assertEqual(error.response, response)395 error = requests.exceptions.HTTPError('message', response=response)396 self.assertEqual(str(error), 'message')397 self.assertEqual(error.response, response)398399400if __name__ == '__main__':401 unittest.main()