Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: JsonArg uses ShouldBindJSON and returns more diagnosable errors (#17413)

Jeffrey Chen29092a385f57
diff --git a/kernel/util/net.go b/kernel/util/net.go
index d7841bcc45..b7e054a34f 100644
--- a/kernel/util/net.go
+++ b/kernel/util/net.go
@@ -19,6 +19,7 @@ package util
import (
"errors"
"fmt"
+ "io"
"net"
"net/http"
"net/url"
@@ -203,9 +204,15 @@ func GetRemoteAddr(req *http.Request) string {
func JsonArg(c *gin.Context, result *gulu.Result) (arg map[string]any, ok bool) {
arg = map[string]any{}
- if err := c.BindJSON(&arg); err != nil {
+ if err := c.ShouldBindJSON(&arg); err != nil {
result.Code = -1
- result.Msg = "parses request failed"
+ var detail string
+ if errors.Is(err, io.EOF) {
+ detail = "the request body is empty or truncated (EOF)"
+ } else {
+ detail = err.Error()
+ }
+ result.Msg = fmt.Sprintf("Parses request [%s] failed: %s", c.Request.URL.Path, detail)
return
}