move checking of error reason out of try/except in connection.py