From a1786d4e775767dfb42ca0d7c70975519bb1c908 Mon Sep 17 00:00:00 2001 From: NNTin Date: Thu, 10 Jan 2019 16:04:48 +0100 Subject: [PATCH] handling ConnectionError --- bot/main.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/bot/main.py b/bot/main.py index c41a98a6..1c64215e 100644 --- a/bot/main.py +++ b/bot/main.py @@ -12,6 +12,7 @@ import re from config import data_json import urllib3 +import requests class StdOutListener(StreamListener): @@ -268,24 +269,24 @@ def on_warning(self, notice): print('Twitter stream started.') while True: - try: - stream.filter(follow=data_json['twitter_ids']) - except urllib3.exceptions.ProtocolError as error: + def print_error(_error): print('---------Error---------') - print('This is probably caused by "Connection reset by peer." Ignore. Nothing you can do.') - print(error) - print('Sleeping for 10 seconds then continuing.') - sleep(10) + print('Known error. Ignore. Nothing you can do.') + print(_error) + print('Sleeping for 1 minute then continuing.') + sleep(600) print('Twitter streaming continues.') print('-----------------------') + try: + stream.filter(follow=data_json['twitter_ids']) + except urllib3.exceptions.ProtocolError as error: + print_error(_error=error) except ConnectionResetError as error: - print('---------Error---------') - print('This is probably caused by "Connection reset by peer." Ignore. Nothing you can do.') - print(error) - print('Sleeping for 10 seconds then continuing.') - sleep(10) - print('Twitter streaming continues.') - print('-----------------------') + print_error(_error=error) + except ConnectionError as error: + print_error(_error=error) + except requests.exceptions.ConnectionError as error: + print_error(_error=error) except Exception as error: print('---------Error---------') print('unknown error')