adafruit / Adafruit_CircuitPython_Requests

Requests-like interface for web interfacing
MIT License
51 stars 36 forks source link

preserve last exception when things are going wrong #124

Closed jepler closed 1 year ago

jepler commented 1 year ago

CircuitPython 8 supports exception chaining, so that the original problem can be shown. Here's how it looks on desktop python3:

Traceback (most recent call last):
  File "/home/jepler/src/bundle/libraries/helpers/requests/adafruit_requests.py", line 527, in _get_socket
    sock.connect((connect_host, port))
ConnectionRefusedError: [Errno 111] Connection refused

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/jepler/src/bundle/libraries/helpers/requests/adafruit_requests.py", line 721, in get
    return self.request("GET", url, **kw)
  File "/home/jepler/src/bundle/libraries/helpers/requests/adafruit_requests.py", line 661, in request
    socket = self._get_socket(host, port, proto, timeout=timeout)
  File "/home/jepler/src/bundle/libraries/helpers/requests/adafruit_requests.py", line 508, in _get_socket
    raise RuntimeError("Sending request failed") from last_exc
RuntimeError: Sending request failed

Before, just the second block would be shown, and it would be hidden that the underlying cause was "connection refused".