From 3e978277b38a2eec9d18f887a7af75df945dd51b Mon Sep 17 00:00:00 2001 From: Rahiel Kasim Date: Tue, 9 Feb 2016 15:28:54 +0100 Subject: [PATCH] echobot: simplify exception handling --- examples/legacy/echobot.py | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/examples/legacy/echobot.py b/examples/legacy/echobot.py index 8d7e07b9a..300d9fa6c 100644 --- a/examples/legacy/echobot.py +++ b/examples/legacy/echobot.py @@ -6,13 +6,9 @@ import logging import telegram +from telegram.error import NetworkError, Unauthorized from time import sleep -try: - from urllib.error import URLError -except ImportError: - from urllib2 import URLError # python 2 - def main(): # Telegram Bot Authorization Token @@ -31,18 +27,11 @@ def main(): while True: try: update_id = echo(bot, update_id) - except telegram.TelegramError as e: - # These are network problems with Telegram. - if e.message in ("Bad Gateway", "Timed out"): - sleep(1) - elif e.message == "Unauthorized": - # The user has removed or blocked the bot. - update_id += 1 - else: - raise e - except URLError as e: - # These are network problems on our end. + except NetworkError: sleep(1) + except Unauthorized: + # The user has removed or blocked the bot. + update_id += 1 def echo(bot, update_id):