error.code 与 error.type 一样是**自由文本面**:主流上游里是枚举,但网关、代理与 兼容层可以往里塞任何东西(包括回显的用户原文)。M41 已给 type 加了守卫, providerUpstreamError 的 code 一直原样带出,是这一族里唯一还漏的字段。 守卫本色从 runtime/model(safeErrorToken)与 runtime/multimodal/voice (dashscopeErrorToken)两份逐字同形的实现上提到 trace.ErrorToken,model 侧改为 调用它(voice 侧仍是自己那份,不在本切片文件面内,见随附 finding)。 唯一的行为变化就是「不像令牌的 code 变空串」:status / type / body 体积与 sha256、 错误文案模板、双 %w 哨兵链(ErrUpstream、trace.ErrUpstream)都不变。 验收:go build ./... + go vet ./... + trace/model 包 test 全绿;新增用例覆盖 「带空格标点的 code 变空串且 status/type 仍在」与「令牌形状的 code 保留」。
95 lines
3.8 KiB
Go
95 lines
3.8 KiB
Go
package model
|
|
|
|
// 本文件钉住 providerUpstreamError 的两条出镜边界:
|
|
//
|
|
// - **error.code 是自由文本面**:它在主流上游是枚举,但网关、代理与兼容层可以往
|
|
// 里面塞任何东西(包括回显的输入)。只有像令牌才出镜,其余变空串 —— 而 status /
|
|
// type / 体积摘要这些诊断面不受影响。
|
|
// - **响应体正文一个字节都不进错误**:这是 providerUpstreamError 存在的理由
|
|
// (旧实现把体截到 512B 拼进 error,而 `chat run` / `loop step` 会把它落日志)。
|
|
//
|
|
// 每条用例都是「把它放宽就变红」的形状:断言的是错误的文本,也就是真正进日志的字符串。
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/trace"
|
|
)
|
|
|
|
// upstreamErrResp 造一条非 2xx 响应:只带状态码、Content-Type 与体,够
|
|
// providerUpstreamError 走完整条收敛路径。
|
|
func upstreamErrResp(status int, body string) *http.Response {
|
|
return &http.Response{
|
|
StatusCode: status,
|
|
Header: http.Header{"Content-Type": []string{"application/json; charset=utf-8"}},
|
|
Body: io.NopCloser(strings.NewReader(body)),
|
|
}
|
|
}
|
|
|
|
// TestProviderUpstreamErrorDropsFreeTextCode code 里是回显的用户原文 ⇒ 该字段整体消失
|
|
// (连 `code=` 这个键都不出现),而 status / type 仍在。去掉令牌守卫就变红。
|
|
func TestProviderUpstreamErrorDropsFreeTextCode(t *testing.T) {
|
|
t.Parallel()
|
|
const echoed = "Invalid value: '她刚才说的那句话'"
|
|
body := `{"error":{"code":"` + echoed + `","type":"invalid_request_error"}}`
|
|
|
|
err := providerUpstreamError("openai", "https://api.example.com/v1/chat/completions", upstreamErrResp(http.StatusBadRequest, body))
|
|
if err == nil {
|
|
t.Fatal("非 2xx 却报成功")
|
|
}
|
|
msg := err.Error()
|
|
if strings.Contains(msg, echoed) {
|
|
t.Fatalf("code 里的用户原文进了错误(会随 chat run 落日志):%v", msg)
|
|
}
|
|
if strings.Contains(msg, "code=") {
|
|
t.Fatalf("非令牌形状的 code 没有被丢弃:%v", msg)
|
|
}
|
|
// 诊断面不能一起丢:状态、协议枚举的 type、体积摘要都还在。
|
|
for _, want := range []string{"status=400", "type=invalid_request_error", "body_bytes=", "api.example.com"} {
|
|
if !strings.Contains(msg, want) {
|
|
t.Fatalf("错误里缺少 %q(诊断面不能一起丢):%v", want, msg)
|
|
}
|
|
}
|
|
if !errors.Is(err, ErrUpstream) || !errors.Is(err, trace.ErrUpstream) {
|
|
t.Fatalf("降级链的哨兵断了:%v", err)
|
|
}
|
|
}
|
|
|
|
// TestProviderUpstreamErrorKeepsTokenShapedCode 真实枚举原样出镜:丢掉它只是少一条
|
|
// 分类提示,但既然它像令牌就该留下。
|
|
func TestProviderUpstreamErrorKeepsTokenShapedCode(t *testing.T) {
|
|
t.Parallel()
|
|
body := `{"error":{"code":"Arrearage","type":"invalid_request_error"}}`
|
|
|
|
err := providerUpstreamError("deepseek", "https://api.deepseek.com/v1/chat/completions",
|
|
upstreamErrResp(http.StatusPaymentRequired, body))
|
|
if err == nil {
|
|
t.Fatal("非 2xx 却报成功")
|
|
}
|
|
if msg := err.Error(); !strings.Contains(msg, "code=Arrearage") {
|
|
t.Fatalf("令牌形状的 code 被误杀:%v", msg)
|
|
}
|
|
}
|
|
|
|
// TestProviderUpstreamErrorDropsBodyText 体里除了 code/type 之外的字节一个都不进错误:
|
|
// 上游把用户的提问回显在 error.message 里是真实写法。
|
|
func TestProviderUpstreamErrorDropsBodyText(t *testing.T) {
|
|
t.Parallel()
|
|
const prompt = "她的原始提问:帮我看看这段代码"
|
|
body := `{"error":{"code":"invalid_request_error","type":"invalid_request_error",` +
|
|
`"message":"Invalid value: '` + prompt + `'"}}`
|
|
|
|
err := providerUpstreamError("openai", "https://api.example.com/v1/chat/completions",
|
|
upstreamErrResp(http.StatusBadRequest, body))
|
|
if err == nil {
|
|
t.Fatal("非 2xx 却报成功")
|
|
}
|
|
if strings.Contains(err.Error(), prompt) {
|
|
t.Fatalf("error.message 的回显进了错误:%v", err)
|
|
}
|
|
}
|