Skip to content

修复流式错误中字符串 code 导致的崩溃 / Fix stream error crash on string code #161

@saxon134

Description

@saxon134

问题 / Problem

中文:
仓库证据:docs/design.md 说明流式错误帧应被转换为 GlobalRouterError;src/globalrouter/_streaming.py 在检测到 SSE payload 含 error 时会调用 error_from_stream_payload;但 src/globalrouter/_errors.py:69 对 error.get("code") 直接执行 int(...)。普通非流式错误路径会把 error.code 当字符串保留,且设计文档中的 native error envelope 也包含 error.code。若流式接口返回 {"error":{"code":"ROUTER_RATE_LIMITED","message":"..."}} 这类字符串 code,SDK 会抛出 ValueError,而不是可捕获的 GlobalRouterError。
影响:调用方无法按文档捕获 GlobalRouterError,request_id、error_type 等诊断信息也会丢失,降低流式任务和聊天的可靠性与信任度。
建议方向:让 error_from_stream_payload 与 error_from_response 的 code 处理保持一致:字符串 code 应进入 GlobalRouterError.code;status_code 只在 code 是数字时解析,否则使用 0 或响应不可用的默认值。
验证方式:新增 sync/async SSE 测试,构造带字符串 error.code 和 metadata 的错误帧,断言抛出 GlobalRouterError 且 code、message、error_type、request_id 正确。
价值:修复后用户能稳定处理流式失败,maintainer 也能获得一致的错误契约,减少误诊和异常崩溃。

English:
Repository evidence: docs/design.md says stream error frames are raised as GlobalRouterError; src/globalrouter/_streaming.py routes SSE error payloads into error_from_stream_payload; src/globalrouter/_errors.py:69 calls int(...) on error.get("code"). The non-streaming path preserves error.code as a string, and the documented native envelope includes error.code. A stream payload such as {"error":{"code":"ROUTER_RATE_LIMITED","message":"..."}} therefore raises ValueError before the SDK can raise GlobalRouterError.
Impact: callers cannot catch the documented exception type, and diagnostic fields such as request_id and error_type are lost, reducing reliability and trust for streaming chat/tasks.
Suggested fix: align error_from_stream_payload with error_from_response: keep string codes in GlobalRouterError.code, and parse status_code only when the value is numeric, otherwise default to 0 when no HTTP response is available.
Verification: add sync and async SSE tests with a string error.code plus metadata, and assert GlobalRouterError fields are preserved.
Value: users get a stable stream failure contract, and maintainers avoid raw exception crashes that obscure the real API error.

价值 / Value

修复该问题可以提升正确性、可靠性和用户信任,降低 string error code crash in error_from_stream_payload 带来的排障与运维成本。

Fixing this issue improves correctness, reliability, and user trust while reducing debugging and operational cost caused by string error code crash in error_from_stream_payload.

证据 / Evidence

File: src/globalrouter/_errors.py

Line: 69

Severity / 严重级别: high

Summary / 摘要: string error code crash in error_from_stream_payload

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions