@@ -46,7 +46,7 @@ const _httpClientIntegration = ((options: Partial<HttpClientOptions> = {}) => {
4646
4747 return {
4848 name : INTEGRATION_NAME ,
49- setup ( client ) : void {
49+ setup ( client : Client ) : void {
5050 _wrapFetch ( client , _options ) ;
5151 _wrapXHR ( client , _options ) ;
5252 } ,
@@ -70,6 +70,7 @@ function _fetchResponseHandler(
7070 requestInfo : RequestInfo ,
7171 response : Response ,
7272 requestInit ?: RequestInit ,
73+ error ?: unknown ,
7374) : void {
7475 if ( _shouldCaptureResponse ( options , response . status , response . url ) ) {
7576 const request = _getRequest ( requestInfo , requestInit ) ;
@@ -89,9 +90,13 @@ function _fetchResponseHandler(
8990 responseHeaders,
9091 requestCookies,
9192 responseCookies,
93+ stacktrace : error instanceof Error ? error . stack : undefined ,
9294 } ) ;
9395
96+ // withScope(scope => {
97+ // scope.setFingerprint([request.url, request.method, response.status.toString()]);
9498 captureEvent ( event ) ;
99+ // });
95100 }
96101}
97102
@@ -151,6 +156,9 @@ function _xhrResponseHandler(
151156 requestHeaders = headers ;
152157 }
153158
159+ const virtualError = new Error ( ) ;
160+ const virtualStacktrace = virtualError . stack ;
161+
154162 const event = _createEvent ( {
155163 url : xhr . responseURL ,
156164 method,
@@ -159,6 +167,7 @@ function _xhrResponseHandler(
159167 // Can't access request cookies from XHR
160168 responseHeaders,
161169 responseCookies,
170+ stacktrace : virtualStacktrace ,
162171 } ) ;
163172
164173 captureEvent ( event ) ;
@@ -283,20 +292,24 @@ function _wrapFetch(client: Client, options: HttpClientOptions): void {
283292 return ;
284293 }
285294
286- addFetchInstrumentationHandler ( handlerData => {
287- if ( getClient ( ) !== client ) {
288- return ;
289- }
295+ addFetchInstrumentationHandler (
296+ handlerData => {
297+ if ( getClient ( ) !== client ) {
298+ return ;
299+ }
290300
291- const { response, args } = handlerData ;
292- const [ requestInfo , requestInit ] = args as [ RequestInfo , RequestInit | undefined ] ;
301+ const { response, args } = handlerData ;
302+ const [ requestInfo , requestInit ] = args as [ RequestInfo , RequestInit | undefined ] ;
293303
294- if ( ! response ) {
295- return ;
296- }
304+ if ( ! response ) {
305+ return ;
306+ }
297307
298- _fetchResponseHandler ( options , requestInfo , response as Response , requestInit ) ;
299- } ) ;
308+ _fetchResponseHandler ( options , requestInfo , response as Response , requestInit , handlerData . error ) ;
309+ } ,
310+ false ,
311+ true ,
312+ ) ;
300313}
301314
302315/**
@@ -327,7 +340,7 @@ function _wrapXHR(client: Client, options: HttpClientOptions): void {
327340 } catch ( e ) {
328341 DEBUG_BUILD && logger . warn ( 'Error while extracting response event form XHR response' , e ) ;
329342 }
330- } ) ;
343+ } , true ) ;
331344}
332345
333346/**
@@ -358,7 +371,15 @@ function _createEvent(data: {
358371 responseCookies ?: Record < string , string > ;
359372 requestHeaders ?: Record < string , string > ;
360373 requestCookies ?: Record < string , string > ;
374+ stacktrace ?: string ;
361375} ) : SentryEvent {
376+ const client = getClient ( ) ;
377+ const virtualStackTrace = client && data . stacktrace ? data . stacktrace : undefined ;
378+ const stack = virtualStackTrace && client ? client . getOptions ( ) . stackParser ( virtualStackTrace ) : undefined ;
379+
380+ // Remove the first frame from the stack as it's the HttpClient call
381+ const nonSentryStack = stack && stack . length ? stack . slice ( 1 ) : undefined ;
382+
362383 const message = `HTTP Client Error with status code: ${ data . status } ` ;
363384
364385 const event : SentryEvent = {
@@ -368,6 +389,7 @@ function _createEvent(data: {
368389 {
369390 type : 'Error' ,
370391 value : message ,
392+ stacktrace : stack ? { frames : nonSentryStack } : undefined ,
371393 } ,
372394 ] ,
373395 } ,
0 commit comments