| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strconv" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | const streamPayloadExcerpt = 120 |
| 10 | |
| 11 | // StreamDecodeError reports an SSE data payload that failed to parse, quoting a |
| 12 | // bounded excerpt of it. Gateways put non-JSON on `data:` lines — HTML error |
| 13 | // pages, bare sentinels, proxy notices — and the decoder error alone names only |
| 14 | // the first offending byte, which is not enough to identify the sender. |
| 15 | func StreamDecodeError(provider string, payload string, err error) error { |
| 16 | return fmt.Errorf("%s: decode stream: %w (payload %s)", provider, err, streamPayloadSnippet(payload)) |
| 17 | } |
| 18 | |
| 19 | func streamPayloadSnippet(payload string) string { |
| 20 | payload = strings.TrimSpace(payload) |
| 21 | if payload == "" { |
| 22 | return "(empty)" |
| 23 | } |
| 24 | if len(payload) > streamPayloadExcerpt { |
| 25 | return strconv.Quote(payload[:streamPayloadExcerpt]) + "…" |
| 26 | } |
| 27 | return strconv.Quote(payload) |
| 28 | } |
| 29 |