From a61bbd9db33392b0969b5264876641952cfb9845 Mon Sep 17 00:00:00 2001 From: Florian Strzelecki Date: Fri, 9 Jul 2021 23:15:39 +0200 Subject: [PATCH] translate: catch requests exceptions This is #2153, cherry-picked back to 7.1.x by @dgw. --- sopel/modules/translate.py | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/sopel/modules/translate.py b/sopel/modules/translate.py index ebd4ef11b7..466a20ed31 100644 --- a/sopel/modules/translate.py +++ b/sopel/modules/translate.py @@ -10,6 +10,7 @@ from __future__ import absolute_import, division, print_function, unicode_literals import json +import logging import random import sys @@ -21,6 +22,7 @@ if sys.version_info.major >= 3: unicode = str +LOGGER = logging.getLogger(__name__) PLUGIN_OUTPUT_PREFIX = '[translate] ' @@ -68,6 +70,9 @@ def translate(text, in_lang='auto', out_lang='en'): try: data = json.loads(result) except ValueError: + LOGGER.error( + 'Error parsing JSON response from translate API (%s to %s: "%s")', + in_lang, out_lang, text) return None, None if raw: @@ -104,7 +109,21 @@ def tr(bot, trigger): bot.reply('Language guessing failed, so try suggesting one!') return - msg, in_lang = translate(phrase, in_lang, out_lang) + try: + msg, in_lang = translate(phrase, in_lang, out_lang) + except requests.Timeout: + bot.reply("Translation service unavailable (timeout).") + LOGGER.error( + 'Translate API error (%s to %s: "%s"): timeout.', + in_lang, out_lang, phrase) + return + except requests.RequestException as http_error: + bot.reply("Translation request failed.") + LOGGER.exception( + 'Translate API error (%s to %s: "%s"): %s.', + in_lang, out_lang, phrase, http_error) + return + if not in_lang: bot.reply("Translation failed, probably because of a rate-limit.") return @@ -171,7 +190,21 @@ def langcode(p): bot.reply('Language guessing failed, so try suggesting one!') return - msg, src = translate(phrase, src, dest) + try: + msg, src = translate(phrase, src, dest) + except requests.Timeout: + bot.reply("Translation service unavailable (timeout).") + LOGGER.error( + 'Translate API error (%s to %s: "%s"): timeout.', + src, dest, phrase) + return + except requests.RequestException as http_error: + bot.reply("Translation request failed.") + LOGGER.exception( + 'Translate API error (%s to %s: "%s"): %s.', + src, dest, phrase, http_error) + return + if not src: return bot.say("Translation failed, probably because of a rate-limit.")