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' ;
3
2
import { addExceptionMechanism , fill , GLOBAL_OBJ , logger , supportsNativeFetch } from '@sentry/utils' ;
4
3
5
- import { eventFromUnknownInput } from '../eventbuilder' ;
6
-
7
4
export type HttpStatusCodeRange = [ number , number ] | number ;
8
5
export type HttpRequestTarget = string | RegExp ;
9
6
interface HttpClientOptions {
@@ -41,6 +38,11 @@ export class HttpClient implements Integration {
41
38
42
39
private readonly _options : HttpClientOptions ;
43
40
41
+ /**
42
+ * Returns current hub.
43
+ */
44
+ private _getCurrentHub ?: ( ) => Hub ;
45
+
44
46
/**
45
47
* @inheritDoc
46
48
*
@@ -59,7 +61,8 @@ export class HttpClient implements Integration {
59
61
*
60
62
* @param options
61
63
*/
62
- public setupOnce ( ) : void {
64
+ public setupOnce ( _ : ( callback : EventProcessor ) => void , getCurrentHub : ( ) => Hub ) : void {
65
+ this . _getCurrentHub = getCurrentHub ;
63
66
this . _wrapFetch ( ) ;
64
67
this . _wrapXHR ( ) ;
65
68
}
@@ -72,12 +75,13 @@ export class HttpClient implements Integration {
72
75
* @param requestInit The request init object
73
76
*/
74
77
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 ) ) {
76
79
const request = new Request ( requestInfo , requestInit ) ;
80
+ const hub = this . _getCurrentHub ( ) ;
77
81
78
82
let requestHeaders , responseHeaders , requestCookies , responseCookies ;
79
83
80
- if ( getCurrentHub ( ) . shouldSendDefaultPii ( ) ) {
84
+ if ( hub . shouldSendDefaultPii ( ) ) {
81
85
[ { headers : requestHeaders , cookies : requestCookies } , { headers : responseHeaders , cookies : responseCookies } ] =
82
86
[
83
87
{ cookieHeader : 'Cookie' , obj : request } ,
@@ -113,7 +117,7 @@ export class HttpClient implements Integration {
113
117
responseCookies,
114
118
} ) ;
115
119
116
- captureEvent ( event ) ;
120
+ hub . captureEvent ( event ) ;
117
121
}
118
122
}
119
123
@@ -125,10 +129,11 @@ export class HttpClient implements Integration {
125
129
* @param headers The HTTP headers
126
130
*/
127
131
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 ) ) {
129
133
let requestHeaders , responseCookies , responseHeaders ;
134
+ const hub = this . _getCurrentHub ( ) ;
130
135
131
- if ( getCurrentHub ( ) . shouldSendDefaultPii ( ) ) {
136
+ if ( hub . shouldSendDefaultPii ( ) ) {
132
137
try {
133
138
const cookieString = xhr . getResponseHeader ( 'Set-Cookie' ) || xhr . getResponseHeader ( 'set-cookie' ) || undefined ;
134
139
@@ -158,7 +163,7 @@ export class HttpClient implements Integration {
158
163
responseCookies,
159
164
} ) ;
160
165
161
- captureEvent ( event ) ;
166
+ hub . captureEvent ( event ) ;
162
167
}
163
168
}
164
169
@@ -362,7 +367,7 @@ export class HttpClient implements Integration {
362
367
* @param url url to verify
363
368
*/
364
369
private _isSentryRequest ( url : string ) : boolean {
365
- const client = getCurrentHub ( ) . getClient ( ) ;
370
+ const client = this . _getCurrentHub && this . _getCurrentHub ( ) . getClient ( ) ;
366
371
367
372
if ( ! client ) {
368
373
return false ;
@@ -397,22 +402,31 @@ export class HttpClient implements Integration {
397
402
requestHeaders ?: Record < string , string > ;
398
403
requestCookies ?: Record < string , string > ;
399
404
} ) : 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
+ } ,
416
430
} ,
417
431
} ;
418
432
0 commit comments