fix: check if project exists when activating
dipi.evil committed
Dec 19, 2025 at 11:18 UTC
07daf31b395831b43d2bbddfdf26a84c84c7713c
1 file changed
+20
-4
python/api/api_message.py
+20
-4
@@ -86,10 +86,26 @@ class ApiMessage(ApiHandler):
86
context = AgentContext(config=config, type=AgentContextType.USER)
87
AgentContext.use(context.id)
88
context_id = context.id
89
-
90
- # Activate project if provided
91
- if project_name:
92
- activate_project(context_id, project_name)
89
+ # Activate project if provided
90
+ if project_name:
91
+ try:
92
+ activate_project(context_id, project_name)
93
+ except Exception as e:
94
+ # Handle non-existent project or context errors more gracefully
95
+ error_msg = str(e)
96
+ PrintStyle.error(f"Failed to activate project '{project_name}' for context '{context_id}': {error_msg}")
97
+ # If the error message suggests a missing resource, return 404; otherwise, 500
98
+ if "not found" in error_msg.lower() or "does not exist" in error_msg.lower():
99
+ return Response(
100
+ f'{{"error": "Project \\"{project_name}\\" not found"}}',
101
+ status=404,
102
+ mimetype="application/json",
103
+ )
104
+ return Response(
105
+ f'{{"error": "Failed to activate project \\"{project_name}\\""}}',
106
+ status=500,
107
+ mimetype="application/json",
108
+ )
109
110
# Update chat lifetime
111
with self._cleanup_lock: