I have the following code:
print("gather data")
try:
snow_response = requests.request("GET", SOME_URL, auth = snow_auth)
snow_response.raise_for_status()
snow_all_applications = json.loads(snow_response.text)["result"]
except (ConnectionAbortedError,ConnectionError,ConnectionRefusedError,TimeoutError,ConnectionResetError) as error:
print(error)
sys.exit()
except Exception as error:
print(f"Other exception occured. status code: {snow_response.status_code}")
print(error)
sys.exit()
I tried disconnecting my wifi to purposely trigger a connection error and here are the logs I get:
Traceback (most recent call last):
File "C:UsersMYUSERNAMEAppDataLocalProgramsPythonPython312Libsite-packagesurllib3connection.py", line 198, in _new_conn
sock = connection.create_connection(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersMYUSERNAMEAppDataLocalProgramsPythonPython312Libsite-packagesurllib3utilconnection.py", line 60, in create_connection
for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersMYUSERNAMEAppDataLocalProgramsPythonPython312Libsocket.py", line 963, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
socket.gaierror: [Errno 11001] getaddrinfo failed
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:UsersMYUSERNAMEAppDataLocalProgramsPythonPython312Libsite-packagesurllib3connectionpool.py", line 793, in urlopen
response = self._make_request(
^^^^^^^^^^^^^^^^^^^
File "C:UsersMYUSERNAMEAppDataLocalProgramsPythonPython312Libsite-packagesurllib3connectionpool.py", line 491, in _make_request
raise new_e
File "C:UsersMYUSERNAMEAppDataLocalProgramsPythonPython312Libsite-packagesurllib3connectionpool.py", line 467, in _make_request
self._validate_conn(conn)
File "C:UsersMYUSERNAMEAppDataLocalProgramsPythonPython312Libsite-packagesurllib3connectionpool.py", line 1099, in _validate_conn
conn.connect()
File "C:UsersMYUSERNAMEAppDataLocalProgramsPythonPython312Libsite-packagesurllib3connection.py", line 616, in connect
self.sock = sock = self._new_conn()
^^^^^^^^^^^^^^^^
File "C:UsersMYUSERNAMEAppDataLocalProgramsPythonPython312Libsite-packagesurllib3connection.py", line 205, in _new_conn
raise NameResolutionError(self.host, self, e) from e
urllib3.exceptions.NameResolutionError: <urllib3.connection.HTTPSConnection object at 0x0000023EE2264680>: Failed to resolve 'myurl.com' ([Errno 11001] getaddrinfo failed)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:UsersMYUSERNAMEAppDataLocalProgramsPythonPython312Libsite-packagesrequestsadapters.py", line 486, in send
resp = conn.urlopen(
^^^^^^^^^^^^^
File "C:UsersMYUSERNAMEAppDataLocalProgramsPythonPython312Libsite-packagesurllib3connectionpool.py", line 847, in urlopen
retries = retries.increment(
^^^^^^^^^^^^^^^^^^
File "C:UsersMYUSERNAMEAppDataLocalProgramsPythonPython312Libsite-packagesurllib3utilretry.py", line 515, in increment
raise MaxRetryError(_pool, url, reason) from reason # type: ignore[arg-type]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='myurl.com', port=443): Max retries exceeded with url: /api/now/xxxx (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x0000023EE2264680>: Failed to resolve 'myurl.com' ([Errno 11001] getaddrinfo failed)"))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:UsersMYUSERNAMEfolderpythonfile.py", line 85, in <module>
snow_response = requests.request("GET", SOME_URL, auth = snow_auth)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersMYUSERNAMEAppDataLocalProgramsPythonPython312Libsite-packagesrequestsapi.py", line 59, in request
return session.request(method=method, url=url, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersMYUSERNAMEAppDataLocalProgramsPythonPython312Libsite-packagesrequestssessions.py", line 589, in request
resp = self.send(prep, **send_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersMYUSERNAMEAppDataLocalProgramsPythonPython312Libsite-packagesrequestssessions.py", line 703, in send
r = adapter.send(request, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersMYUSERNAMEAppDataLocalProgramsPythonPython312Libsite-packagesrequestsadapters.py", line 519, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='myurl.com', port=443): Max retries exceeded with url: /api/now/xxxx (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x0000023EE2264680>: Failed to resolve 'myurl.com' ([Errno 11001] getaddrinfo failed)"))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:UsersMYUSERNAMEfolderpythonfile.py", line 96, in <module>
print(f"Other exception occured. status code: {snow_response.status_code}")
^^^^^^^^^^^^^
NameError: name 'snow_response' is not defined
So as of the last lines, we can clearly see that the first exception block is ignored.
I don’t get it because the ConnectionError is raised (line 62).