fix: address review - fix log wording and add response body assertion in test
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -346,9 +347,15 @@ func TestGatewayService_AnthropicAPIKeyPassthrough_CountTokens404PassthroughNotE
|
|||||||
err := svc.ForwardCountTokens(context.Background(), c, account, parsed)
|
err := svc.ForwardCountTokens(context.Background(), c, account, parsed)
|
||||||
|
|
||||||
if tt.wantPassthrough {
|
if tt.wantPassthrough {
|
||||||
// 404 透传:返回 nil(不记录为错误),但 HTTP 状态码是 404
|
// 返回 nil(不记录为错误),HTTP 状态码 404 + Anthropic 错误体
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, http.StatusNotFound, rec.Code)
|
require.Equal(t, http.StatusNotFound, rec.Code)
|
||||||
|
var errResp map[string]any
|
||||||
|
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &errResp))
|
||||||
|
require.Equal(t, "error", errResp["type"])
|
||||||
|
errObj, ok := errResp["error"].(map[string]any)
|
||||||
|
require.True(t, ok)
|
||||||
|
require.Equal(t, "not_found_error", errObj["type"])
|
||||||
} else {
|
} else {
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
require.Equal(t, tt.statusCode, rec.Code)
|
require.Equal(t, tt.statusCode, rec.Code)
|
||||||
|
|||||||
@@ -6223,11 +6223,11 @@ func (s *GatewayService) forwardCountTokensAnthropicAPIKeyPassthrough(ctx contex
|
|||||||
upstreamMsg := strings.TrimSpace(extractUpstreamErrorMessage(respBody))
|
upstreamMsg := strings.TrimSpace(extractUpstreamErrorMessage(respBody))
|
||||||
upstreamMsg = sanitizeUpstreamErrorMessage(upstreamMsg)
|
upstreamMsg = sanitizeUpstreamErrorMessage(upstreamMsg)
|
||||||
|
|
||||||
// 中转站不支持 count_tokens 端点时(404),透传 404 让客户端 fallback 到本地估算。
|
// 中转站不支持 count_tokens 端点时(404),返回 404 让客户端 fallback 到本地估算。
|
||||||
// 返回 nil 避免 handler 层记录为错误,也不设置 ops 上游错误上下文。
|
// 返回 nil 避免 handler 层记录为错误,也不设置 ops 上游错误上下文。
|
||||||
if resp.StatusCode == http.StatusNotFound {
|
if resp.StatusCode == http.StatusNotFound {
|
||||||
logger.LegacyPrintf("service.gateway",
|
logger.LegacyPrintf("service.gateway",
|
||||||
"[count_tokens] Upstream does not support count_tokens (404), passing through: account=%d name=%s msg=%s",
|
"[count_tokens] Upstream does not support count_tokens (404), returning 404: account=%d name=%s msg=%s",
|
||||||
account.ID, account.Name, truncateString(upstreamMsg, 512))
|
account.ID, account.Name, truncateString(upstreamMsg, 512))
|
||||||
s.countTokensError(c, http.StatusNotFound, "not_found_error", "count_tokens endpoint is not supported by upstream")
|
s.countTokensError(c, http.StatusNotFound, "not_found_error", "count_tokens endpoint is not supported by upstream")
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user