Skip to content

feat: Filter internal Sentry errors from transports/sdk #1732

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Changelog

## Unreleased

- [core] feat: Filter internal Sentry errors from transports/sdk

## 4.3.0

- [browser]: Move `ReportingObserver` integration to "pluggable" making it an opt-in integration
Expand Down
22 changes: 21 additions & 1 deletion packages/core/src/integrations/inboundfilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ const DEFAULT_IGNORE_ERRORS = [/^Script error\.?$/, /^Javascript error: Script e

/** JSDoc */
interface InboundFiltersOptions {
ignoreErrors?: Array<string | RegExp>;
blacklistUrls?: Array<string | RegExp>;
ignoreErrors?: Array<string | RegExp>;
ignoreInternal?: boolean;
whitelistUrls?: Array<string | RegExp>;
}

Expand Down Expand Up @@ -54,6 +55,10 @@ export class InboundFilters implements Integration {

/** JSDoc */
public shouldDropEvent(event: SentryEvent, options: InboundFiltersOptions): boolean {
if (this.isSentryError(event, options)) {
logger.warn(`Event dropped due to being internal Sentry Error.\nEvent: ${getEventDescription(event)}`);
return true;
}
if (this.isIgnoredError(event, options)) {
logger.warn(
`Event dropped due to being matched by \`ignoreErrors\` option.\nEvent: ${getEventDescription(event)}`,
Expand All @@ -79,6 +84,20 @@ export class InboundFilters implements Integration {
return false;
}

/** JSDoc */
public isSentryError(event: SentryEvent, options: InboundFiltersOptions = {}): boolean {
if (!options.ignoreInternal) {
return false;
}

try {
// tslint:disable-next-line:no-unsafe-any
return (event as any).exception.values[0].type === 'SentryError';
} catch (_oO) {
return false;
}
}

/** JSDoc */
public isIgnoredError(event: SentryEvent, options: InboundFiltersOptions = {}): boolean {
if (!options.ignoreErrors || !options.ignoreErrors.length) {
Expand Down Expand Up @@ -120,6 +139,7 @@ export class InboundFilters implements Integration {
...(clientOptions.ignoreErrors || []),
...DEFAULT_IGNORE_ERRORS,
],
ignoreInternal: typeof this.options.ignoreInternal !== 'undefined' ? this.options.ignoreInternal : true,
whitelistUrls: [...(this.options.whitelistUrls || []), ...(clientOptions.whitelistUrls || [])],
};
}
Expand Down
46 changes: 46 additions & 0 deletions packages/core/test/lib/integrations/inboundfilters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ describe('InboundFilters', () => {
});

describe('shouldDropEvent', () => {
it('should drop when error is internal one', () => {
inboundFilters.isSentryError = () => true;
expect(inboundFilters.shouldDropEvent({}, inboundFilters.mergeOptions())).toBe(true);
});

it('should drop when error is ignored', () => {
inboundFilters.isIgnoredError = () => true;
expect(inboundFilters.shouldDropEvent({}, inboundFilters.mergeOptions())).toBe(true);
Expand Down Expand Up @@ -49,6 +54,47 @@ describe('InboundFilters', () => {
});
});

describe('isSentryError', () => {
const messageEvent = {
message: 'captureMessage',
};
const exceptionEvent = {
exception: {
values: [
{
type: 'SyntaxError',
value: 'unidentified ? at line 1337',
},
],
},
};
const sentryEvent = {
exception: {
values: [
{
type: 'SentryError',
value: 'something something server connection',
},
],
},
};

it('should work as expected', () => {
expect(inboundFilters.isSentryError(messageEvent, inboundFilters.mergeOptions())).toBe(false);
expect(inboundFilters.isSentryError(exceptionEvent, inboundFilters.mergeOptions())).toBe(false);
expect(inboundFilters.isSentryError(sentryEvent, inboundFilters.mergeOptions())).toBe(true);
});

it('should be configurable', () => {
inboundFilters = new InboundFilters({
ignoreInternal: false,
});
expect(inboundFilters.isSentryError(messageEvent, inboundFilters.mergeOptions())).toBe(false);
expect(inboundFilters.isSentryError(exceptionEvent, inboundFilters.mergeOptions())).toBe(false);
expect(inboundFilters.isSentryError(sentryEvent, inboundFilters.mergeOptions())).toBe(false);
});
});

describe('ignoreErrors', () => {
const messageEvent = {
message: 'captureMessage',
Expand Down