You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The functions in http_requests should be improved so that error details returned in the API response are preserved.
The problem it solves
Currently, the functions in http_requests use response.raise_for_status() which throws a generic exception based on the HTTP response code. An error response from the API server can include additional details about why the request failed. For example, "At least one of supported fields should be set and non-empty" was stored in response.content for a recent API call I made that failed. But the exception was a generic 400 response exception and I had to make changes to the http_requests function to figure out what the real error was.
Alternatives
Catching the generic exceptions and logging the content. But since there is no one single entry point or method that can be overridden, it has to be done in multiple places, which is verbose. The best place to do it would be in http_requests.
The text was updated successfully, but these errors were encountered:
My workaround was to catch the exception earlier in the request/response lifecycle and add additional information:
classTimeoutSession(requests.Session):
# Connect timeout and read timeout. Request docs say to make it slightly# more than multiple of 3. Use '_' in hope that if upstream adds a timeout to# the session in the future, it won't conflict._timeout= (6.25, 30.25)
def__init__(self, timeout=None):
self._timeout=timeoutorself._timeoutsuper().__init__()
defrequest(self, method, url, **kwargs):
log.debug(kwargs)
kwargs.setdefault('timeout', self._timeout)
try:
response=super().request(method, url, **kwargs)
log.debug(response.content)
response.raise_for_status()
returnresponseexceptrequests.HTTPErrorase:
iftext:=e.response.text:
raiserequests.HTTPError(text, e.response) fromeraise
The timeout stuff (#88) isn't related to this issue it just ended up in the same custom Session as this workaround.
Enhancement description
The functions in
http_requests
should be improved so that error details returned in the API response are preserved.The problem it solves
Currently, the functions in
http_requests
useresponse.raise_for_status()
which throws a generic exception based on the HTTP response code. An error response from the API server can include additional details about why the request failed. For example, "At least one of supported fields should be set and non-empty" was stored inresponse.content
for a recent API call I made that failed. But the exception was a generic 400 response exception and I had to make changes to thehttp_requests
function to figure out what the real error was.Alternatives
Catching the generic exceptions and logging the content. But since there is no one single entry point or method that can be overridden, it has to be done in multiple places, which is verbose. The best place to do it would be in
http_requests
.The text was updated successfully, but these errors were encountered: