@@ -4,6 +4,7 @@ import { isRegExp } from '@sentry/utils/is';
4
4
import { logger } from '@sentry/utils/logger' ;
5
5
import { getEventDescription } from '@sentry/utils/misc' ;
6
6
import { includes } from '@sentry/utils/string' ;
7
+ import { Client } from '../interfaces' ;
7
8
8
9
// "Script error." is hard coded into browsers for errors that it can't read.
9
10
// this is the result of a script being pulled in from an external domain and CORS.
@@ -18,13 +19,6 @@ interface InboundFiltersOptions {
18
19
19
20
/** Inbound filters configurable by the user */
20
21
export class InboundFilters implements Integration {
21
- /** JSDoc */
22
- private ignoreErrors ?: Array < string | RegExp > = DEFAULT_IGNORE_ERRORS ;
23
- /** JSDoc */
24
- private blacklistUrls ?: Array < string | RegExp > ;
25
- /** JSDoc */
26
- private whitelistUrls ?: Array < string | RegExp > ;
27
-
28
22
/**
29
23
* @inheritDoc
30
24
*/
@@ -34,19 +28,23 @@ export class InboundFilters implements Integration {
34
28
*/
35
29
public static id : string = 'InboundFilters' ;
36
30
37
- public constructor ( private readonly options : InboundFiltersOptions = { } ) {
38
- this . configureOptions ( ) ;
39
- }
31
+ public constructor ( private readonly options : InboundFiltersOptions = { } ) { }
40
32
41
33
/**
42
34
* @inheritDoc
43
35
*/
44
36
public setupOnce ( ) : void {
45
37
addGlobalEventProcessor ( async ( event : SentryEvent ) => {
46
- const self = getCurrentHub ( ) . getIntegration ( InboundFilters ) ;
38
+ const hub = getCurrentHub ( ) ;
39
+ if ( ! hub ) {
40
+ return event ;
41
+ }
42
+ const self = hub . getIntegration ( InboundFilters ) ;
47
43
if ( self ) {
48
- self . configureOptions ( ) ;
49
- if ( self . shouldDropEvent ( event ) ) {
44
+ const client = hub . getClient ( ) as Client ;
45
+ const clientOptions = client ? client . getOptions ( ) : { } ;
46
+ const options = self . mergeOptions ( clientOptions ) ;
47
+ if ( self . shouldDropEvent ( event , options ) ) {
50
48
return null ;
51
49
}
52
50
}
@@ -55,22 +53,22 @@ export class InboundFilters implements Integration {
55
53
}
56
54
57
55
/** JSDoc */
58
- public shouldDropEvent ( event : SentryEvent ) : boolean {
59
- if ( this . isIgnoredError ( event ) ) {
56
+ public shouldDropEvent ( event : SentryEvent , options : InboundFiltersOptions ) : boolean {
57
+ if ( this . isIgnoredError ( event , options ) ) {
60
58
logger . warn (
61
59
`Event dropped due to being matched by \`ignoreErrors\` option.\nEvent: ${ getEventDescription ( event ) } ` ,
62
60
) ;
63
61
return true ;
64
62
}
65
- if ( this . isBlacklistedUrl ( event ) ) {
63
+ if ( this . isBlacklistedUrl ( event , options ) ) {
66
64
logger . warn (
67
65
`Event dropped due to being matched by \`blacklistUrls\` option.\nEvent: ${ getEventDescription (
68
66
event ,
69
67
) } .\nUrl: ${ this . getEventFilterUrl ( event ) } `,
70
68
) ;
71
69
return true ;
72
70
}
73
- if ( ! this . isWhitelistedUrl ( event ) ) {
71
+ if ( ! this . isWhitelistedUrl ( event , options ) ) {
74
72
logger . warn (
75
73
`Event dropped due to not being matched by \`whitelistUrls\` option.\nEvent: ${ getEventDescription (
76
74
event ,
@@ -82,35 +80,48 @@ export class InboundFilters implements Integration {
82
80
}
83
81
84
82
/** JSDoc */
85
- public isIgnoredError ( event : SentryEvent ) : boolean {
86
- if ( ! this . ignoreErrors ) {
83
+ public isIgnoredError ( event : SentryEvent , options : InboundFiltersOptions = { } ) : boolean {
84
+ if ( ! options . ignoreErrors || ! options . ignoreErrors . length ) {
87
85
return false ;
88
86
}
89
87
90
88
return this . getPossibleEventMessages ( event ) . some ( message =>
91
89
// Not sure why TypeScript complains here...
92
- ( this . ignoreErrors as Array < RegExp | string > ) . some ( pattern => this . isMatchingPattern ( message , pattern ) ) ,
90
+ ( options . ignoreErrors as Array < RegExp | string > ) . some ( pattern => this . isMatchingPattern ( message , pattern ) ) ,
93
91
) ;
94
92
}
95
93
96
94
/** JSDoc */
97
- public isBlacklistedUrl ( event : SentryEvent ) : boolean {
95
+ public isBlacklistedUrl ( event : SentryEvent , options : InboundFiltersOptions = { } ) : boolean {
98
96
// TODO: Use Glob instead?
99
- if ( ! this . blacklistUrls ) {
97
+ if ( ! options . blacklistUrls || ! options . blacklistUrls . length ) {
100
98
return false ;
101
99
}
102
100
const url = this . getEventFilterUrl ( event ) ;
103
- return ! url ? false : this . blacklistUrls . some ( pattern => this . isMatchingPattern ( url , pattern ) ) ;
101
+ return ! url ? false : options . blacklistUrls . some ( pattern => this . isMatchingPattern ( url , pattern ) ) ;
104
102
}
105
103
106
104
/** JSDoc */
107
- public isWhitelistedUrl ( event : SentryEvent ) : boolean {
105
+ public isWhitelistedUrl ( event : SentryEvent , options : InboundFiltersOptions = { } ) : boolean {
108
106
// TODO: Use Glob instead?
109
- if ( ! this . whitelistUrls ) {
107
+ if ( ! options . whitelistUrls || ! options . whitelistUrls . length ) {
110
108
return true ;
111
109
}
112
110
const url = this . getEventFilterUrl ( event ) ;
113
- return ! url ? true : this . whitelistUrls . some ( pattern => this . isMatchingPattern ( url , pattern ) ) ;
111
+ return ! url ? true : options . whitelistUrls . some ( pattern => this . isMatchingPattern ( url , pattern ) ) ;
112
+ }
113
+
114
+ /** JSDoc */
115
+ public mergeOptions ( clientOptions : InboundFiltersOptions = { } ) : InboundFiltersOptions {
116
+ return {
117
+ blacklistUrls : [ ...( this . options . blacklistUrls || [ ] ) , ...( clientOptions . blacklistUrls || [ ] ) ] ,
118
+ ignoreErrors : [
119
+ ...( this . options . ignoreErrors || [ ] ) ,
120
+ ...( clientOptions . ignoreErrors || [ ] ) ,
121
+ ...DEFAULT_IGNORE_ERRORS ,
122
+ ] ,
123
+ whitelistUrls : [ ...( this . options . whitelistUrls || [ ] ) , ...( clientOptions . whitelistUrls || [ ] ) ] ,
124
+ } ;
114
125
}
115
126
116
127
/** JSDoc */
@@ -124,19 +135,6 @@ export class InboundFilters implements Integration {
124
135
}
125
136
}
126
137
127
- /** JSDoc */
128
- private configureOptions ( ) : void {
129
- if ( this . options . ignoreErrors ) {
130
- this . ignoreErrors = [ ...DEFAULT_IGNORE_ERRORS , ...this . options . ignoreErrors ] ;
131
- }
132
- if ( this . options . blacklistUrls ) {
133
- this . blacklistUrls = [ ...this . options . blacklistUrls ] ;
134
- }
135
- if ( this . options . whitelistUrls ) {
136
- this . whitelistUrls = [ ...this . options . whitelistUrls ] ;
137
- }
138
- }
139
-
140
138
/** JSDoc */
141
139
private getPossibleEventMessages ( event : SentryEvent ) : string [ ] {
142
140
if ( event . message ) {
0 commit comments