fix: improve error handling and nil consistency in FormatArgsJSON

- Use fmt.Sprintf fallback instead of {} on encoding errors
- Normalize nil args to {} in FormatArgsJSON for consistent output
- Update tests to expect {} instead of null for nil args

Based on PR #2670 review feedback from afjcjsbx
This commit is contained in:
David Siewert
2026-04-27 16:05:10 +06:00
parent 97b1c3efec
commit 8dca2a1319
4 changed files with 9 additions and 7 deletions
+7 -1
View File
@@ -10,6 +10,11 @@ import (
const ToolFeedbackContinuationHint = "Continuing the current task."
func FormatArgsJSON(args map[string]any, prettyPrint, disableEscapeHTML bool) string {
// Normalize nil to empty map for consistent output
if args == nil {
args = map[string]any{}
}
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
if prettyPrint {
@@ -19,7 +24,8 @@ func FormatArgsJSON(args map[string]any, prettyPrint, disableEscapeHTML bool) st
enc.SetEscapeHTML(false)
}
if err := enc.Encode(args); err != nil {
return "{}"
// Fallback to fmt.Sprintf to preserve visibility of problematic args
return fmt.Sprintf("%v", args)
}
return strings.TrimSpace(buf.String())
}
+1 -1
View File
@@ -143,7 +143,7 @@ func TestFormatArgsJSON_EscapeHTMLByDefault(t *testing.T) {
func TestFormatArgsJSON_NilArgs(t *testing.T) {
got := FormatArgsJSON(nil, false, false)
want := `null`
want := `{}`
if got != want {
t.Fatalf("FormatArgsJSON() nil = %q, want %q", got, want)
}