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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix HTTPException message #59

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Conversation

imptype
Copy link
Contributor

@imptype imptype commented Jul 24, 2024

  1. Fix HTTPException erroring when getting content types other than application/json.
    image

  2. Be more explicit with the response data. Sometimes it doesn't have the "code" key in it, but it is a valid json.
    image

@jnsougata jnsougata requested review from jnsougata and removed request for jnsougata July 25, 2024 04:50
@jnsougata jnsougata added the bug Something isn't working label Jul 25, 2024
Copy link
Owner

@jnsougata jnsougata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we are passing the resp object ref inside the Exception class so it will be good to hide the content_type check within the class for clarity, rather than putting it in the global _handler.

@imptype
Copy link
Contributor Author

imptype commented Jul 25, 2024

As we are passing the resp object ref inside the Exception class so it will be good to hide the content_type check within the class for clarity, rather than putting it in the global _handler.

We can't move the content_type check inside the class because you can't use await in __init__. There are other ways to async init but it's messier than what we have now.

@jnsougata
Copy link
Owner

jnsougata commented Jul 26, 2024

As we are passing the resp object ref inside the Exception class so it will be good to hide the content_type check within the class for clarity, rather than putting it in the global _handler.

We can't move the content_type check inside the class because you can't use await in __init__. There are other ways to async init but it's messier than what we have now.

In that case, we can just add those awaited values to the error obj before raising exception or we can add response attr to the Error class.

Error.response.status_code if we need to check status code.
await error.response.read() if we need the body for exception handling and we can add some async method like to_string()

@imptype
Copy link
Contributor Author

imptype commented Feb 5, 2025

If we REALLY want to avoid checking the response's content-type outside and put it inside the HTTPException class instead for the sake of making the code in https.py look less and cleaner, the only decent way to do it is using an async class method

So instead of

        if resp.status >= 400:
            raise HTTPException(resp, await resp.json())

it's now

        if resp.status >= 400:
            raise await HTTPException.create(resp)

and the check and async reads are in the async create method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants