diff --git a/backend/routes/history_routes.py b/backend/routes/history_routes.py index a5079d68..63d1af38 100644 --- a/backend/routes/history_routes.py +++ b/backend/routes/history_routes.py @@ -60,7 +60,13 @@ def create_history(): } """ try: - data = request.get_json() + data = request.get_json(silent=True) + if not isinstance(data, dict): + return api_error_response( + validation_error("请求体必须是 JSON 对象", "请提供包含 topic 和 outline 的 JSON 对象。"), + context={"endpoint": "/api/history"}, + ) + topic = data.get('topic') outline = data.get('outline') task_id = data.get('task_id') diff --git a/tests/errors_test.py b/tests/errors_test.py index 86d6d6ab..3bbe1470 100644 --- a/tests/errors_test.py +++ b/tests/errors_test.py @@ -313,6 +313,20 @@ def test_outline_missing_topic_returns_structured_error(client): assert data["error_message"] +def test_create_history_null_json_returns_structured_error(client): + response = client.post( + "/api/history", + data="null", + content_type="application/json", + ) + data = response.get_json() + + assert response.status_code == 400 + assert data["success"] is False + assert data["error"]["code"] == "INVALID_REQUEST" + assert data["error"]["detail"] == "请求体必须是 JSON 对象" + + def test_sse_error_event_is_structured(): data = _normalize_sse_error( "error",