-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert ClientResponseError in HTTPException #3449
Comments
cc @marrataj ( he was the guy from my project originally spotting that little inconvenience ) |
GitMate.io thinks the contributor most likely able to help you is @asvetlov. Possibly related issues are #662 (ClientResponseError wrong error propagation?), #2781 (Replace ClientResponseError.code with .status), #1754 (ClientResponseError after upgrading to latest version), #850 (How to handle ClientResponseError/ServerDisconnectedError properly), and #2980 (Drop deprecated code parameter from ClientResponseError). |
FYI: I don't think we should expect any improvement in the existing code as I am not really convinced this falls into automatic behavior. Perhaps it does not and those are the things that are best to be handled case-by-base. Anyway, any recommendation is welcomed. |
You can collect a mapping of status_code -> web_exception_class easy. Another question is: should you pass all exceptions as-is or wrap them somehow. Maybe you want to distinguish gateway timeout returned by client from timeouts to proxy server itself. Taking all the above into account I doubt if automation transition is possible in a generic matter as a part of aiohttp. |
We do something like this with a custom response class import aiohttp
from aiohttp import http_exceptions
class CustomResponse(aiohttp.ClientResponse):
def raise_for_status(self) -> None:
if 400 <= self.status:
raise http_exceptions.HttpProcessingError(
code=self.status,
message=self.reason,
headers=self.headers,
)
session = aiohttp.ClientSession(..., raise_for_status=True, response_class=CustomResponse) |
@stj so the trick is to always Ah my nad, there is a method with the same name. Slipped my mind. I like this idea, seems simple and should play nicely in global context as well as in local use case |
Caution: custom response class is a sort of gray hidden feature of aiohttp. |
Ah...that does not sound right, I mean to implement and face the potential consequences later. So @asvetlov your recommendation is to basically do this case by case. Whatever |
Yes, I think it is a practical solution. |
Long story short
We have the case, in our project (rest API), where we make some requests to the upstream services using the session. So long story short, we have sth like
async with session.get(...)
. That may generatesClientResponseError
. And that is fine, because at this point we act as a client. But we need a way to respond withHTTPException
to the original caller of the API.I just can't find a neat way other than getting the
status
fromClientResponseError
and choosing one of the subclasses ofHTTPException
(that do set the appropriate code) other than if-ing by the status one by one maybe discarding ones that we do not care about asHTTPInternalServerError
.Expected behaviour
I am able to easily transform
ClientResponseError
intoHTTPException
that will share the HTTP code and carried body.Actual behaviour
I cannot do the above.
None as this is most likely not a bug. It might be a feature or I have failed to locate existing solution.
Your environment
The text was updated successfully, but these errors were encountered: