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

Add more details to exceptions #467

Open
felipehertzer opened this issue Dec 19, 2024 · 5 comments
Open

Add more details to exceptions #467

felipehertzer opened this issue Dec 19, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@felipehertzer
Copy link

felipehertzer commented Dec 19, 2024

Hi,

I'm trying to find a clean way to handle errors in my project, but I’ve noticed that exceptions, specifically RequestException, do not return an integer that can be used to handle errors programmatically.

It would be incredibly useful if all the fields in RequestException were filled, including an integer field (e.g., an error code) that provides more clarity for error handling.

from curl_cffi import requests
try:
    response = requests.get(
        url="https://httpbin.org/status/404",
        impersonate="chrome",
    )
   response.raise_for_status()
except requests.errors.RequestsError as e:
    print(e, e.code, e.response)

Right now:

'HTTP Error 404: ', 0, None

I expect (with status_code):

'HTTP Error 404: ', 404, '<Response [{404]>'

OR (with curl error code) e.response.status_code:

'HTTP Error 404: ', CURL_CODE, '<Response [{404]>'

Describe the solution you'd like
To be able to easly identify the status error code

Describe alternatives you've considered
I tried to get it from the error message, but it is a bit of hack.

Thank you.

@felipehertzer felipehertzer added the enhancement New feature or request label Dec 19, 2024
@lexiforest
Copy link
Owner

Reasonable request. However, I'm afraid that status code might be unavailable when a "real" exception happens, i.e. when something went wrong during requesting.

@felipehertzer
Copy link
Author

felipehertzer commented Dec 19, 2024

And if include the fields with errors that consistently have a status, such as HTTPError?

raise HTTPError(f"HTTP Error {self.status_code}: {self.reason}")

And then I can check if the exception is a HTTPError type

@perklet
Copy link
Collaborator

perklet commented Dec 19, 2024

Oh, yeah. I think we can guarantee that HTTPError does have a status code itself or in response.

@eight04
Copy link

eight04 commented Dec 23, 2024

Also noticed that the error thrown by raise_for_status has no response property.

@lexiforest
Copy link
Owner

I see, I guess it's more consistent to add the response field to the exception.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants