Skip to content

Commit c71468d

Browse files
committed
Separate error reporting by anonymous sessions
1 parent 41ba4b8 commit c71468d

File tree

1 file changed

+49
-34
lines changed

1 file changed

+49
-34
lines changed

src/errors.ts

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as Sentry from '@sentry/browser';
2+
import * as uuid from 'uuid/v4';
23

34
import { UI_VERSION, serverVersion, desktopVersion } from './services/service-versions';
45
import { ApiError } from './services/server-api-types';
@@ -12,49 +13,63 @@ export function isSentryInitialized() {
1213
export { Sentry };
1314

1415
export function initSentry(dsn: string | undefined) {
15-
if (dsn) {
16-
Sentry.init({
17-
dsn: dsn,
18-
release: UI_VERSION,
19-
ignoreErrors: [
20-
'ResizeObserver loop limit exceeded', // No visible effect: https://stackoverflow.com/a/50387233/68051
21-
],
22-
beforeSend: function (event, hint) {
23-
const exception = hint?.originalException;
24-
if (exception instanceof ApiError) {
25-
event.fingerprint = [
26-
"{{ default }}",
27-
exception.operationName,
28-
...(exception.errorCode
29-
? [exception.errorCode.toString()]
30-
: []
31-
)
32-
];
33-
}
34-
return event;
16+
if (!dsn) return;
17+
18+
Sentry.init({
19+
dsn: dsn,
20+
release: UI_VERSION,
21+
ignoreErrors: [
22+
'ResizeObserver loop limit exceeded', // No visible effect: https://stackoverflow.com/a/50387233/68051
23+
],
24+
beforeSend: function (event, hint) {
25+
const exception = hint?.originalException;
26+
if (exception instanceof ApiError) {
27+
event.fingerprint = [
28+
"{{ default }}",
29+
exception.operationName,
30+
...(exception.errorCode
31+
? [exception.errorCode.toString()]
32+
: []
33+
)
34+
];
3535
}
36-
});
37-
sentryInitialized = true;
38-
39-
serverVersion.then((version) => addErrorTag('version:server', version));
40-
desktopVersion.then((version) => addErrorTag('version:desktop', version));
41-
42-
// If we're running in the main window (not the SW),
43-
// stop reporting errors after the page starts unloading
44-
if (typeof window !== 'undefined') {
45-
window.addEventListener('beforeunload', () => {
46-
Sentry.getCurrentHub().getClient().getOptions().enabled = false;
47-
sentryInitialized = false;
48-
});
36+
return event;
4937
}
38+
});
39+
sentryInitialized = true;
40+
41+
serverVersion.then((version) => addErrorTag('version:server', version));
42+
desktopVersion.then((version) => addErrorTag('version:desktop', version));
43+
44+
// If we're running in the main window (not the SW),
45+
// stop reporting errors after the page starts unloading
46+
if (typeof window !== 'undefined') {
47+
window.addEventListener('beforeunload', () => {
48+
Sentry.getCurrentHub().getClient().getOptions().enabled = false;
49+
sentryInitialized = false;
50+
});
5051
}
52+
53+
Sentry.configureScope((scope) => {
54+
// We use a random id to distinguish between many errors in one session vs
55+
// one error in many sessions. This isn't persisted and can't be used to
56+
// identify anybody between sessions.
57+
const randomId = uuid();
58+
scope.setUser({
59+
id: randomId,
60+
username: `anon-${randomId}`
61+
});
62+
});
5163
}
5264

5365
export function logErrorsAsUser(email: string | undefined) {
5466
if (!sentryInitialized) return;
5567

5668
Sentry.configureScope((scope) => {
57-
scope.setUser({ email: email });
69+
scope.setUser({
70+
id: email,
71+
email: email
72+
});
5873
});
5974
}
6075

0 commit comments

Comments
 (0)