mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-11-18 15:48:39 +08:00
Normalize string websocket errors without discarding their messages. Recognize untyped and body-wrapped failures across SSE, websocket, and duplex paths while retaining status, headers, and sequence numbers. Cover transport and format combinations, replay invalidation, credential status precedence, and Responses framing with regression tests.
31 lines
965 B
Go
31 lines
965 B
Go
package helps
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/MoeForge/moego/ojson"
|
|
)
|
|
|
|
// CodexResponseEventType recognizes error envelopes that omit the event type.
|
|
// Explicit event types remain authoritative so output and metadata cannot be
|
|
// mistaken for a terminal failure merely because they contain an error field.
|
|
func CodexResponseEventType(payload []byte) string {
|
|
if eventType := strings.TrimSpace(ojson.GetBytesNoCopy(payload, "type").String()); eventType != "" {
|
|
return eventType
|
|
}
|
|
if !ojson.ValidBytes(payload) {
|
|
return ""
|
|
}
|
|
for _, path := range []string{"error", "body.error", "response.error"} {
|
|
node := ojson.GetBytesNoCopy(payload, path)
|
|
if node.IsObject() || node.Type == ojson.String && strings.TrimSpace(node.String()) != "" {
|
|
return "error"
|
|
}
|
|
}
|
|
if strings.TrimSpace(ojson.GetBytesNoCopy(payload, "code").String()) != "" &&
|
|
strings.TrimSpace(ojson.GetBytesNoCopy(payload, "message").String()) != "" {
|
|
return "error"
|
|
}
|
|
return ""
|
|
}
|