Httpx: Docs incorrectly reference `HTTPError.message` attribute

Created on 3 Sep 2020  路  2Comments  路  Source: encode/httpx

Checklist

  • [x] The bug is reproducible against the latest release and/or master.
  • [x] There are no similar issues or pull requests to fix it yet.

Describe the bug

The documentation indicates that message field can be used in the HTTPError:
https://github.com/encode/httpx/blob/master/httpx/_exceptions.py#L54

    try:
        response = httpx.get("https://www.example.com")
        response.raise_for_status()
    except httpx.HTTPError as exc:
        print(f"HTTP Exception for {exc.request.url} - {exc.message}")

But there is not such field:

AttributeError: 'HTTPStatusError' object has no attribute 'message'

To reproduce

Execute the example from the doc

Expected behavior

Print the string without raising any exceptions

Actual behavior

AttributeError is raised

Possible fixes

  1. Update the documentation to use str(exc) instead of exc.message
    or
  2. Set self.message field in HTTPError
docs

Most helpful comment

Since BaseException.message was deprecated in 2.6 and removed in 3, I think #1255 is the one to go with.

DeprecationWarning: BaseException.message has been deprecated as of Python 2.6

All 2 comments

Good catch, yup. Of those two I think we should probably set a .message attribute on the exception instances, right here... https://github.com/encode/httpx/blob/19b863af408123783d5f9916e71688b32ba558c8/httpx/_exceptions.py#L63-L64

Changing it to...

super().__init__(message) 
self.message = message
self.request = request 

Though you're right, the alternative would be a documentation fix, to read eg...

print(f"HTTP Exception for {exc.request.url} - {exc}")

Since BaseException.message was deprecated in 2.6 and removed in 3, I think #1255 is the one to go with.

DeprecationWarning: BaseException.message has been deprecated as of Python 2.6
Was this page helpful?
0 / 5 - 0 ratings