cyberjunky / python-garminconnect

Python 3 API wrapper for Garmin Connect to get activity statistics
MIT License
964 stars 149 forks source link

Local variable 'response' referenced before assignment #106

Closed CloCkWeRX closed 1 year ago

CloCkWeRX commented 1 year ago

See https://github.com/cyberjunky/home-assistant-garmin_connect/issues/41

If an exception is raised in any of the get/post methods - https://github.com/cyberjunky/python-garminconnect/blob/master/garminconnect/__init__.py#L69 sometimes the handler will try to use a variable that has never been defined.

Rather than handling Exception; more specifically handling requests.exceptions.ConnectionError (among others) would prevent this issue.

cyberjunky commented 1 year ago

Good find, will look into this!

giachello commented 1 year ago

Hey I also have this issue. It is a pretty easy solve. You can handle requests.exceptions.ConnectionError (and not reference response which is not defined, just return the raise). Would be great if you could fix this. I get a ton of logs for this and would love not to :-)

Here is my log:

2023-03-12 10:39:25.619 ERROR (MainThread) [custom_components.garmin_connect] Unknown error occurred during Garmin Connect login request
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/urllib3/connectionpool.py", line 703, in urlopen
    httplib_response = self._make_request(
  File "/usr/local/lib/python3.10/site-packages/urllib3/connectionpool.py", line 449, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "/usr/local/lib/python3.10/site-packages/urllib3/connectionpool.py", line 444, in _make_request
    httplib_response = conn.getresponse()
  File "/usr/local/lib/python3.10/http/client.py", line 1374, in getresponse
    response.begin()
  File "/usr/local/lib/python3.10/http/client.py", line 318, in begin
    version, status, reason = self._read_status()
  File "/usr/local/lib/python3.10/http/client.py", line 287, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/requests/adapters.py", line 489, in send
    resp = conn.urlopen(
  File "/usr/local/lib/python3.10/site-packages/urllib3/connectionpool.py", line 787, in urlopen
    retries = retries.increment(
  File "/usr/local/lib/python3.10/site-packages/urllib3/util/retry.py", line 550, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "/usr/local/lib/python3.10/site-packages/urllib3/packages/six.py", line 769, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/lib/python3.10/site-packages/urllib3/connectionpool.py", line 703, in urlopen
    httplib_response = self._make_request(
  File "/usr/local/lib/python3.10/site-packages/urllib3/connectionpool.py", line 449, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "/usr/local/lib/python3.10/site-packages/urllib3/connectionpool.py", line 444, in _make_request
    httplib_response = conn.getresponse()
  File "/usr/local/lib/python3.10/http/client.py", line 1374, in getresponse
    response.begin()
  File "/usr/local/lib/python3.10/http/client.py", line 318, in begin
    version, status, reason = self._read_status()
  File "/usr/local/lib/python3.10/http/client.py", line 287, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
urllib3.exceptions.ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/garminconnect/__init__.py", line 70, in get
    response = self.session.get(url, headers=total_headers, params=params)
  File "/usr/local/lib/python3.10/site-packages/requests/sessions.py", line 600, in get
    return self.request("GET", url, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/cloudscraper/__init__.py", line 257, in request
    self.perform_request(method, url, *args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/cloudscraper/__init__.py", line 190, in perform_request
    return super(CloudScraper, self).request(method, url, *args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/requests/sessions.py", line 587, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.10/site-packages/requests/sessions.py", line 701, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/requests/adapters.py", line 547, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/config/custom_components/garmin_connect/__init__.py", line 70, in async_login
    await self.hass.async_add_executor_job(self._api.login)
  File "/usr/local/lib/python3.10/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/local/lib/python3.10/site-packages/garminconnect/__init__.py", line 251, in login
    return self.login_session()
  File "/usr/local/lib/python3.10/site-packages/garminconnect/__init__.py", line 273, in login_session
    response = self.sso_rest_client.get("login", params=params)
  File "/usr/local/lib/python3.10/site-packages/garminconnect/__init__.py", line 75, in get
    logger.debug("Response in exception: %s", response.content)
UnboundLocalError: local variable 'response' referenced before assignment
cyberjunky commented 1 year ago

I will have a look tomorrow... thanks for reporting!

cyberjunky commented 1 year ago

@CloCkWeRX @giachello Can you try version 0.1.55 and let me know if this fixes your issues? Thanks!

CloCkWeRX commented 1 year ago

I mostly see this through https://github.com/cyberjunky/home-assistant-garmin_connect to will wait for that to get bumped. Thanks for sorting it eiher way