ProtoThis / python-synology

MIT License
55 stars 38 forks source link

Timeout exceptions not handled properly #47

Open kuchel77 opened 4 years ago

kuchel77 commented 4 years ago

When using the example script, and putting in the details for my NAS which has been turned off, I am getting the following messages as the timeout exceptions haven't be properly handled in the code.

This also then impacts on Home Assistant using this, and this is filling the logs as well rather than a simple offline message.

Creating Valid API

=== Information ===
DEBUG: Request params: {'api': 'SYNO.API.Info', 'version': 1, 'method': 'query'}
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/connection.py", line 156, in _new_conn
    conn = connection.create_connection(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/util/connection.py", line 84, in create_connection
    raise err
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/util/connection.py", line 74, in create_connection
    sock.connect(sa)
TimeoutError: [Errno 60] Operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/connectionpool.py", line 665, in urlopen
    httplib_response = self._make_request(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/connectionpool.py", line 387, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1230, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1276, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1225, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1004, in _send_output
    self.send(msg)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 944, in send
    self.connect()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/connection.py", line 184, in connect
    conn = self._new_conn()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/connection.py", line 168, in _new_conn
    raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7fe1e84aa100>: Failed to establish a new connection: [Errno 60] Operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/requests/adapters.py", line 439, in send
    resp = conn.urlopen(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/connectionpool.py", line 719, in urlopen
    retries = retries.increment(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/util/retry.py", line 436, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='192.168.0.152', port=5000): Max retries exceeded with url: /webapi/query.cgi?api=SYNO.API.Info&version=1&method=query (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fe1e84aa100>: Failed to establish a new connection: [Errno 60] Operation timed out'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/t08640554/Documents/python-synology/synology_dsm/synology_dsm.py", line 240, in _execute_request
    resp = self._session.get(url, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/requests/sessions.py", line 543, in get
    return self.request('GET', url, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/requests/sessions.py", line 530, in request
    resp = self.send(prep, **send_kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/requests/sessions.py", line 643, in send
    r = adapter.send(request, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/requests/adapters.py", line 516, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='192.168.0.152', port=5000): Max retries exceeded with url: /webapi/query.cgi?api=SYNO.API.Info&version=1&method=query (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fe1e84aa100>: Failed to establish a new connection: [Errno 60] Operation timed out'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 7, in <module>
    print("Model:           " + str(api.information.model))
  File "/Users/t08640554/Documents/python-synology/synology_dsm/synology_dsm.py", line 280, in information
    data = self.get(SynoDSMInformation.API_KEY, "getinfo")
  File "/Users/t08640554/Documents/python-synology/synology_dsm/synology_dsm.py", line 173, in get
    return self._request("GET", api, method, params, **kwargs)
  File "/Users/t08640554/Documents/python-synology/synology_dsm/synology_dsm.py", line 187, in _request
    self.discover_apis()
  File "/Users/t08640554/Documents/python-synology/synology_dsm/synology_dsm.py", line 108, in discover_apis
    self._apis = self.get(self.API_INFO, "query")["data"]
  File "/Users/t08640554/Documents/python-synology/synology_dsm/synology_dsm.py", line 173, in get
    return self._request("GET", api, method, params, **kwargs)
  File "/Users/t08640554/Documents/python-synology/synology_dsm/synology_dsm.py", line 217, in _request
    response = self._execute_request(request_method, url, params=params, **kwargs)
  File "/Users/t08640554/Documents/python-synology/synology_dsm/synology_dsm.py", line 256, in _execute_request
    raise SynologyDSMRequestException(exp)
synology_dsm.exceptions.SynologyDSMRequestException: ConnectionError = <urllib3.connection.HTTPConnection object at 0x7fe1e84aa100>: Failed to establish a new connection: [Errno 60] Operation timed out
Quentame commented 4 years ago

Is it better with #56 ?

kuchel77 commented 4 years ago
from synology_dsm import SynologyDSM

print("Creating Valid API")
api = SynologyDSM("192.168.0.152", "5000", "mark", "pwd", False, timeout=30, debugmode=True)

That is essentially my test code above. My NAS was using 192.168.0.152 previously but once I got the network bonding and finalised setup it is now using 192.168.0.240. Changing the timeout just changes how long it takes to spit out the error above rather than handling any errors. I have no device using 192.168.0.152 to reply with a http status code or connection refused or anything similar.

There is a similar error if I point it to another server, that says connection refused on port 5000 with a whole lot of unhandled exceptions as well.

In the tests, if you had INVALID_HOST as well as VALID_HOST, it would show the same errors as above.

Quentame commented 4 years ago

For my tests I was not in my local network (still not) so maybe that's why.

What do you mean by "not handled properly" ? There is a SynologyDSMRequestException but the issue is that it prints all the stack above ?

Quentame commented 3 years ago

Do you have an idea on how to fix this ?

Eventually submit a PR if you can.