Skip to content

Commit a941812

Browse files
committed
log: CC API error 记录上游响应体,不再只有 status
排查 hk_sji 的 400 时发现:日志只有 `CC API error {"status":400}`,没有任何 body, 导致根因只能靠推断(那次最终是靠对比提交历史才定位到 reasoning_content 未回传)。 三处非 2xx 分支本来就拿到了 errorText(用于 mapCcError),只是没写进日志。现在一并记录: - handleChatCompletions - handleMessages(Anthropic 端点) - handleResponses 新增 summarizeUpstreamError(): - 截断 500 字符,避免异常大的响应体刷爆日志 - 压掉换行,保证一条日志一行(便于 grep) - 超出部分标注省略长度 验证(mock 上游返回真实的 400 insufficient credits 样本): - 三个端点均记录完整 body,含 `insufficient credits` 关键词 - 5000 字符的超长 body 被截断到 515(含省略标记) - 既有端点回归全绿(流式逐字节一致、非流式字段一致、背压正常)
1 parent feafd6d commit a941812

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

‎proxy.mjs‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,15 @@ function log(level, msg, data) {
206206
}
207207
}
208208

209+
// 把上游错误体摘要成单行,便于日志排查。
210+
// 之前 CC API error 只记 status,不记 body —— 遇到 400 只能靠猜(问题来源见 hk_sji 排查)。
211+
// 截断到 500 字符,避免异常大的 body 刷爆日志;同时压掉换行,保证一条日志一行。
212+
function summarizeUpstreamError(text, limit = 500) {
213+
if (!text) return '';
214+
const flat = String(text).replace(/\s+/g, ' ').trim();
215+
return flat.length > limit ? flat.slice(0, limit) + '…(' + (flat.length - limit) + ' more)' : flat;
216+
}
217+
209218
// ── 会话管理 ───────────────────────────────────────
210219
// 每个 API Key 独立一个 session,12h 过期 + 1h 随机抖动
211220
// 同一 Key 在同一周期内复用,到期自动换新
@@ -1027,7 +1036,7 @@ async function handleChatCompletions(req, res) {
10271036

10281037
if (!ccResponse.ok) {
10291038
const errorText = await ccResponse.text().catch(() => '');
1030-
log('error', 'CC API error', { status: ccResponse.status });
1039+
log('error', 'CC API error', { status: ccResponse.status, body: summarizeUpstreamError(errorText) });
10311040
const mapped = mapCcError(ccResponse.status, errorText);
10321041
sendJSON(res, mapped.status, mapped.body);
10331042
return;
@@ -1833,7 +1842,7 @@ async function handleMessages(req, res) {
18331842

18341843
if (!ccResponse.ok) {
18351844
const errorText = await ccResponse.text().catch(() => '');
1836-
log('error', 'CC API error (Anthropic)', { status: ccResponse.status });
1845+
log('error', 'CC API error (Anthropic)', { status: ccResponse.status, body: summarizeUpstreamError(errorText) });
18371846
const mapped = mapCcError(ccResponse.status, errorText);
18381847
sendAnthropicError(res, mapped.status, mapped.body.error.type, mapped.body.error.message);
18391848
return;
@@ -2621,7 +2630,7 @@ async function handleResponses(req, res) {
26212630

26222631
if (!ccResponse.ok) {
26232632
const errorText = await ccResponse.text().catch(() => '');
2624-
log('error', 'CC API error', { status: ccResponse.status, path: '/v1/responses' });
2633+
log('error', 'CC API error', { status: ccResponse.status, path: '/v1/responses', body: summarizeUpstreamError(errorText) });
26252634
const mapped = mapCcError(ccResponse.status, errorText);
26262635
sendResponsesError(res, mapped.status, mapped.body.error.type, mapped.body.error.message, mapped.body.retry_after);
26272636
return;

0 commit comments

Comments
 (0)