Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClientResponseError raised by "raise_for_status" does not contain response body #4760

Open
mfrlin opened this issue May 21, 2020 · 4 comments

Comments

@mfrlin
Copy link

mfrlin commented May 21, 2020

馃悾 Is your feature request related to a problem? Please describe.
APIs return description of the problem in the body but the response body is lost if you use raise_for_status.

馃挕 Describe the solution you'd like
raise_for_status should throw exception that includes response body (or whole response for that matter).

@borysvorona
Copy link
Member

borysvorona commented May 26, 2020

Already mentioned here #4600

@andrea-cassioli-maersk
Copy link

Same issue here. I would expect to have the response body back from the request also in case of error (quite important when debugging!)

@HansBrende
Copy link

@mfrlin @andrea-cassioli-maersk after much tinkering, I was able to create a workaround for this issue while we are waiting for aiohttp v4 to come out. Implementation here: #4600 (comment)

@towards-a-new-leftypol
Copy link

I am using wireshark for now to get the response message, but I need to log what the server sends back. Maybe it's as simple as a change like this:

diff --git a/aiohttp/client_exceptions.py b/aiohttp/client_exceptions.py
index f7023ef8..65d6de88 100644
--- a/aiohttp/client_exceptions.py
+++ b/aiohttp/client_exceptions.py
@@ -63,6 +63,7 @@ class ClientResponseError(ClientError):
         status: Optional[int] = None,
         message: str = "",
         headers: Optional[LooseHeaders] = None,
+        response: ClientResponse
     ) -> None:
         self.request_info = request_info
         if status is not None:
@@ -73,6 +74,7 @@ class ClientResponseError(ClientError):
         self.headers = headers
         self.history = history
         self.args = (request_info, history)
+        self.response = response
 
     def __str__(self) -> str:
         return "{}, message={!r}, url={!r}".format(
diff --git a/aiohttp/client_reqrep.py b/aiohttp/client_reqrep.py
index 3d7a90d6..dd3ebe2a 100644
--- a/aiohttp/client_reqrep.py
+++ b/aiohttp/client_reqrep.py
@@ -860,6 +860,7 @@ class ClientResponse(HeadersMixin):
                         status=exc.code,
                         message=exc.message,
                         headers=exc.headers,
+                        response=self
                     ) from exc
 
                 if message.code < 100 or message.code > 199 or message.code == 101:
@@ -964,6 +965,7 @@ class ClientResponse(HeadersMixin):
                 status=self.status,
                 message=self.reason,
                 headers=self.headers,
+                response=self
             )
 
     def _cleanup_writer(self) -> None:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants