Prof-Reza commited on
Commit
901e364
·
verified ·
1 Parent(s): 62bf7f1

Fix syntax error in JSON error handler and ensure error details are escaped safely

Browse files

Correct a syntax error in the exception handling of finalize_and_doc() that produced invalid Python code in the f-string. The code now uses json.dumps to build a proper JSON error response, eliminating the unexpected character after the line continuation character.

Files changed (1) hide show
  1. app.py +6 -4
app.py CHANGED
@@ -490,10 +490,12 @@ def finalize_and_doc(chat_history, chat_pairs, sources, plan, chat_key):
490
  try:
491
  json_string = plan_course(chat_history, sources)
492
  except Exception as e:
493
- json_string = (
494
- "{\n \"error\": \"An error occurred while generating the course outline.\",\n"
495
- f" \"details\": \"{str(e).replace('"', '\\"')}\"\n}}"
496
- )
 
 
497
  # Attempt to parse the JSON to ensure it is valid; if it fails, wrap as raw string
498
  try:
499
  parsed = json.loads(json_string)
 
490
  try:
491
  json_string = plan_course(chat_history, sources)
492
  except Exception as e:
493
+ # Construct a minimal JSON error message safely using json.dumps to escape characters
494
+ error_obj = {
495
+ "error": "An error occurred while generating the course outline.",
496
+ "details": str(e),
497
+ }
498
+ json_string = json.dumps(error_obj, indent=2)
499
  # Attempt to parse the JSON to ensure it is valid; if it fails, wrap as raw string
500
  try:
501
  parsed = json.loads(json_string)