safeMarshalJSON marshals value to JSON. On failure it returns a diagnostic error object rather than panicking, which is appropriate for debug telemetry where a marshal failure should not crash the caller.
(label string, value any)
| 745 | // for debug telemetry where a marshal failure should not crash the |
| 746 | // caller. |
| 747 | func safeMarshalJSON(label string, value any) json.RawMessage { |
| 748 | data, err := json.Marshal(value) |
| 749 | if err != nil { |
| 750 | fallback, fallbackErr := json.Marshal(map[string]string{ |
| 751 | "error": fmt.Sprintf("chatdebug: failed to marshal %s: %v", label, err), |
| 752 | }) |
| 753 | if fallbackErr == nil { |
| 754 | return append(json.RawMessage(nil), fallback...) |
| 755 | } |
| 756 | return json.RawMessage(`{"error":"chatdebug: failed to marshal value"}`) |
| 757 | } |
| 758 | return append(json.RawMessage(nil), data...) |
| 759 | } |
| 760 | |
| 761 | func appendStreamContentText( |
| 762 | content []normalizedContentPart, |
no test coverage detected