@@ -97,6 +97,78 @@ describe("ApiClient", () => {
9797 } ) ;
9898 } ) ;
9999
100+ it ( "adds network and whitelist guidance to DNS failures without suggesting credentials" , async ( ) => {
101+ fetchWithSsrFGuardMock . mockRejectedValueOnce (
102+ new Error ( "getaddrinfo ENOTFOUND api.sgroup.qq.com" ) ,
103+ ) ;
104+
105+ const client = new ApiClient ( { baseUrl : "https://qqbot.test" } ) ;
106+ let error : unknown ;
107+ try {
108+ await client . request ( "token-1" , "GET" , "/v2/users/@me" ) ;
109+ } catch ( caught ) {
110+ error = caught ;
111+ }
112+
113+ const message = error instanceof Error ? error . message : String ( error ) ;
114+ expect ( message ) . toContain ( "Network error [/v2/users/@me]" ) ;
115+ expect ( message ) . toContain ( "network connectivity and DNS" ) ;
116+ expect ( message ) . toContain ( "server IP whitelist" ) ;
117+ expect ( message ) . not . toContain ( "appId" ) ;
118+ expect ( message ) . not . toContain ( "clientSecret" ) ;
119+ } ) ;
120+
121+ it ( "adds credential guidance to structured HTTP 401 errors" , async ( ) => {
122+ const release = vi . fn ( async ( ) => { } ) ;
123+ fetchWithSsrFGuardMock . mockResolvedValueOnce ( {
124+ response : new Response ( '{"code":11241,"message":"invalid credentials"}' , {
125+ status : 401 ,
126+ headers : { "content-type" : "application/json" } ,
127+ } ) ,
128+ release,
129+ } ) ;
130+
131+ const client = new ApiClient ( { baseUrl : "https://qqbot.test" } ) ;
132+ let error : unknown ;
133+ try {
134+ await client . request ( "token-1" , "POST" , "/v2/messages" , { content : "hi" } ) ;
135+ } catch ( caught ) {
136+ error = caught ;
137+ }
138+
139+ const message = error instanceof Error ? error . message : String ( error ) ;
140+ expect ( message ) . toContain ( "API Error [/v2/messages]: invalid credentials" ) ;
141+ expect ( message ) . toContain ( "QQBot account appId and clientSecret" ) ;
142+ expect ( message ) . toContain ( "https://q.qq.com/" ) ;
143+ expect ( release ) . toHaveBeenCalledTimes ( 1 ) ;
144+ } ) ;
145+
146+ it ( "keeps non-auth structured API guidance generic" , async ( ) => {
147+ const release = vi . fn ( async ( ) => { } ) ;
148+ fetchWithSsrFGuardMock . mockResolvedValueOnce ( {
149+ response : new Response ( '{"code":40034025,"message":"invalid event id"}' , {
150+ status : 400 ,
151+ headers : { "content-type" : "application/json" } ,
152+ } ) ,
153+ release,
154+ } ) ;
155+
156+ const client = new ApiClient ( { baseUrl : "https://qqbot.test" } ) ;
157+ let error : unknown ;
158+ try {
159+ await client . request ( "token-1" , "POST" , "/v2/messages" , { content : "hi" } ) ;
160+ } catch ( caught ) {
161+ error = caught ;
162+ }
163+
164+ const message = error instanceof Error ? error . message : String ( error ) ;
165+ expect ( message ) . toContain ( "API Error [/v2/messages]: invalid event id" ) ;
166+ expect ( message ) . toContain ( "QQBot API troubleshooting" ) ;
167+ expect ( message ) . not . toContain ( "appId" ) ;
168+ expect ( message ) . not . toContain ( "clientSecret" ) ;
169+ expect ( release ) . toHaveBeenCalledTimes ( 1 ) ;
170+ } ) ;
171+
100172 it ( "bounds successful response bodies without using response.text()" , async ( ) => {
101173 const release = vi . fn ( async ( ) => { } ) ;
102174 const streamed = createStreamingResponse ( {
0 commit comments