@@ -64,7 +64,8 @@ export function formatError(error: unknown, withStack = false): string {
6464 const e = error as any ;
6565 if ( ! e ) return String ( error ) ;
6666
67- const status = e . status ?? e . response ?. status ?? e . cause ?. status ;
67+ const status = e . status ?? e . status_code ?? e . response ?. status ?? e . cause ?. status ?? e . cause ?. status_code ;
68+ const statusText = firstText ( e . statusText , e . response ?. statusText , e . cause ?. statusText ) ;
6869 const name = e . name && e . name !== 'Error' ? e . name : undefined ;
6970
7071 // 候选错误体来源(按优先级)
@@ -73,6 +74,8 @@ export function formatError(error: unknown, withStack = false): string {
7374 e . response ?. data , // axios
7475 e . response ?. body , // 部分封装
7576 e . body , // fetch 裸响应体
77+ e . errorDetails , // Gemini SDK
78+ e . details , // 常见 provider 详情字段
7679 e . cause ?. error ,
7780 e . cause ?. response ?. data ,
7881 ] ;
@@ -86,12 +89,32 @@ export function formatError(error: unknown, withStack = false): string {
8689 // SDK 的 error.message 有时已含详情;只有当它不只是无信息量文本(裸状态码 / "no body" 等)时才采用
8790 const rawMsg = typeof e . message === 'string' ? e . message . trim ( ) : '' ;
8891 const msg = rawMsg && ! isUninformative ( rawMsg ) ? rawMsg : undefined ;
92+ const code = firstText ( e . code , e . error ?. code , e . response ?. data ?. code , e . cause ?. code ) ;
93+ const type = firstText ( e . type , e . error ?. type , e . response ?. data ?. type , e . cause ?. type ) ;
94+ const param = firstText ( e . param , e . error ?. param , e . response ?. data ?. param ) ;
95+ const requestId = firstText (
96+ e . requestID ,
97+ e . request_id ,
98+ e . error ?. requestID ,
99+ e . error ?. request_id ,
100+ getHeader ( e . headers , 'x-request-id' ) ,
101+ getHeader ( e . headers , 'request-id' ) ,
102+ getHeader ( e . response ?. headers , 'x-request-id' ) ,
103+ getHeader ( e . response ?. headers , 'request-id' ) ,
104+ ) ;
105+ const headersText = describeHeaders ( e . headers ?? e . response ?. headers ) ;
89106
90107 const parts = [
91108 name ,
92109 status ? `status=${ status } ` : undefined ,
110+ statusText ? `statusText=${ statusText } ` : undefined ,
93111 msg ,
94112 bodyText ? `body=${ bodyText } ` : undefined ,
113+ code ? `code=${ code } ` : undefined ,
114+ type ? `type=${ type } ` : undefined ,
115+ param ? `param=${ param } ` : undefined ,
116+ requestId ? `requestId=${ requestId } ` : undefined ,
117+ headersText ? `headers=${ headersText } ` : undefined ,
95118 ] . filter ( Boolean ) ;
96119
97120 let text = parts . length ? parts . join ( ' ' ) : String ( error ) ;
@@ -117,6 +140,8 @@ function isUninformative(text: string): boolean {
117140 const t = text . trim ( ) . toLowerCase ( ) ;
118141 if ( ! t ) return true ;
119142 if ( / ^ \d + $ / . test ( t ) ) return true ; // 纯状态码,如 "400"
143+ if ( / ^ (?: \d + \s + ) ? s t a t u s c o d e \s * \( n o b o d y \) $ / . test ( t ) ) return true ;
144+ if ( / ^ \( n o s t a t u s c o d e o r b o d y \) $ / . test ( t ) ) return true ;
120145 return / ^ ( n o b o d y | n o r e s p o n s e | n o c o n t e n t | b a d r e q u e s t | e r r o r | f a i l e d | f a i l u r e | n o n e | n u l l | u n d e f i n e d | \[ o b j e c t o b j e c t \] | \{ \} ) $ / . test ( t ) ;
121146}
122147
@@ -127,16 +152,63 @@ function describeErrorBody(body: any, full = false): string | undefined {
127152 if ( typeof body === 'string' ) {
128153 text = body . trim ( ) || undefined ;
129154 } else if ( typeof body === 'object' ) {
130- // 常见形状 :{ error: { message, type } } 或 { message } 或 { error: "..." }
155+ // 常见及兼容接口形状 :{ error: { message, type } } / { message } / { msg } / { detail }
131156 const inner = body . error ?? body ;
132- const msg = typeof inner === 'string' ? inner : inner ?. message ;
157+ const msg = typeof inner === 'string'
158+ ? inner
159+ : firstText ( inner ?. message , inner ?. msg , inner ?. detail , inner ?. error_description , inner ?. data ?. message ) ;
133160 const type = typeof inner === 'object' ? inner ?. type : undefined ;
134- text = [ type , msg ] . filter ( Boolean ) . join ( ': ' ) || JSON . stringify ( body ) ;
161+ const code = typeof inner === 'object' ? inner ?. code : undefined ;
162+ // 日志场景保留完整 provider JSON;用户展示场景只取关键摘要,避免把大响应落进消息历史。
163+ text = full
164+ ? safeStringify ( body )
165+ : [ type , code , msg ] . filter ( Boolean ) . join ( ': ' ) || safeStringify ( body ) ;
135166 }
136167 if ( ! text || isUninformative ( text ) ) return undefined ;
137168 return full ? text : truncate ( text , 800 ) ;
138169}
139170
171+ function firstText ( ...values : any [ ] ) : string | undefined {
172+ for ( const value of values ) {
173+ if ( value == null || value === '' ) continue ;
174+ if ( typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' ) return String ( value ) ;
175+ }
176+ return undefined ;
177+ }
178+
179+ function getHeader ( headers : any , name : string ) : string | undefined {
180+ if ( ! headers ) return undefined ;
181+ try {
182+ if ( typeof headers . get === 'function' ) return headers . get ( name ) ?? undefined ;
183+ const value = headers [ name ] ?? headers [ name . toLowerCase ( ) ] ;
184+ return Array . isArray ( value ) ? value . join ( ', ' ) : firstText ( value ) ;
185+ } catch {
186+ return undefined ;
187+ }
188+ }
189+
190+ /** 响应头常含 request id / 网关诊断信息;排除 cookie 和认证字段后写入日志。 */
191+ function describeHeaders ( headers : any ) : string | undefined {
192+ if ( ! headers ) return undefined ;
193+ try {
194+ const entries : [ string , string ] [ ] = typeof headers . entries === 'function'
195+ ? [ ...headers . entries ( ) ]
196+ : Object . entries ( headers ) . map ( ( [ key , value ] ) => [ key , Array . isArray ( value ) ? value . join ( ', ' ) : String ( value ) ] ) ;
197+ const safeEntries = entries . filter ( ( [ key ] ) => ! / ^ ( s e t - c o o k i e | c o o k i e | a u t h o r i z a t i o n | p r o x y - a u t h o r i z a t i o n ) $ / i. test ( key ) ) ;
198+ return safeEntries . length ? safeStringify ( Object . fromEntries ( safeEntries ) ) : undefined ;
199+ } catch {
200+ return undefined ;
201+ }
202+ }
203+
204+ function safeStringify ( value : unknown ) : string {
205+ try {
206+ return JSON . stringify ( value ) ;
207+ } catch {
208+ return String ( value ) ;
209+ }
210+ }
211+
140212/**
141213 * 把 error 对象的所有自有属性(含 SDK 常用的非枚举字段,如 status / error / code / response)
142214 * 序列化成 JSON。作为 formatError 抓不到错误体时的兜底,确保能看到原始错误结构。
@@ -158,6 +230,8 @@ function dumpErrorObject(error: unknown): string {
158230 seen . add ( val ) ;
159231 if ( val instanceof Error ) {
160232 out [ k ] = { name : val . name , message : val . message , ...dumpOwn ( val ) } ;
233+ } else if ( typeof Headers !== 'undefined' && val instanceof Headers ) {
234+ out [ k ] = Object . fromEntries ( val . entries ( ) ) ;
161235 } else {
162236 try { out [ k ] = JSON . parse ( JSON . stringify ( val ) ) ; }
163237 catch { out [ k ] = dumpOwn ( val ) ; }
0 commit comments