1- import { captureEvent , getCurrentHub } from '@sentry/core' ;
2- import { Event as SentryEvent , Integration } from '@sentry/types' ;
1+ import { Event as SentryEvent , EventProcessor , Hub , Integration } from '@sentry/types' ;
32import { addExceptionMechanism , fill , GLOBAL_OBJ , logger , supportsNativeFetch } from '@sentry/utils' ;
43
5- import { eventFromUnknownInput } from '../eventbuilder' ;
6-
74export type HttpStatusCodeRange = [ number , number ] | number ;
85export type HttpRequestTarget = string | RegExp ;
96interface HttpClientOptions {
@@ -41,6 +38,11 @@ export class HttpClient implements Integration {
4138
4239 private readonly _options : HttpClientOptions ;
4340
41+ /**
42+ * Returns current hub.
43+ */
44+ private _getCurrentHub ?: ( ) => Hub ;
45+
4446 /**
4547 * @inheritDoc
4648 *
@@ -59,7 +61,8 @@ export class HttpClient implements Integration {
5961 *
6062 * @param options
6163 */
62- public setupOnce ( ) : void {
64+ public setupOnce ( _ : ( callback : EventProcessor ) => void , getCurrentHub : ( ) => Hub ) : void {
65+ this . _getCurrentHub = getCurrentHub ;
6366 this . _wrapFetch ( ) ;
6467 this . _wrapXHR ( ) ;
6568 }
@@ -72,12 +75,13 @@ export class HttpClient implements Integration {
7275 * @param requestInit The request init object
7376 */
7477 private _fetchResponseHandler ( requestInfo : RequestInfo , response : Response , requestInit ?: RequestInit ) : void {
75- if ( this . _shouldCaptureResponse ( response . status , response . url ) ) {
78+ if ( this . _getCurrentHub && this . _shouldCaptureResponse ( response . status , response . url ) ) {
7679 const request = new Request ( requestInfo , requestInit ) ;
80+ const hub = this . _getCurrentHub ( ) ;
7781
7882 let requestHeaders , responseHeaders , requestCookies , responseCookies ;
7983
80- if ( getCurrentHub ( ) . shouldSendDefaultPii ( ) ) {
84+ if ( hub . shouldSendDefaultPii ( ) ) {
8185 [ { headers : requestHeaders , cookies : requestCookies } , { headers : responseHeaders , cookies : responseCookies } ] =
8286 [
8387 { cookieHeader : 'Cookie' , obj : request } ,
@@ -113,7 +117,7 @@ export class HttpClient implements Integration {
113117 responseCookies,
114118 } ) ;
115119
116- captureEvent ( event ) ;
120+ hub . captureEvent ( event ) ;
117121 }
118122 }
119123
@@ -125,10 +129,11 @@ export class HttpClient implements Integration {
125129 * @param headers The HTTP headers
126130 */
127131 private _xhrResponseHandler ( xhr : XMLHttpRequest , method : string , headers : Record < string , string > ) : void {
128- if ( this . _shouldCaptureResponse ( xhr . status , xhr . responseURL ) ) {
132+ if ( this . _getCurrentHub && this . _shouldCaptureResponse ( xhr . status , xhr . responseURL ) ) {
129133 let requestHeaders , responseCookies , responseHeaders ;
134+ const hub = this . _getCurrentHub ( ) ;
130135
131- if ( getCurrentHub ( ) . shouldSendDefaultPii ( ) ) {
136+ if ( hub . shouldSendDefaultPii ( ) ) {
132137 try {
133138 const cookieString = xhr . getResponseHeader ( 'Set-Cookie' ) || xhr . getResponseHeader ( 'set-cookie' ) || undefined ;
134139
@@ -158,7 +163,7 @@ export class HttpClient implements Integration {
158163 responseCookies,
159164 } ) ;
160165
161- captureEvent ( event ) ;
166+ hub . captureEvent ( event ) ;
162167 }
163168 }
164169
@@ -362,7 +367,7 @@ export class HttpClient implements Integration {
362367 * @param url url to verify
363368 */
364369 private _isSentryRequest ( url : string ) : boolean {
365- const client = getCurrentHub ( ) . getClient ( ) ;
370+ const client = this . _getCurrentHub && this . _getCurrentHub ( ) . getClient ( ) ;
366371
367372 if ( ! client ) {
368373 return false ;
@@ -397,22 +402,31 @@ export class HttpClient implements Integration {
397402 requestHeaders ?: Record < string , string > ;
398403 requestCookies ?: Record < string , string > ;
399404 } ) : SentryEvent {
400- const event = eventFromUnknownInput ( ( ) => [ ] , `HTTP Client Error with status code: ${ data . status } ` ) ;
401-
402- event . request = {
403- url : data . url ,
404- method : data . method ,
405- headers : data . requestHeaders ,
406- cookies : data . requestCookies ,
407- } ;
408-
409- event . contexts = {
410- ...event . contexts ,
411- response : {
412- status_code : data . status ,
413- headers : data . responseHeaders ,
414- cookies : data . responseCookies ,
415- body_size : this . _getResponseSizeFromHeaders ( data . responseHeaders ) ,
405+ const message = `HTTP Client Error with status code: ${ data . status } ` ;
406+
407+ const event : SentryEvent = {
408+ message,
409+ exception : {
410+ values : [
411+ {
412+ type : 'Error' ,
413+ value : message ,
414+ } ,
415+ ] ,
416+ } ,
417+ request : {
418+ url : data . url ,
419+ method : data . method ,
420+ headers : data . requestHeaders ,
421+ cookies : data . requestCookies ,
422+ } ,
423+ contexts : {
424+ response : {
425+ status_code : data . status ,
426+ headers : data . responseHeaders ,
427+ cookies : data . responseCookies ,
428+ body_size : this . _getResponseSizeFromHeaders ( data . responseHeaders ) ,
429+ } ,
416430 } ,
417431 } ;
418432
0 commit comments