From efdfbf33cebdef31ae4ee51def459b4990cdf3bd Mon Sep 17 00:00:00 2001 From: cthomas Date: Wed, 12 Feb 2025 16:45:18 -0800 Subject: [PATCH] fix: catch tool parsing errors in api layer (#983) --- letta/server/rest_api/routers/v1/tools.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/letta/server/rest_api/routers/v1/tools.py b/letta/server/rest_api/routers/v1/tools.py index 912503fe..58588d77 100644 --- a/letta/server/rest_api/routers/v1/tools.py +++ b/letta/server/rest_api/routers/v1/tools.py @@ -124,6 +124,10 @@ def upsert_tool( # Log the error and raise a conflict exception print(f"Unique constraint violation occurred: {e}") raise HTTPException(status_code=409, detail=str(e)) + except LettaToolCreateError as e: + # HTTP 400 == Bad Request + print(f"Error occurred during tool upsert: {e}") + raise HTTPException(status_code=400, detail=str(e)) except Exception as e: # Catch other unexpected errors and raise an internal server error print(f"Unexpected error occurred: {e}") @@ -140,8 +144,17 @@ def modify_tool( """ Update an existing tool """ - actor = server.user_manager.get_user_or_default(user_id=user_id) - return server.tool_manager.update_tool_by_id(tool_id=tool_id, tool_update=request, actor=actor) + try: + actor = server.user_manager.get_user_or_default(user_id=user_id) + return server.tool_manager.update_tool_by_id(tool_id=tool_id, tool_update=request, actor=actor) + except LettaToolCreateError as e: + # HTTP 400 == Bad Request + print(f"Error occurred during tool update: {e}") + raise HTTPException(status_code=400, detail=str(e)) + except Exception as e: + # Catch other unexpected errors and raise an internal server error + print(f"Unexpected error occurred: {e}") + raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}") @router.post("/add-base-tools", response_model=List[Tool], operation_id="add_base_tools")