Skip to content

Commit e1d882a

Browse files
committed
refactor: Implements global error catching
1 parent f9bc723 commit e1d882a

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

src/app/app.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ export class AppComponent implements OnInit, OnDestroy {
106106
ngOnInit() {
107107
this.errorHandler.printInitialDetails();
108108
this.errorHandler.initializeGlitchTip();
109+
this.errorHandler.initializeWindowCatching();
109110

110111
// Set meta tags on route change
111112
this.router.events

src/app/services/error-handler.service.ts

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,24 @@ export class ErrorHandlerService {
9292
this.glitchTipEnabled = this.shouldEnableGlitchTip();
9393
if (!this.glitchTipEnabled) return;
9494
const glitchTipDsn = this.envService.getGlitchTipDsn();
95-
Sentry.init({
96-
dsn: glitchTipDsn,
97-
integrations: [ Sentry.browserTracingIntegration() ],
98-
tracesSampleRate: 1.0,
99-
release: __APP_VERSION__,
100-
environment: this.envService.getEnvironmentType(),
101-
denyUrls: ['localhost'],
102-
});
95+
try {
96+
Sentry.init({
97+
dsn: glitchTipDsn,
98+
integrations: [ Sentry.browserTracingIntegration() ],
99+
tracesSampleRate: 1.0,
100+
release: __APP_VERSION__,
101+
environment: this.envService.getEnvironmentType(),
102+
denyUrls: ['localhost'],
103+
});
104+
} catch (e) {
105+
this.handleError({
106+
error: e,
107+
message: 'Unable to initialize GlitchTip. Possibly due to adblock, invalid DSN, or user\'s privacy preferences',
108+
location: 'ErrorHandlerService.initializeGlitchTip',
109+
showToast: false,
110+
});
111+
this.glitchTipEnabled = false;
112+
}
103113
}
104114

105115
/* Gets the user ID from local storage (if available) */
@@ -165,6 +175,29 @@ export class ErrorHandlerService {
165175
return JSON.parse(localStorage.getItem('DL_error_log') || '[]').reverse();
166176
}
167177

178+
public initializeWindowCatching() {
179+
if (isPlatformBrowser(this.platformId) && typeof window !== 'undefined') {
180+
window.onerror = (message, source, lineno, colno, error) => {
181+
this.handleError({
182+
message: String(message),
183+
location: `window (${source}:${lineno}:${colno})`,
184+
error,
185+
showToast: false,
186+
});
187+
};
188+
189+
window.onunhandledrejection = (event) => {
190+
this.handleError({
191+
message: 'Unhandled Promise Rejection',
192+
location: 'window.onunhandledrejection',
193+
error: event.reason,
194+
showToast: false,
195+
});
196+
};
197+
}
198+
199+
}
200+
168201
public printInitialDetails(): void {
169202
if (!isPlatformBrowser(this.platformId)) return;
170203
const environment = this.envService.getEnvironmentType();

0 commit comments

Comments
 (0)