From 9b62a5704bd0db1be65029d6cd672bf25288f935 Mon Sep 17 00:00:00 2001 From: Kumi Date: Fri, 30 Aug 2024 17:01:30 +0200 Subject: [PATCH] fix(error-handling): improve search results error handling Added a try-except block around the search results API call to handle potential errors gracefully. Now, if an exception occurs, an error message is logged, and the user is redirected to an error page with a 500 status code. This improves user experience by providing clear feedback when the search functionality fails. --- src/wikimore/app.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/wikimore/app.py b/src/wikimore/app.py index 65e14e2..c9f13d5 100644 --- a/src/wikimore/app.py +++ b/src/wikimore/app.py @@ -445,9 +445,22 @@ def search_results( url = ( f"{base_url}/w/api.php?action=query&format=json&list=search&srsearch={srquery}" ) - with urllib.request.urlopen(url) as response: - data = json.loads(response.read().decode()) - search_results = data["query"]["search"] + + try: + with urllib.request.urlopen(url) as response: + data = json.loads(response.read().decode()) + search_results = data["query"]["search"] + except Exception as e: + logger.error(f"Error fetching search results: {e}") + return ( + render_template( + "article.html", + title="Search Error", + content="An error occurred while fetching search results. Please try again later.", + ), + 500, + ) + return render_template( "search_results.html", query=query,