Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion backend/apps/agent_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
50 changes: 50 additions & 0 deletions test/backend/app/test_agent_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,56 @@
assert "data: chunk2" in content


async def test_agent_run_api_error_debug_mode(mocker, mock_auth_header):

Check warning on line 203 in test/backend/app/test_agent_app.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use asynchronous features in this function or remove the `async` keyword.

See more on https://sonarcloud.io/project/issues?id=ModelEngine-Group_nexent&issues=AZ4go0stOMFtWHqP9ymR&open=AZ4go0stOMFtWHqP9ymR&pullRequest=2977
"""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):

Check warning on line 227 in test/backend/app/test_agent_app.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use asynchronous features in this function or remove the `async` keyword.

See more on https://sonarcloud.io/project/issues?id=ModelEngine-Group_nexent&issues=AZ4go0stOMFtWHqP9ymS&open=AZ4go0stOMFtWHqP9ymS&pullRequest=2977
"""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
Expand Down
Loading