Skip to content

Commit f7b6452

Browse files
committed
feat: API changes
- Add hint parameter - Remove eventprocessors from hub
1 parent 1f3bdca commit f7b6452

File tree

24 files changed

+328
-421
lines changed

24 files changed

+328
-421
lines changed

packages/browser/src/backend.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Backend, logger, Options, SentryError } from '@sentry/core';
2-
import { SentryEvent, SentryResponse, Status } from '@sentry/types';
2+
import { SentryEvent, SentryEventHint, SentryResponse, Severity, Status } from '@sentry/types';
33
import { isDOMError, isDOMException, isError, isErrorEvent, isPlainObject } from '@sentry/utils/is';
44
import { supportsFetch } from '@sentry/utils/supports';
55
import { eventFromPlainObject, eventFromStacktrace, prepareFramesForEvent } from './parsers';
@@ -57,7 +57,7 @@ export class BrowserBackend implements Backend {
5757
/**
5858
* @inheritDoc
5959
*/
60-
public async eventFromException(exception: any, syntheticException: Error | null): Promise<SentryEvent> {
60+
public async eventFromException(exception: any, hint?: SentryEventHint): Promise<SentryEvent> {
6161
let event;
6262

6363
if (isErrorEvent(exception as ErrorEvent) && (exception as ErrorEvent).error) {
@@ -74,16 +74,16 @@ export class BrowserBackend implements Backend {
7474
const name = ex.name || (isDOMError(ex) ? 'DOMError' : 'DOMException');
7575
const message = ex.message ? `${name}: ${ex.message}` : name;
7676

77-
event = await this.eventFromMessage(message, syntheticException);
77+
event = await this.eventFromMessage(message, undefined, hint);
7878
} else if (isError(exception as Error)) {
7979
// we have a real Error object, do nothing
8080
event = eventFromStacktrace(computeStackTrace(exception as Error));
81-
} else if (isPlainObject(exception as {})) {
81+
} else if (isPlainObject(exception as {}) && hint && hint.syntheticException) {
8282
// If it is plain Object, serialize it manually and extract options
8383
// This will allow us to group events based on top-level keys
8484
// which is much better than creating new group when any key/value change
8585
const ex = exception as {};
86-
event = eventFromPlainObject(ex, syntheticException);
86+
event = eventFromPlainObject(ex, hint.syntheticException);
8787
} else {
8888
// If none of previous checks were valid, then it means that
8989
// it's not a DOMError/DOMException
@@ -92,7 +92,7 @@ export class BrowserBackend implements Backend {
9292
// it's not an Error
9393
// So bail out and capture it as a simple message:
9494
const ex = exception as string;
95-
event = await this.eventFromMessage(ex, syntheticException);
95+
event = await this.eventFromMessage(ex, undefined, hint);
9696
}
9797

9898
event = {
@@ -112,14 +112,15 @@ export class BrowserBackend implements Backend {
112112
/**
113113
* @inheritDoc
114114
*/
115-
public async eventFromMessage(message: string, syntheticException: Error | null): Promise<SentryEvent> {
115+
public async eventFromMessage(message: string, level?: Severity, hint?: SentryEventHint): Promise<SentryEvent> {
116116
const event: SentryEvent = {
117117
fingerprint: [message],
118+
level,
118119
message,
119120
};
120121

121-
if (this.options.attachStacktrace && syntheticException) {
122-
const stacktrace = computeStackTrace(syntheticException);
122+
if (this.options.attachStacktrace && hint && hint.syntheticException) {
123+
const stacktrace = computeStackTrace(hint.syntheticException);
123124
const frames = prepareFramesForEvent(stacktrace.stack);
124125
event.stacktrace = {
125126
frames,

packages/browser/src/integrations/dedupe.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { logger } from '@sentry/core';
2-
import { getCurrentHub } from '@sentry/hub';
2+
import { getCurrentHub, Scope } from '@sentry/hub';
33
import { Integration, SentryEvent, SentryException, StackFrame } from '@sentry/types';
44

55
/** Deduplication filter */
@@ -18,17 +18,19 @@ export class Dedupe implements Integration {
1818
* @inheritDoc
1919
*/
2020
public install(): void {
21-
getCurrentHub().addEventProcessor(async (event: SentryEvent) => {
22-
// Juuust in case something goes wrong
23-
try {
24-
if (this.shouldDropEvent(event)) {
25-
return null;
21+
getCurrentHub().configureScope((scope: Scope) => {
22+
scope.addEventProcessor(async (event: SentryEvent) => {
23+
// Juuust in case something goes wrong
24+
try {
25+
if (this.shouldDropEvent(event)) {
26+
return null;
27+
}
28+
} catch (_oO) {
29+
return (this.previousEvent = event);
2630
}
27-
} catch (_oO) {
28-
return (this.previousEvent = event);
29-
}
3031

31-
return (this.previousEvent = event);
32+
return (this.previousEvent = event);
33+
});
3234
});
3335
}
3436

packages/browser/src/integrations/helpers.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getCurrentHub } from '@sentry/hub';
1+
import { getCurrentHub, Scope } from '@sentry/hub';
22
import { Mechanism, SentryEvent, SentryWrappedFunction } from '@sentry/types';
33
import { isFunction } from '@sentry/utils/is';
44
import { htmlTreeAsString } from '@sentry/utils/misc';
@@ -66,15 +66,17 @@ export function wrap(
6666
ignoreNextOnError();
6767

6868
getCurrentHub().withScope(async () => {
69-
getCurrentHub().addEventProcessor(async (event: SentryEvent) => {
70-
const processedEvent = { ...event };
69+
getCurrentHub().configureScope((scope: Scope) => {
70+
scope.addEventProcessor(async (event: SentryEvent) => {
71+
const processedEvent = { ...event };
7172

72-
if (options.mechanism) {
73-
processedEvent.exception = processedEvent.exception || {};
74-
processedEvent.exception.mechanism = options.mechanism;
75-
}
73+
if (options.mechanism) {
74+
processedEvent.exception = processedEvent.exception || {};
75+
processedEvent.exception.mechanism = options.mechanism;
76+
}
7677

77-
return processedEvent;
78+
return processedEvent;
79+
});
7880
});
7981

8082
getCurrentHub().captureException(ex);

packages/browser/src/integrations/inboundfilters.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { logger } from '@sentry/core';
2-
import { getCurrentHub } from '@sentry/hub';
2+
import { getCurrentHub, Scope } from '@sentry/hub';
33
import { Integration, SentryEvent } from '@sentry/types';
44
import { isRegExp } from '@sentry/utils/is';
55
import { BrowserOptions } from '../backend';
@@ -27,11 +27,13 @@ export class InboundFilters implements Integration {
2727
public install(options: BrowserOptions = {}): void {
2828
this.configureOptions(options);
2929

30-
getCurrentHub().addEventProcessor(async (event: SentryEvent) => {
31-
if (this.shouldDropEvent(event)) {
32-
return null;
33-
}
34-
return event;
30+
getCurrentHub().configureScope((scope: Scope) => {
31+
scope.addEventProcessor(async (event: SentryEvent) => {
32+
if (this.shouldDropEvent(event)) {
33+
return null;
34+
}
35+
return event;
36+
});
3537
});
3638
}
3739

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getCurrentHub } from '@sentry/hub';
1+
import { getCurrentHub, Scope } from '@sentry/hub';
22
import { Integration, SentryEvent } from '@sentry/types';
33
import { SDK_NAME, SDK_VERSION } from '../version';
44

@@ -13,19 +13,21 @@ export class SDKInformation implements Integration {
1313
* @inheritDoc
1414
*/
1515
public install(): void {
16-
getCurrentHub().addEventProcessor(async (event: SentryEvent) => ({
17-
...event,
18-
sdk: {
19-
name: SDK_NAME,
20-
packages: [
21-
...((event.sdk && event.sdk.packages) || []),
22-
{
23-
name: 'npm:@sentry/browser',
24-
version: SDK_VERSION,
25-
},
26-
],
27-
version: SDK_VERSION,
28-
},
29-
}));
16+
getCurrentHub().configureScope((scope: Scope) => {
17+
scope.addEventProcessor(async (event: SentryEvent) => ({
18+
...event,
19+
sdk: {
20+
name: SDK_NAME,
21+
packages: [
22+
...((event.sdk && event.sdk.packages) || []),
23+
{
24+
name: 'npm:@sentry/browser',
25+
version: SDK_VERSION,
26+
},
27+
],
28+
version: SDK_VERSION,
29+
},
30+
}));
31+
});
3032
}
3133
}

packages/browser/test/index.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,10 @@ describe('SentryBrowser', () => {
7575
getCurrentHub().pushScope();
7676
getCurrentHub().bindClient(
7777
new BrowserClient({
78-
afterSend: (event: SentryEvent) => {
78+
beforeSend: (event: SentryEvent) => {
7979
expect(event.breadcrumbs!).to.have.lengthOf(2);
8080
done();
81+
return event;
8182
},
8283
dsn,
8384
}),
@@ -106,13 +107,14 @@ describe('SentryBrowser', () => {
106107
getCurrentHub().pushScope();
107108
getCurrentHub().bindClient(
108109
new BrowserClient({
109-
afterSend: (event: SentryEvent) => {
110+
beforeSend: (event: SentryEvent) => {
110111
expect(event.exception).to.not.be.undefined;
111112
expect(event.exception!.values![0]).to.not.be.undefined;
112113
expect(event.exception!.values![0].type).to.equal('Error');
113114
expect(event.exception!.values![0].value).to.equal('test');
114115
expect(event.exception!.values![0].stacktrace).to.not.be.empty;
115116
done();
117+
return event;
116118
},
117119
dsn,
118120
}),
@@ -129,10 +131,11 @@ describe('SentryBrowser', () => {
129131
getCurrentHub().pushScope();
130132
getCurrentHub().bindClient(
131133
new BrowserClient({
132-
afterSend: (event: SentryEvent) => {
134+
beforeSend: (event: SentryEvent) => {
133135
expect(event.message).to.equal('test');
134136
expect(event.exception).to.be.undefined;
135137
done();
138+
return event;
136139
},
137140
dsn,
138141
}),
@@ -145,10 +148,11 @@ describe('SentryBrowser', () => {
145148
getCurrentHub().pushScope();
146149
getCurrentHub().bindClient(
147150
new BrowserClient({
148-
afterSend: (event: SentryEvent) => {
151+
beforeSend: (event: SentryEvent) => {
149152
expect(event.message).to.equal('event');
150153
expect(event.exception).to.be.undefined;
151154
done();
155+
return event;
152156
},
153157
dsn,
154158
}),

0 commit comments

Comments
 (0)