@@ -25,10 +25,10 @@ const settings: Settings = {
2525 telemetry : false ,
2626} ;
2727
28- function jsonResponse ( body : unknown , status = 200 ) : Response {
28+ function jsonResponse ( body : unknown , status = 200 , headers : Record < string , string > = { } ) : Response {
2929 return new Response ( JSON . stringify ( body ) , {
3030 status,
31- headers : { "content-type" : "application/json" } ,
31+ headers : { "content-type" : "application/json" , ... headers } ,
3232 } ) ;
3333}
3434
@@ -202,15 +202,53 @@ test("Token Plan 多地区并行校验选择实际接受 Key 的国际站点", a
202202 }
203203} ) ;
204204
205- test ( "所有候选站点明确拒绝 Key 时返回 AUTH 校验失败 " , async ( ) => {
205+ test ( "所有候选站点拒绝 Key 时透传首个服务端错误 " , async ( ) => {
206206 const originalFetch = httpTransport . fetch ;
207- httpTransport . fetch = async ( ) =>
208- jsonResponse ( { code : "InvalidApiKey" , message : "invalid key" } , 401 ) ;
207+ httpTransport . fetch = async ( input ) => {
208+ const requestId = requestUrl ( input ) . includes ( "cn-beijing" )
209+ ? "req-cn-rejected"
210+ : "req-sg-rejected" ;
211+ return jsonResponse (
212+ { code : "InvalidApiKey" , message : "invalid key" , request_id : requestId } ,
213+ 401 ,
214+ ) ;
215+ } ;
209216
210217 try {
211218 const error = await captureValidationError ( "sk-sp-placeholder" ) ;
212- expect ( error . exitCode ) . toBe ( ExitCode . AUTH ) ;
213- expect ( error . message ) . toMatch ( / A P I k e y v a l i d a t i o n f a i l e d / ) ;
219+ expect ( error . exitCode ) . toBe ( ExitCode . GENERAL ) ;
220+ expect ( error . message ) . toBe ( "invalid key" ) ;
221+ expect ( error . api ) . toEqual ( {
222+ httpStatus : 401 ,
223+ apiCode : "InvalidApiKey" ,
224+ requestId : "req-cn-rejected" ,
225+ } ) ;
226+ } finally {
227+ httpTransport . fetch = originalFetch ;
228+ }
229+ } ) ;
230+
231+ test ( "显式 Base URL 返回 404 时保留状态和 request ID" , async ( ) => {
232+ const originalFetch = httpTransport . fetch ;
233+ httpTransport . fetch = async ( ) =>
234+ jsonResponse ( { code : "NotFound" , message : "models route not found" } , 404 , {
235+ "x-request-id" : "req-route-not-found" ,
236+ } ) ;
237+
238+ try {
239+ const error = await validateApiKey ( { identity, settings } , "sk-ws-placeholder" , {
240+ explicitBaseUrl : "https://ws-explicit.ap-southeast-1.maas.aliyuncs.com" ,
241+ } ) . catch ( ( caught ) => caught ) ;
242+ expect ( error ) . toBeInstanceOf ( BailianError ) ;
243+ expect ( error ) . toMatchObject ( {
244+ exitCode : ExitCode . GENERAL ,
245+ message : "models route not found" ,
246+ api : {
247+ httpStatus : 404 ,
248+ apiCode : "NotFound" ,
249+ requestId : "req-route-not-found" ,
250+ } ,
251+ } ) ;
214252 } finally {
215253 httpTransport . fetch = originalFetch ;
216254 }
0 commit comments