Difficulty catching an exception

I’m trying to embrace python a little more but I’m having problems catching a particular error. Here is the traceback.

2017-03-01 12:20:25.210662 WARNING Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/appdaemon/appdaemon.py", line 602, in worker
    function(ha.sanitize_timer_kwargs(args["kwargs"]))
  File "/home/homeassistant/code/appdaemon/calalarm/calalarm.py", line 194, in checkifcalchanged
    self.schedulealarm(self.roomowners[owner]["room"])
  File "/home/homeassistant/code/appdaemon/calalarm/calalarm.py", line 266, in schedulealarm
    meeting=self.getMeetings(self.roomowners[o]["calendar"])
  File "/home/homeassistant/code/appdaemon/calalarm/calalarm.py", line 280, in getMeetings
    response = request.execute()
  File "/usr/local/lib/python3.4/dist-packages/oauth2client/_helpers.py", line 133, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/googleapiclient/http.py", line 835, in execute
    method=str(self.method), body=self.body, headers=self.headers)
  File "/usr/local/lib/python3.4/dist-packages/googleapiclient/http.py", line 175, in _retry_request
    raise exception
  File "/usr/local/lib/python3.4/dist-packages/googleapiclient/http.py", line 162, in _retry_request
    resp, content = http.request(uri, method, *args, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/oauth2client/transport.py", line 175, in new_request
    redirections, connection_type)
  File "/usr/local/lib/python3.4/dist-packages/oauth2client/transport.py", line 282, in request
    connection_type=connection_type)
  File "/usr/local/lib/python3.4/dist-packages/httplib2/__init__.py", line 1322, in request
    (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
  File "/usr/local/lib/python3.4/dist-packages/httplib2/__init__.py", line 1072, in _request
    (response, content) = self._conn_request(conn, request_uri, method, body, headers)
  File "/usr/local/lib/python3.4/dist-packages/httplib2/__init__.py", line 1025, in _conn_request
    response = conn.getresponse()
  File "/usr/lib/python3.4/http/client.py", line 1172, in getresponse
    response.begin()
  File "/usr/lib/python3.4/http/client.py", line 351, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python3.4/http/client.py", line 313, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/usr/lib/python3.4/socket.py", line 371, in readinto
    return self._sock.recv_into(b)
  File "/usr/lib/python3.4/ssl.py", line 745, in recv_into
    return self.read(nbytes, buffer)
  File "/usr/lib/python3.4/ssl.py", line 617, in read
    v = self._sslobj.read(len, buffer)
ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1769)

2017-03-01 12:20:25.211232 WARNING ------------------------------------------------------------
2017-03-01 12:20:25.211663 WARNING ------------------------------------------------------------

This is ok, and really just needs to wait till the next loop and try again. I tried

try:
statement that throws this error
except SSLError
also tried
except ssl.SSLError

Both say that SSLError or ssl.SSLError are not defined. How do I trap this?

Maybe import ssl?

I think I tried that before, but maybe not. We’ll see if it keeps going the next time it hits it. I put a log message in there where I catch it to let me know if it got executed and escaped ok.

Can you post lines from your app where you’re attempting to catch this exception? Likely in getMeetings in calalarm.py

and what are your imports

so far it’s looking like just including ssl did it. I thought I had done that before, but maybe not. On the other hand, I’ve been changing some things in several modules tonight so it may have happened and I just didn’t catch it. I know where the error is getting thrown and I think it’s google timing out on me. If I just restart the app, it will fun fine for a while ( maybe an hour, maybe a week). I’ll let it run overnight and see if I catch it tomorrow. but it’s literally as simple as I posted above. My question is really about how do I determine what value or name to put on the except line.

import ssl

try:
    # some_logic
except ssl.SSLError as e:
    # recovery_logic_here

Should totally work! :slight_smile:

Optionally, you could also do. Both ways are correct, and precise.

import ssl
from ssl import SSLError

try:
    # some_logic
except SSLError as e:
    # recovery_logic_here