Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(api): keep the datasets API error response body shape (#21831)

陈志谦306541333ba9
diff --git a/internal/handler/dataset.go b/internal/handler/dataset.go
index 23c7f32275..3cebce9954 100644
--- a/internal/handler/dataset.go
+++ b/internal/handler/dataset.go
@@ -264,7 +264,7 @@ func (h *DatasetsHandler) CreateDataset(c *gin.Context) {
}
if _, exists := raw["ext"]; exists {
err = fmt.Errorf("extra inputs are not permitted: ext")
- common.ErrorWithCode(c, common.CodeArgumentError, err.Error())
+ common.ResponseWithCodeData(c, common.CodeArgumentError, nil, err.Error())
operationLog.Code = int(common.CodeArgumentError)
operationLog.Error = err
return
@@ -272,7 +272,7 @@ func (h *DatasetsHandler) CreateDataset(c *gin.Context) {
for field := range raw {
if !createDatasetAllowedFields[field] {
err = fmt.Errorf("extra inputs are not permitted: %s", field)
- common.ErrorWithCode(c, common.CodeArgumentError, err.Error())
+ common.ResponseWithCodeData(c, common.CodeArgumentError, nil, err.Error())
operationLog.Code = int(common.CodeArgumentError)
operationLog.Error = err
return
@@ -281,7 +281,7 @@ func (h *DatasetsHandler) CreateDataset(c *gin.Context) {
var req service.CreateDatasetRequest
if err = json.Unmarshal(bodyBytes, &req); err != nil {
- common.ErrorWithCode(c, common.CodeDataError, err.Error())
+ common.ResponseWithCodeData(c, common.CodeDataError, nil, err.Error())
operationLog.Code = int(common.CodeDataError)
operationLog.Error = err
return
@@ -292,7 +292,7 @@ func (h *DatasetsHandler) CreateDataset(c *gin.Context) {
if req.Name == "" || (len(bodyBytes) > 0 && jsonNullValue(bodyBytes, "name")) {
err = fmt.Errorf("field validation for 'name' failed on the 'required' tag")
- common.ErrorWithCode(c, common.CodeDataError, err.Error())
+ common.ResponseWithCodeData(c, common.CodeDataError, nil, err.Error())
operationLog.Code = int(common.CodeDataError)
operationLog.Error = err
return
@@ -506,7 +506,7 @@ func (h *DatasetsHandler) UpdateDataset(c *gin.Context) {
bodyBytes, _, err := parseJSONRequestObject(c)
if err != nil {
- common.ErrorWithCode(c, common.CodeArgumentError, err.Error())
+ // parseJSONRequestObject already wrote the error response.
operationLog.Code = int(common.CodeArgumentError)
operationLog.Error = err
return
@@ -557,7 +557,7 @@ func (h *DatasetsHandler) UpdateDataset(c *gin.Context) {
return
}
if code != common.CodeSuccess {
- common.ErrorWithCode(c, code, "dataset updated failed")
+ common.ResponseWithCodeData(c, code, nil, "dataset updated failed")
operationLog.Code = int(code)
operationLog.Error = errors.New("dataset updated failed")
return
@@ -790,7 +790,7 @@ func (h *DatasetsHandler) DeleteDatasets(c *gin.Context) {
bodyBytes, rawPayload, err := parseJSONRequestObject(c)
if err != nil {
- common.ErrorWithCode(c, common.CodeArgumentError, err.Error())
+ // parseJSONRequestObject already wrote the error response.
operationLog.Code = int(common.CodeArgumentError)
operationLog.Error = err
return
@@ -800,7 +800,7 @@ func (h *DatasetsHandler) DeleteDatasets(c *gin.Context) {
for field := range rawPayload {
if field != "ids" && field != "delete_all" {
err = fmt.Errorf("extra inputs are not permitted: %s", field)
- common.ErrorWithCode(c, common.CodeArgumentError, err.Error())
+ common.ResponseWithCodeData(c, common.CodeArgumentError, nil, err.Error())
operationLog.Code = int(common.CodeArgumentError)
operationLog.Error = err
return
@@ -812,7 +812,7 @@ func (h *DatasetsHandler) DeleteDatasets(c *gin.Context) {
DeleteAll bool `json:"delete_all,omitempty"`
}
if err = json.Unmarshal(bodyBytes, &req); err != nil {
- common.ErrorWithCode(c, common.CodeDataError, err.Error())
+ common.ResponseWithCodeData(c, common.CodeDataError, nil, err.Error())
operationLog.Code = int(common.CodeDataError)
operationLog.Error = err
return
@@ -830,7 +830,7 @@ func (h *DatasetsHandler) DeleteDatasets(c *gin.Context) {
}
if len(duplicates) > 0 {
err = fmt.Errorf("duplicate ids: '%s'", strings.Join(duplicates, ", "))
- common.ErrorWithCode(c, common.CodeArgumentError, err.Error())
+ common.ResponseWithCodeData(c, common.CodeArgumentError, nil, err.Error())
operationLog.Code = int(common.CodeArgumentError)
operationLog.Error = err
return
diff --git a/internal/handler/dataset_error_response_test.go b/internal/handler/dataset_error_response_test.go
new file mode 100644
index 0000000000..e0fdde94db
--- /dev/null
+++ b/internal/handler/dataset_error_response_test.go
@@ -0,0 +1,193 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+package handler
+
+import (
+ "bytes"
+ "encoding/json"
+ "net/http"
+ "net/http/httptest"
+ "testing"
+
+ "github.com/gin-gonic/gin"
+
+ "ragflow/internal/common"
+ "ragflow/internal/entity"
+)
+
+// The datasets API mirrors the Python contract, whose error bodies always
+// carry the three keys `code`, `data` and `message` (api/utils/api_utils.py
+// get_json_result). These tests pin the `data` key on CreateDataset error
+// branches and keep UpdateDataset from writing a second response after
+// parseJSONRequestObject already answered.
+
+func newDatasetErrorResponseRouter(method, path string, handlerFn func(*gin.Context)) *gin.Engine {
+ gin.SetMode(gin.TestMode)
+ r := gin.New()
+ r.Handle(method, path, func(c *gin.Context) {
+ c.Set("user", &entity.User{ID: "user-1"})
+ handlerFn(c)
+ })
+ return r
+}
+
+func assertDatasetErrorBodyHasDataKey(t *testing.T, r *gin.Engine, method, target, body string, wantCode common.ErrorCode) map[string]interface{} {
+ t.Helper()
+
+ resp := httptest.NewRecorder()
+ req := httptest.NewRequest(method, target, bytes.NewBufferString(body))
+ req.Header.Set("Content-Type", "application/json")
+ r.ServeHTTP(resp, req)
+
+ if resp.Code != http.StatusOK {
+ t.Fatalf("status=%d body=%s", resp.Code, resp.Body.String())
+ }
+
+ var parsed map[string]interface{}
+ if err := json.Unmarshal(resp.Body.Bytes(), &parsed); err != nil {
+ t.Fatalf("response body is not one JSON object: %v body=%s", err, resp.Body.String())
+ }
+ if gotCode, ok := parsed["code"].(float64); !ok || int(gotCode) != int(wantCode) {
+ t.Fatalf("code=%v want=%d body=%s", parsed["code"], int(wantCode), resp.Body.String())
+ }
+ if _, ok := parsed["data"]; !ok {
+ t.Fatalf("error body lost the `data` key (Python get_json_result always sends it): body=%s", resp.Body.String())
+ }
+ if _, ok := parsed["message"].(string); !ok {
+ t.Fatalf("error body lost the `message` key: body=%s", resp.Body.String())
+ }
+ return parsed
+}
+
+func TestCreateDatasetExtraFieldKeepsDataInBody(t *testing.T) {
+ r := newDatasetErrorResponseRouter(http.MethodPost, "/api/v1/datasets", func(c *gin.Context) {
+ h := &DatasetsHandler{}
+ h.CreateDataset(c)
+ })
+
+ body := assertDatasetErrorBodyHasDataKey(t, r, http.MethodPost, "/api/v1/datasets",
+ `{"name":"kb","ext":{}}`, common.CodeArgumentError)
+ if body["message"] != "extra inputs are not permitted: ext" {
+ t.Fatalf("message=%v", body["message"])
+ }
+}
+
+func TestCreateDatasetUnknownFieldKeepsDataInBody(t *testing.T) {
+ r := newDatasetErrorResponseRouter(http.MethodPost, "/api/v1/datasets", func(c *gin.Context) {
+ h := &DatasetsHandler{}
+ h.CreateDataset(c)
+ })
+
+ body := assertDatasetErrorBodyHasDataKey(t, r, http.MethodPost, "/api/v1/datasets",
+ `{"name":"kb","bogus_field":1}`, common.CodeArgumentError)
+ if body["message"] != "extra inputs are not permitted: bogus_field" {
+ t.Fatalf("message=%v", body["message"])
+ }
+}
+
+func TestCreateDatasetMalformedJSONKeepsDataInBody(t *testing.T) {
+ r := newDatasetErrorResponseRouter(http.MethodPost, "/api/v1/datasets", func(c *gin.Context) {
+ h := &DatasetsHandler{}
+ h.CreateDataset(c)
+ })
+
+ assertDatasetErrorBodyHasDataKey(t, r, http.MethodPost, "/api/v1/datasets",
+ `{"name":`, common.CodeArgumentError)
+}
+
+func TestCreateDatasetMissingNameKeepsDataInBody(t *testing.T) {
+ r := newDatasetErrorResponseRouter(http.MethodPost, "/api/v1/datasets", func(c *gin.Context) {
+ h := &DatasetsHandler{}
+ h.CreateDataset(c)
+ })
+
+ assertDatasetErrorBodyHasDataKey(t, r, http.MethodPost, "/api/v1/datasets",
+ `{"parser_config":{}}`, common.CodeDataError)
+}
+
+func TestUpdateDatasetRespondsExactlyOnceOnMalformedRequest(t *testing.T) {
+ r := newDatasetErrorResponseRouter(http.MethodPut, "/api/v1/datasets/:dataset_id", func(c *gin.Context) {
+ h := &DatasetsHandler{}
+ h.UpdateDataset(c)
+ })
+
+ // A wrong content type fails inside parseJSONRequestObject, which already
+ // writes the error response. The handler must not write a second one.
+ resp := httptest.NewRecorder()
+ req := httptest.NewRequest(http.MethodPut, "/api/v1/datasets/123e4567-e89b-12d3-a456-426614174000", bytes.NewBufferString(`{"name":"kb"}`))
+ req.Header.Set("Content-Type", "text/plain")
+ r.ServeHTTP(resp, req)
+
+ var parsed map[string]interface{}
+ if err := json.Unmarshal(resp.Body.Bytes(), &parsed); err != nil {
+ t.Fatalf("response body is not one JSON object (a second response was appended): %v body=%s", err, resp.Body.String())
+ }
+ if gotCode, ok := parsed["code"].(float64); !ok || int(gotCode) != int(common.CodeArgumentError) {
+ t.Fatalf("code=%v want=%d body=%s", parsed["code"], int(common.CodeArgumentError), resp.Body.String())
+ }
+ if _, ok := parsed["data"]; !ok {
+ t.Fatalf("error body lost the `data` key: body=%s", resp.Body.String())
+ }
+}
+
+func TestDeleteDatasetsExtraFieldKeepsDataInBody(t *testing.T) {
+ r := newDatasetErrorResponseRouter(http.MethodDelete, "/api/v1/datasets", func(c *gin.Context) {
+ h := &DatasetsHandler{}
+ h.DeleteDatasets(c)
+ })
+
+ body := assertDatasetErrorBodyHasDataKey(t, r, http.MethodDelete, "/api/v1/datasets",
+ `{"ids":["a"],"bogus":1}`, common.CodeArgumentError)
+ if body["message"] != "extra inputs are not permitted: bogus" {
+ t.Fatalf("message=%v", body["message"])
+ }
+}
+
+func TestDeleteDatasetsDuplicateIDsKeepsDataInBody(t *testing.T) {
+ r := newDatasetErrorResponseRouter(http.MethodDelete, "/api/v1/datasets", func(c *gin.Context) {
+ h := &DatasetsHandler{}
+ h.DeleteDatasets(c)
+ })
+
+ body := assertDatasetErrorBodyHasDataKey(t, r, http.MethodDelete, "/api/v1/datasets",
+ `{"ids":["a","a"]}`, common.CodeArgumentError)
+ if body["message"] != "duplicate ids: 'a'" {
+ t.Fatalf("message=%v", body["message"])
+ }
+}
+
+func TestDeleteDatasetsRespondsExactlyOnceOnMalformedRequest(t *testing.T) {
+ r := newDatasetErrorResponseRouter(http.MethodDelete, "/api/v1/datasets", func(c *gin.Context) {
+ h := &DatasetsHandler{}
+ h.DeleteDatasets(c)
+ })
+
+ // A wrong content type fails inside parseJSONRequestObject, which already
+ // writes the error response. The handler must not write a second one.
+ resp := httptest.NewRecorder()
+ req := httptest.NewRequest(http.MethodDelete, "/api/v1/datasets", bytes.NewBufferString(`{"ids":["a"]}`))
+ req.Header.Set("Content-Type", "text/plain")
+ r.ServeHTTP(resp, req)
+
+ var parsed map[string]interface{}
+ if err := json.Unmarshal(resp.Body.Bytes(), &parsed); err != nil {
+ t.Fatalf("response body is not one JSON object (a second response was appended): %v body=%s", err, resp.Body.String())
+ }
+ if _, ok := parsed["data"]; !ok {
+ t.Fatalf("error body lost the `data` key: body=%s", resp.Body.String())
+ }
+}