mwalker22 commited on
Commit
b31021c
·
1 Parent(s): 5f64962

PR Requested change for improving the processing and testing of potential JSON errors.

Browse files
backend/tools/course_insights_tool.py CHANGED
@@ -34,9 +34,13 @@ def course_insights(search_query: str) -> str:
34
  # Step 1: Search
35
  search_resp = requests.get(f"{BASE_URL}/search", headers=get_headers(), params={"search_query": search_query})
36
  search_resp.raise_for_status()
37
- courses = search_resp.json().get("courses", [])
38
- logger.debug(f"[TOOL RESULT] course_insights: {json.dumps(search_resp.json())}")
39
- logger.debug(f"Number of courses found: {len(courses)}")
 
 
 
 
40
 
41
  if not courses:
42
  return f"No courses found for query '{search_query}'."
@@ -50,8 +54,12 @@ def course_insights(search_query: str) -> str:
50
 
51
  detail_resp = requests.get(f"{BASE_URL}/courses/{course_id}", headers=get_headers())
52
  detail_resp.raise_for_status()
53
- data = detail_resp.json()
54
- course = data.get("course", {})
 
 
 
 
55
 
56
  tees = course.get("tees", {})
57
  all_tees = []
 
34
  # Step 1: Search
35
  search_resp = requests.get(f"{BASE_URL}/search", headers=get_headers(), params={"search_query": search_query})
36
  search_resp.raise_for_status()
37
+ try:
38
+ courses = search_resp.json().get("courses", [])
39
+ logger.debug(f"[TOOL RESULT] course_insights: {json.dumps(search_resp.json())}")
40
+ logger.debug(f"Number of courses found: {len(courses)}")
41
+ except json.JSONDecodeError as e:
42
+ logger.error(f"[ERROR] JSONDecodeError: {e}")
43
+ return "An unexpected error occurred: Invalid JSON"
44
 
45
  if not courses:
46
  return f"No courses found for query '{search_query}'."
 
54
 
55
  detail_resp = requests.get(f"{BASE_URL}/courses/{course_id}", headers=get_headers())
56
  detail_resp.raise_for_status()
57
+ try:
58
+ data = detail_resp.json()
59
+ course = data.get("course", {})
60
+ except json.JSONDecodeError as e:
61
+ logger.error(f"[ERROR] JSONDecodeError: {e}")
62
+ return "An unexpected error occurred: Invalid JSON"
63
 
64
  tees = course.get("tees", {})
65
  all_tees = []