@@ -218,7 +218,7 @@ export class CopilotApi {
218218 } ,
219219 } ) ;
220220 if ( ! response . ok ) {
221- throw new Error ( `Failed to fetch sessions: ${ response . statusText } ` ) ;
221+ await this . handleApiError ( response , 'getAllSessions' ) ;
222222 }
223223 const sessions = await response . json ( ) ;
224224 return sessions . sessions ;
@@ -250,7 +250,7 @@ export class CopilotApi {
250250 }
251251 } ) ;
252252 if ( ! response . ok ) {
253- throw new Error ( `Failed to fetch session: ${ response . statusText } ` ) ;
253+ await this . handleApiError ( response , 'getSessionInfo' ) ;
254254 }
255255
256256 return ( await response . json ( ) ) as SessionInfo ;
@@ -265,7 +265,7 @@ export class CopilotApi {
265265 } ,
266266 } ) ;
267267 if ( ! logsResponse . ok ) {
268- throw new Error ( `Failed to fetch logs: ${ logsResponse . statusText } ` ) ;
268+ await this . handleApiError ( logsResponse , 'getLogsFromSession' ) ;
269269 }
270270 return await logsResponse . text ( ) ;
271271 }
@@ -283,13 +283,13 @@ export class CopilotApi {
283283 } ) ;
284284 if ( ! response . ok ) {
285285 Logger . warn ( `Failed to fetch job info for session ${ sessionId } : ${ response . statusText } ` , CopilotApi . ID ) ;
286- return undefined ;
286+ return ;
287287 }
288288 const data = await response . json ( ) as JobInfo ;
289289 return data ;
290290 } catch ( error ) {
291291 Logger . warn ( `Error fetching job info for session ${ sessionId } : ${ error } ` , CopilotApi . ID ) ;
292- return undefined ;
292+ return ;
293293 }
294294 }
295295
@@ -305,6 +305,30 @@ export class CopilotApi {
305305
306306 return this . credentialStore . getHub ( authProvider ) ;
307307 }
308+
309+ private async handleApiError ( response : Response , action : string ) : Promise < never > {
310+ let errorBody : string | undefined = undefined ;
311+ try {
312+ errorBody = await response . text ( ) ;
313+ } catch ( e ) { /* ignore */ }
314+ const msg = `'${ action } ' failed with ${ response . statusText } ${ errorBody ? `: ${ errorBody } ` : '' } ` ;
315+ Logger . error ( msg , CopilotApi . ID ) ;
316+
317+ /* __GDPR__
318+ "remoteAgent.apiError" : {
319+ "action" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
320+ "status" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
321+ "body" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
322+ }
323+ */
324+ this . telemetry . sendTelemetryErrorEvent ( 'remoteAgent.apiError' , {
325+ action,
326+ status : response . status . toString ( ) ,
327+ body : errorBody || '' ,
328+ } ) ;
329+
330+ throw new Error ( msg ) ;
331+ }
308332}
309333
310334
0 commit comments