I am dealing with a HTTP API that, unfortunately, has some stability issues for the time being. It will now and then throw a 500 server error when load gets too high, and it starts tripping over itself.
To make this less of a problem, I've enabled the retry mechanism for 500 server errors:
def _start_http_session() -> requests.Session:
"""Starts a HTTP session"""
adapter = requests.adapters.HTTPAdapter(max_retries=RETRY_STRATEGY)
session = requests.Session()
session.mount("https://", adapter)
session.mount("http://", adapter)
return session
This works great, but the problem is that I can no longer find a reference to the HTTP response in case of an error. I have a top level error handler that looks something like this:
if (
isinstance(error, requests.RequestException)
and error.response is not None
):
req = error.request
resp = error.response
# Log request and response ...
This used to work great when I did response.raise_for_status(), but it no longer works because RetryError doesn't include a reference to the response. Instead, the error now gets logged as a generic exception, with no information about the response from the server:
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=8088): Max retries exceeded with url: /test (Caused by ResponseError('too many 500 error responses'))
I really want to be able to log the server response, so I can provide more information when filing bug reports towards the API.