diff --git a/backend/apps/agent_app.py b/backend/apps/agent_app.py index 86716d80a..cf6ffd574 100644 --- a/backend/apps/agent_app.py +++ b/backend/apps/agent_app.py @@ -61,8 +61,11 @@ async def agent_run_api(agent_request: AgentRequest, http_request: Request, auth ) except Exception as e: logger.error(f"Agent run error: {str(e)}") + # Only expose actual error in debug mode for better diagnosis + # Keep generic message in normal mode for user experience + error_detail = str(e) if agent_request.is_debug else "Agent run error." raise HTTPException( - status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail="Agent run error.") + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail=error_detail) @agent_runtime_router.get("/stop/{conversation_id}") diff --git a/test/backend/app/test_agent_app.py b/test/backend/app/test_agent_app.py index 22365cf0b..2582e197d 100644 --- a/test/backend/app/test_agent_app.py +++ b/test/backend/app/test_agent_app.py @@ -200,6 +200,56 @@ async def mock_stream(): assert "data: chunk2" in content +async def test_agent_run_api_error_debug_mode(mocker, mock_auth_header): + """Test agent_run_api error case in debug mode - should expose actual error.""" + mock_run_agent_stream = mocker.patch( + "apps.agent_app.run_agent_stream", new_callable=mocker.AsyncMock) + mock_run_agent_stream.side_effect = Exception("Test error") + + response = runtime_client.post( + "/agent/run", + json={ + "agent_id": 1, + "conversation_id": 123, + "query": "test query", + "history": [], + "minio_files": [], + "is_debug": True, # Debug mode + }, + headers=mock_auth_header + ) + + assert response.status_code == 500 + # In debug mode, actual error should be exposed + assert "Test error" in response.json()["detail"] + + +async def test_agent_run_api_error_normal_mode(mocker, mock_auth_header): + """Test agent_run_api error case in normal mode - should show generic error.""" + mock_run_agent_stream = mocker.patch( + "apps.agent_app.run_agent_stream", new_callable=mocker.AsyncMock) + mock_run_agent_stream.side_effect = Exception("Test internal error") + + response = runtime_client.post( + "/agent/run", + json={ + "agent_id": 1, + "conversation_id": 123, + "query": "test query", + "history": [], + "minio_files": [], + "is_debug": False, # Normal mode + }, + headers=mock_auth_header + ) + + assert response.status_code == 500 + # In normal mode, generic error message should be shown + assert response.json()["detail"] == "Agent run error." + # Actual error should NOT be exposed in normal mode + assert "Test internal error" not in response.json()["detail"] + + def test_agent_stop_api_success(mocker, mock_conversation_id): """Test agent_stop_api success case.""" # Mock the authentication function to return user_id