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

Print better "reason" for raise_for_status error message. #6319

Closed
gk-patel opened this issue Dec 29, 2022 · 3 comments
Closed

Print better "reason" for raise_for_status error message. #6319

gk-patel opened this issue Dec 29, 2022 · 3 comments

Comments

@gk-patel
Copy link

gk-patel commented Dec 29, 2022

Sometimes proper reason is missing in the Error thrown by the raise_for_status method.

Exception has occurred: HTTPError 405 Client Error: for url: https://bla.com

This is becasue the reason is an empty string on #Line1010

But it would be helpful to maybe try and deduct a reason, if not provided by the server (i.e. just a status code is provided)

My very ugly implementation of this would look like (just before Line1010)

from http import HTTPStatus    # somewhere on top 
.... 

if not reason: 
    for msg, code in HTTPStatus.__dict__.items(): 
        if code == self.status_code: 
            reason = msg 
            break

I am sure you guys will find a one liner.

I would expect a more clear error message as follows

Exception has occurred: HTTPError 405 Client Error: METHOD_NOT_ALLOWED for url: https://bla.com

Reproduction Steps

Simply try sending a POST message to a server that does not accept POST.
The server should return a proper status_code back but the reason should/could be empty.
This might not be an edge case and can be see in some popular servers (eg. I saw this in Jira REST API).

System Information

$ python -m requests.help
{
  "chardet": {
    "version": null      
  },
  "charset_normalizer": {
    "version": "2.1.1"   
  },
  "cryptography": {      
    "version": ""        
  },
  "idna": {
    "version": "3.4"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.10.9"
  },
  "platform": {
    "release": "10",
    "system": "Windows"
  },
  "pyOpenSSL": {
    "openssl_version": "",
    "version": null
  },
  "requests": {
    "version": "2.28.1"
  },
  "system_ssl": {
    "version": "1010111f"
  },
  "urllib3": {
    "version": "1.26.13"
  },
  "using_charset_normalizer": true,
  "using_pyopenssl": false
}
@ravi-kale
Copy link

Hey @gk-patel, I can see the reason in response for the method not allowed for jira rest api as well. Check the screenshot attached. Else can you give more brief on steps to reproduce
image

@gk-patel
Copy link
Author

gk-patel commented Jan 4, 2023

Hi @ravi-kale,

Maybe its a problem with our enterprise Jira, unfortunately it does not return r.reason.
The sample error message I printed above was a real example from our enterprise Jira.
I am just suggesting a fall back improvement. I dont think it should interfere with currently available behavior.

Best Regards,
G. K. Patel

@nateprewitt
Copy link
Member

Hi @gk-patel, we generally won't try to infer what a server is intending or mutate what we've received. Reasons are optional metadata from the server and allowed to be omitted. What each status codes means is widely published. If a user needs a textual reminder, it should be quick to look up from the codes list as you've shown.

I think we'll close this as I don't see it being an accepted feature. Thanks for the suggestion.

@nateprewitt nateprewitt closed this as not planned Won't fix, can't repro, duplicate, stale Jan 19, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants