mirror of
https://github.com/alterware/aw-bot.git
synced 2025-11-18 09:02:07 +00:00
chore: fix exception handling here as well
This commit is contained in:
@@ -22,16 +22,27 @@ def get_topics_by_id(topic_id):
|
|||||||
Returns:
|
Returns:
|
||||||
dict or None: The topic data if successful, otherwise None.
|
dict or None: The topic data if successful, otherwise None.
|
||||||
"""
|
"""
|
||||||
response = requests.get(f"{DISCOURSE_BASE_URL}/t/{topic_id}.json", headers=headers)
|
try:
|
||||||
if response.status_code == 200:
|
response = requests.get(
|
||||||
return response.json()
|
f"{DISCOURSE_BASE_URL}/t/{topic_id}.json",
|
||||||
elif response.status_code == 403:
|
headers=headers,
|
||||||
logger.error(f"Access forbidden for topic {topic_id}: {response.status_code}")
|
timeout=10, # prevent hanging forever
|
||||||
return None
|
|
||||||
else:
|
|
||||||
logger.error(
|
|
||||||
f"Error fetching topic {topic_id}: {response.status_code} - {response.text}"
|
|
||||||
)
|
)
|
||||||
|
if response.status_code == 200:
|
||||||
|
return response.json()
|
||||||
|
elif response.status_code == 403:
|
||||||
|
logger.error(
|
||||||
|
f"Access forbidden for topic {topic_id}: {response.status_code}"
|
||||||
|
)
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
logger.error(
|
||||||
|
f"Error fetching topic {topic_id}: {response.status_code} - {response.text}"
|
||||||
|
)
|
||||||
|
return None
|
||||||
|
|
||||||
|
except requests.exceptions.RequestException as e:
|
||||||
|
logger.error(f"Request failed for topic {topic_id}: {e}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user