Skip to content

Commit e602e66

Browse files
error handling added in fetch content
1 parent 2c32de8 commit e602e66

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/app.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,18 +1353,17 @@ async def fetch_azure_search_content():
13531353
url = request_json.get("url")
13541354

13551355
if not url:
1356-
track_event_if_configured("FetchAzureSearchContentFailed", {"error": "URL is required"})
13571356
return jsonify({"error": "URL is required"}), 400
13581357

13591358
# Get Azure AD token
13601359
credential = DefaultAzureCredentialSync()
13611360
token = credential.get_token("https://search.azure.com/.default")
13621361
access_token = token.token
13631362

1364-
def fetch_content():
1363+
def fetch_content(fetch_url):
13651364
try:
13661365
response = requests.get(
1367-
url,
1366+
fetch_url,
13681367
headers={
13691368
"Authorization": f"Bearer {access_token}",
13701369
"Content-Type": "application/json"
@@ -1374,16 +1373,20 @@ def fetch_content():
13741373
if response.status_code == 200:
13751374
data = response.json()
13761375
content = data.get("content", "")
1377-
return content
1376+
return {"success": True, "content": content}
13781377
else:
1379-
return
1378+
error_msg = f"Request failed with status code {response.status_code} {response.text}"
1379+
return {"success": False, "error": error_msg}
13801380
except Exception as e:
13811381
logging.exception("Error fetching content from Azure Search")
1382-
print(f"Error fetching content from Azure Search: {str(e)}")
1383-
return f"Error: {str(e)}"
1382+
return {"success": False, "error": f"Exception: {str(e)}"}
13841383

1385-
content = await asyncio.to_thread(fetch_content)
1386-
return jsonify({"content": content}), 200
1384+
result = await asyncio.to_thread(fetch_content, url)
1385+
1386+
if result["success"]:
1387+
return jsonify({"content": result["content"]}), 200
1388+
else:
1389+
return jsonify({"error": result["error"]}), 500
13871390

13881391
except Exception as e:
13891392
logging.exception("Exception in /fetch-azure-search-content")

0 commit comments

Comments
 (0)