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,12 @@ 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 ) ;
77
80
78
81
let requestHeaders , responseHeaders , requestCookies , responseCookies ;
79
82
80
- if ( getCurrentHub ( ) . shouldSendDefaultPii ( ) ) {
83
+ if ( this . _getCurrentHub ( ) . shouldSendDefaultPii ( ) ) {
81
84
[ { headers : requestHeaders , cookies : requestCookies } , { headers : responseHeaders , cookies : responseCookies } ] =
82
85
[
83
86
{ cookieHeader : 'Cookie' , obj : request } ,
@@ -113,7 +116,7 @@ export class HttpClient implements Integration {
113
116
responseCookies,
114
117
} ) ;
115
118
116
- captureEvent ( event ) ;
119
+ this . _getCurrentHub ( ) . captureEvent ( event ) ;
117
120
}
118
121
}
119
122
@@ -125,10 +128,10 @@ export class HttpClient implements Integration {
125
128
* @param headers The HTTP headers
126
129
*/
127
130
private _xhrResponseHandler ( xhr : XMLHttpRequest , method : string , headers : Record < string , string > ) : void {
128
- if ( this . _shouldCaptureResponse ( xhr . status , xhr . responseURL ) ) {
131
+ if ( this . _getCurrentHub && this . _shouldCaptureResponse ( xhr . status , xhr . responseURL ) ) {
129
132
let requestHeaders , responseCookies , responseHeaders ;
130
133
131
- if ( getCurrentHub ( ) . shouldSendDefaultPii ( ) ) {
134
+ if ( this . _getCurrentHub ( ) . shouldSendDefaultPii ( ) ) {
132
135
try {
133
136
const cookieString = xhr . getResponseHeader ( 'Set-Cookie' ) || xhr . getResponseHeader ( 'set-cookie' ) || undefined ;
134
137
@@ -158,7 +161,7 @@ export class HttpClient implements Integration {
158
161
responseCookies,
159
162
} ) ;
160
163
161
- captureEvent ( event ) ;
164
+ this . _getCurrentHub ( ) . captureEvent ( event ) ;
162
165
}
163
166
}
164
167
@@ -362,7 +365,7 @@ export class HttpClient implements Integration {
362
365
* @param url url to verify
363
366
*/
364
367
private _isSentryRequest ( url : string ) : boolean {
365
- const client = getCurrentHub ( ) . getClient ( ) ;
368
+ const client = this . _getCurrentHub && this . _getCurrentHub ( ) . getClient ( ) ;
366
369
367
370
if ( ! client ) {
368
371
return false ;
@@ -397,22 +400,31 @@ export class HttpClient implements Integration {
397
400
requestHeaders ?: Record < string , string > ;
398
401
requestCookies ?: Record < string , string > ;
399
402
} ) : 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 ) ,
403
+ const message = `HTTP Client Error with status code: ${ data . status } ` ;
404
+
405
+ const event : SentryEvent = {
406
+ message,
407
+ exception : {
408
+ values : [
409
+ {
410
+ type : 'Error' ,
411
+ value : message ,
412
+ } ,
413
+ ] ,
414
+ } ,
415
+ request : {
416
+ url : data . url ,
417
+ method : data . method ,
418
+ headers : data . requestHeaders ,
419
+ cookies : data . requestCookies ,
420
+ } ,
421
+ contexts : {
422
+ response : {
423
+ status_code : data . status ,
424
+ headers : data . responseHeaders ,
425
+ cookies : data . responseCookies ,
426
+ body_size : this . _getResponseSizeFromHeaders ( data . responseHeaders ) ,
427
+ } ,
416
428
} ,
417
429
} ;
418
430
0 commit comments