forked from Jigsaw-Code/outline-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_reporter.ts
76 lines (70 loc) · 2.94 KB
/
error_reporter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright 2018 The Outline Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as sentry from '@sentry/browser';
import {Integration as SentryIntegration} from '@sentry/types';
export interface OutlineErrorReporter {
report(userFeedback: string, feedbackCategory: string, userEmail?: string): Promise<void>;
}
export class SentryErrorReporter implements OutlineErrorReporter {
constructor(appVersion: string, dsn: string, private tags: {[id: string]: string;}) {
if (dsn) {
sentry.init({dsn, release: appVersion, integrations: getSentryBrowserIntegrations});
}
this.setUpUnhandledRejectionListener();
}
async report(userFeedback: string, feedbackCategory: string, userEmail?: string): Promise<void> {
sentry.captureEvent(
{message: userFeedback, user: {email: userEmail}, tags: {category: feedbackCategory}});
sentry.configureScope(scope => {
scope.setUser({email: userEmail || ''});
if (this.tags) {
scope.setTags(this.tags);
}
scope.setTag('category', feedbackCategory);
});
sentry.captureMessage(userFeedback);
sentry.configureScope(scope => {
scope.clear(); // Reset the user context, don't cache the email
});
}
private setUpUnhandledRejectionListener() {
// Chrome is the only browser that supports the unhandledrejection event.
// This is fine for Android, but will not work in iOS.
const unhandledRejection = 'unhandledrejection';
window.addEventListener(unhandledRejection, (event: PromiseRejectionEvent) => {
const reason = event.reason;
const msg = reason.stack ? reason.stack : reason;
sentry.addBreadcrumb({message: msg, category: unhandledRejection});
});
}
}
// Returns a list of Sentry browser integrations that maintains the default integrations,
// but replaces the Breadcrumbs integration with a custom one that only collects console statements.
// See https://docs.sentry.io/platforms/javascript/configuration/integrations/default/
export function getSentryBrowserIntegrations(defaultIntegrations: SentryIntegration[]):
SentryIntegration[] {
const integrations = defaultIntegrations.filter(integration => {
return integration.name !== 'Breadcrumbs';
});
const breadcrumbsIntegration = new sentry.Integrations.Breadcrumbs({
console: true,
dom: false,
fetch: false,
history: false,
sentry: false,
xhr: false,
});
integrations.push(breadcrumbsIntegration);
return integrations;
}