0

I am doing multiple RESTful API calls using requests inside a loop. I would like to handle exceptions safely, without the script stopping. Here's my code:

try:
    r = requests.get(url)
except requests.exceptions as e:
    print("ERROR: Requests exception: %s" % e)
if 'json' in r.headers['Content-Type']:
    return r.json()

This works for a while, but then fails with this traceback on the second line. The except clause isn't catching the exception.

  File "C:\Python27\myfiles\callAPI.py", line 82, in getAPI
    r = requests.get(url)
  File "C:\Python27\lib\site-packages\requests\api.py", line 67, in get
    return request('get', url, params=params, **kwargs)
  File "C:\Python27\lib\site-packages\requests\api.py", line 53, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Python27\lib\site-packages\requests_cache\core.py", line 128, in request
    **kwargs
  File "C:\Python27\lib\site-packages\requests\sessions.py", line 468, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Python27\lib\site-packages\requests_cache\core.py", line 101, in send
    return send_request_and_cache_response()
  File "C:\Python27\lib\site-packages\requests_cache\core.py", line 93, in send_request_and_cache_response
    response = super(CachedSession, self).send(request, **kwargs)
  File "C:\Python27\lib\site-packages\requests\sessions.py", line 608, in send
    r.content
  File "C:\Python27\lib\site-packages\requests\models.py", line 737, in content
    self._content = bytes().join(self.iter_content(CONTENT_CHUNK_SIZE)) or bytes()
  File "C:\Python27\lib\site-packages\requests\models.py", line 663, in generate
    raise ChunkedEncodingError(e)
requests.exceptions.ChunkedEncodingError: ('Connection broken: IncompleteRead(0 bytes read)', IncompleteRead(0 bytes read))

How can I safely handle this?

Xavier Ashe
  • 123
  • 1
  • 8
  • 3
    `requests.exceptions` is a module, not a tuple of exceptions... `except requests.exceptions` therefore won't actually catch any the exceptions defined within that module. – jonrsharpe Dec 31 '15 at 16:15
  • I looked [at the docs](http://docs.python-requests.org/en/latest/api/#requests.exceptions.RequestException), but I didn't see requests.exceptions.ChunkedEncodingError. How would I handle ALL exceptions? – Xavier Ashe Dec 31 '15 at 16:24
  • 2
    I think to handle all base-class exceptions you need to use `exception: requests.exceptions.RequestException`. See also [this thread](https://stackoverflow.com/questions/16511337/correct-way-to-try-except-using-python-requests-module) – vrs Dec 31 '15 at 16:33
  • See [here](https://github.com/kennethreitz/requests/blob/d290a9b4c6e7c40b30e14091de0352ab955c6299/requests/exceptions.py#L86) - they're all subclasses of `RequestException`, so just catch that. – jonrsharpe Dec 31 '15 at 16:33
  • @vrs - Thank you. If you'll post that as an an answer, I'll mark it as correct. Not only do I need to improve my python skill, I also have to improve my stackoverflow searches! – Xavier Ashe Dec 31 '15 at 17:58

0 Answers0