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.
This commit is contained in:
Kumi 2024-08-30 17:01:30 +02:00
parent 249fba16a6
commit 9b62a5704b
Signed by: kumi
GPG key ID: ECBCC9082395383F

View file

@ -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,