Skip to content

Commit

Permalink
ref(core): Refactor InboundFilters integration to use processEvent (
Browse files Browse the repository at this point in the history
#9020)

This refactors core integrations to use `processEvent`.
  • Loading branch information
mydea committed Sep 27, 2023
1 parent d4f67a2 commit 95e1801
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 41 deletions.
26 changes: 9 additions & 17 deletions packages/core/src/integrations/inboundfilters.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Event, EventProcessor, Hub, Integration, StackFrame } from '@sentry/types';
import type { Client, Event, EventHint, Integration, StackFrame } from '@sentry/types';
import { getEventDescription, logger, stringMatchesSomePattern } from '@sentry/utils';

// "Script error." is hard coded into browsers for errors that it can't read.
Expand Down Expand Up @@ -48,23 +48,15 @@ export class InboundFilters implements Integration {
/**
* @inheritDoc
*/
public setupOnce(addGlobalEventProcessor: (processor: EventProcessor) => void, getCurrentHub: () => Hub): void {
const eventProcess: EventProcessor = (event: Event) => {
const hub = getCurrentHub();
if (hub) {
const self = hub.getIntegration(InboundFilters);
if (self) {
const client = hub.getClient();
const clientOptions = client ? client.getOptions() : {};
const options = _mergeOptions(self._options, clientOptions);
return _shouldDropEvent(event, options) ? null : event;
}
}
return event;
};
public setupOnce(_addGlobaleventProcessor: unknown, _getCurrentHub: unknown): void {
// noop
}

eventProcess.id = this.name;
addGlobalEventProcessor(eventProcess);
/** @inheritDoc */
public processEvent(event: Event, _eventHint: EventHint, client: Client): Event | null {
const clientOptions = client.getOptions();
const options = _mergeOptions(this._options, clientOptions);
return _shouldDropEvent(event, options) ? null : event;
}
}

Expand Down
43 changes: 19 additions & 24 deletions packages/core/test/lib/integrations/inboundfilters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import type { Event, EventProcessor } from '@sentry/types';

import type { InboundFiltersOptions } from '../../../src/integrations/inboundfilters';
import { InboundFilters } from '../../../src/integrations/inboundfilters';
import { getDefaultTestClientOptions, TestClient } from '../../mocks/client';

const PUBLIC_DSN = 'https://username@domain/123';

/**
* Creates an instance of the InboundFilters integration and returns
Expand All @@ -25,30 +28,22 @@ function createInboundFiltersEventProcessor(
options: Partial<InboundFiltersOptions> = {},
clientOptions: Partial<InboundFiltersOptions> = {},
): EventProcessor {
const eventProcessors: EventProcessor[] = [];
const inboundFiltersInstance = new InboundFilters(options);

function addGlobalEventProcessor(processor: EventProcessor): void {
eventProcessors.push(processor);
expect(eventProcessors).toHaveLength(1);
}

function getCurrentHub(): any {
return {
getIntegration(_integration: any): any {
// pretend integration is enabled
return inboundFiltersInstance;
},
getClient(): any {
return {
getOptions: () => clientOptions,
};
},
};
}

inboundFiltersInstance.setupOnce(addGlobalEventProcessor, getCurrentHub);
return eventProcessors[0];
const client = new TestClient(
getDefaultTestClientOptions({
dsn: PUBLIC_DSN,
...clientOptions,
defaultIntegrations: false,
integrations: [new InboundFilters(options)],
}),
);

client.setupIntegrations();

const eventProcessors = client['_eventProcessors'];
const eventProcessor = eventProcessors.find(processor => processor.id === 'InboundFilters');

expect(eventProcessor).toBeDefined();
return eventProcessor!;
}

// Fixtures
Expand Down

0 comments on commit 95e1801

Please sign in to comment.