@@ -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