From a58c5879804dab4ebb906d9fcd153386d3130570 Mon Sep 17 00:00:00 2001 From: dece Date: Fri, 27 May 2022 01:35:54 +0200 Subject: [PATCH] wordreference: capture some requests exceptions --- wordreference.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/wordreference.py b/wordreference.py index 4b5a76e..c22fe59 100755 --- a/wordreference.py +++ b/wordreference.py @@ -91,7 +91,10 @@ def main(): def get_translations(lang, words): """Get translations for these words.""" encoded_words = urllib.parse.quote(words) - response = requests.get(f"{URL}/{lang}/{encoded_words}") + try: + response = requests.get(f"{URL}/{lang}/{encoded_words}") + except requests.exceptions.RequestException: + exit("Could not connect to WordReference.") if response.status_code != 200: exit("Could not connect to WordReference.") @@ -233,7 +236,10 @@ AUTOCOMP_URL = f"{URL}/2012/autocomplete/autocomplete.aspx" def get_suggestions(lang, words): """Show completion suggestions for these words.""" params = {"dict": lang, "query": words} - response = requests.get(AUTOCOMP_URL, params=params) + try: + response = requests.get(AUTOCOMP_URL, params=params) + except requests.exceptions.RequestException: + exit("Could not connect to WordReference.") if response.status_code != 200: exit("Could not connect to WordReference.")