Skip to content

Commit c69749d

Browse files
authored
[fix] Correct usage of init config in inboundFilters (getsentry#1703)
1 parent 141f55f commit c69749d

File tree

4 files changed

+313
-170
lines changed

4 files changed

+313
-170
lines changed

packages/browser/test/integration/init.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Sentry.init({
3636
// debug: true,
3737
attachStacktrace: true,
3838
transport: DummyTransport,
39+
ignoreErrors: ['ignoreErrorTest'],
3940
// integrations: function(old) {
4041
// return [new Sentry.Integrations.Debug({ stringify: true })].concat(old);
4142
// },

packages/browser/test/integration/test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,31 @@ for (var idx in frames) {
7878
document.body.removeChild(this.iframe);
7979
});
8080

81+
describe('config', function() {
82+
it('should allow to ignore specific errors', function(done) {
83+
var iframe = this.iframe;
84+
85+
iframeExecute(
86+
iframe,
87+
done,
88+
function() {
89+
Sentry.captureException(new Error('foo'));
90+
Sentry.captureException(new Error('ignoreErrorTest'));
91+
Sentry.captureException(new Error('bar'));
92+
},
93+
function(sentryData) {
94+
if (debounceAssertEventCount(sentryData, 2, done)) {
95+
assert.equal(sentryData[0].exception.values[0].type, 'Error');
96+
assert.equal(sentryData[0].exception.values[0].value, 'foo');
97+
assert.equal(sentryData[1].exception.values[0].type, 'Error');
98+
assert.equal(sentryData[1].exception.values[0].value, 'bar');
99+
done();
100+
}
101+
}
102+
);
103+
});
104+
});
105+
81106
describe('API', function() {
82107
it('should capture Sentry.captureMessage', function(done) {
83108
var iframe = this.iframe;

packages/core/src/integrations/inboundfilters.ts

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { isRegExp } from '@sentry/utils/is';
44
import { logger } from '@sentry/utils/logger';
55
import { getEventDescription } from '@sentry/utils/misc';
66
import { includes } from '@sentry/utils/string';
7+
import { Client } from '../interfaces';
78

89
// "Script error." is hard coded into browsers for errors that it can't read.
910
// this is the result of a script being pulled in from an external domain and CORS.
@@ -18,13 +19,6 @@ interface InboundFiltersOptions {
1819

1920
/** Inbound filters configurable by the user */
2021
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-
2822
/**
2923
* @inheritDoc
3024
*/
@@ -34,19 +28,23 @@ export class InboundFilters implements Integration {
3428
*/
3529
public static id: string = 'InboundFilters';
3630

37-
public constructor(private readonly options: InboundFiltersOptions = {}) {
38-
this.configureOptions();
39-
}
31+
public constructor(private readonly options: InboundFiltersOptions = {}) {}
4032

4133
/**
4234
* @inheritDoc
4335
*/
4436
public setupOnce(): void {
4537
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);
4743
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)) {
5048
return null;
5149
}
5250
}
@@ -55,22 +53,22 @@ export class InboundFilters implements Integration {
5553
}
5654

5755
/** 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)) {
6058
logger.warn(
6159
`Event dropped due to being matched by \`ignoreErrors\` option.\nEvent: ${getEventDescription(event)}`,
6260
);
6361
return true;
6462
}
65-
if (this.isBlacklistedUrl(event)) {
63+
if (this.isBlacklistedUrl(event, options)) {
6664
logger.warn(
6765
`Event dropped due to being matched by \`blacklistUrls\` option.\nEvent: ${getEventDescription(
6866
event,
6967
)}.\nUrl: ${this.getEventFilterUrl(event)}`,
7068
);
7169
return true;
7270
}
73-
if (!this.isWhitelistedUrl(event)) {
71+
if (!this.isWhitelistedUrl(event, options)) {
7472
logger.warn(
7573
`Event dropped due to not being matched by \`whitelistUrls\` option.\nEvent: ${getEventDescription(
7674
event,
@@ -82,35 +80,48 @@ export class InboundFilters implements Integration {
8280
}
8381

8482
/** 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) {
8785
return false;
8886
}
8987

9088
return this.getPossibleEventMessages(event).some(message =>
9189
// 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)),
9391
);
9492
}
9593

9694
/** JSDoc */
97-
public isBlacklistedUrl(event: SentryEvent): boolean {
95+
public isBlacklistedUrl(event: SentryEvent, options: InboundFiltersOptions = {}): boolean {
9896
// TODO: Use Glob instead?
99-
if (!this.blacklistUrls) {
97+
if (!options.blacklistUrls || !options.blacklistUrls.length) {
10098
return false;
10199
}
102100
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));
104102
}
105103

106104
/** JSDoc */
107-
public isWhitelistedUrl(event: SentryEvent): boolean {
105+
public isWhitelistedUrl(event: SentryEvent, options: InboundFiltersOptions = {}): boolean {
108106
// TODO: Use Glob instead?
109-
if (!this.whitelistUrls) {
107+
if (!options.whitelistUrls || !options.whitelistUrls.length) {
110108
return true;
111109
}
112110
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+
};
114125
}
115126

116127
/** JSDoc */
@@ -124,19 +135,6 @@ export class InboundFilters implements Integration {
124135
}
125136
}
126137

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-
140138
/** JSDoc */
141139
private getPossibleEventMessages(event: SentryEvent): string[] {
142140
if (event.message) {

0 commit comments

Comments
 (0)