agrc / masquerade

Disguise UGRC's Web API endpoints as an Esri locator service.
https://github.com/agrc/masquerade#readme
MIT License
1 stars 0 forks source link

Uncaught exception #118

Closed steveoh closed 1 year ago

steveoh commented 1 year ago

https://github.com/agrc/masquerade/blob/0421f21dcb564ba9d6d006f61628af7016247560/src/masquerade/providers/web_api.py#L106

There is no try except for https://requests.readthedocs.io/en/latest/api/#requests.Timeout exceptions.

This will happen after your timeout expires unless you are catching it somewhere else that I don't see?

response = requests.get("https://slow.com/", timeout=.1)
Traceback (most recent call last):
  File "urllib3/connectionpool.py", line 386, in _make_request
    self._validate_conn(conn)
  File "urllib3/connectionpool.py", line 1042, in _validate_conn
    conn.connect()
  File "urllib3/connection.py", line 419, in connect
    self.sock = ssl_wrap_socket(
                ^^^^^^^^^^^^^^^^
  File "urllib3/util/ssl_.py", line 449, in ssl_wrap_socket
    ssl_sock = _ssl_wrap_socket_impl(
               ^^^^^^^^^^^^^^^^^^^^^^
  File "urllib3/util/ssl_.py", line 493, in _ssl_wrap_socket_impl
    return ssl_context.wrap_socket(sock, server_hostname=server_hostname)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/ssl.py", line 517, in wrap_socket
    return self.sslsocket_class._create(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/ssl.py", line 1075, in _create
    self.do_handshake()
  File "/ssl.py", line 1346, in do_handshake
    self._sslobj.do_handshake()
TimeoutError: _ssl.c:985: The handshake operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "requests/adapters.py", line 489, in send
    resp = conn.urlopen(
           ^^^^^^^^^^^^^
  File "urllib3/connectionpool.py", line 787, in urlopen
    retries = retries.increment(
              ^^^^^^^^^^^^^^^^^^
  File "urllib3/util/retry.py", line 550, in increment
    raise six.reraise(type(error), error, _stacktrace)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "urllib3/packages/six.py", line 770, in reraise
    raise value
  File "urllib3/connectionpool.py", line 703, in urlopen
    httplib_response = self._make_request(
                       ^^^^^^^^^^^^^^^^^^^
  File "urllib3/connectionpool.py", line 389, in _make_request
    self._raise_timeout(err=e, url=url, timeout_value=conn.timeout)
  File "urllib3/connectionpool.py", line 340, in _raise_timeout
    raise ReadTimeoutError(
urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='slow.com', port=443): Read timed out. (read timeout=0.1)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "requests/api.py", line 73, in get
    return request("get", url, params=params, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "requests/api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "requests/sessions.py", line 587, in request
    resp = self.send(prep, **send_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "requests/sessions.py", line 701, in send
    r = adapter.send(request, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "requests/adapters.py", line 578, in send
    raise ReadTimeout(e, request=request)
requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='slow.com', port=443): Read timed out. (read timeout=0.1)

Something like the following would be more fault tolerant

import requests

try:
    response = requests.get(url, timeout=5)
    response.raise_for_status() # Raise an exception if the status code is not 200 OK
except requests.exceptions.Timeout:
    # Timeout occurred
    print("Timeout error occurred")
except requests.exceptions.RequestException as e:
    # Other error occurred
    print("Error occurred: {}".format(e))
stdavis commented 1 year ago

I believe that those exceptions would be caught in these places. But I'm open to other ideas. What do you think? https://github.com/agrc/masquerade/blob/0421f21dcb564ba9d6d006f61628af7016247560/src/masquerade/main.py#L202-L213 https://github.com/agrc/masquerade/blob/0421f21dcb564ba9d6d006f61628af7016247560/src/masquerade/main.py#L269-L276