8000 browser: GlobalHandlers integration · lordnox/sentry-javascript@bcf6f15 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Jan 8, 2025. It is now read-only.

Commit bcf6f15

Browse files
committed
browser: GlobalHandlers integration
1 parent e18e699 commit bcf6f15

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { captureEvent } from '@sentry/minimal';
2+
import { Integration, SentryEvent } from '@sentry/types';
3+
import { eventFromStacktrace } from '../parsers';
4+
import {
5+
installGlobalHandler,
6+
installGlobalUnhandledRejectionHandler,
7+
StackTrace as TraceKitStackTrace,
8+
subscribe,
9+
} from '../tracekit';
10+
11+
/** Global handlers */
12+
export class GlobalHandlers implements Integration {
13+
/**
14+
* @inheritDoc
15+
*/
16+
public name: string = 'GlobalHandlers';
17+
/**
18+
* @inheritDoc
19+
*/
20+
public install(
21+
options: {
22+
onerror: boolean;
23+
onunhandledpromiserejection: boolean;
24+
} = {
25+
onerror: true,
26+
onunhandledpromiserejection: true,
27+
},
28+
): void {
29+
if (options.onerror) {
30+
installGlobalHandler();
31+
}
32+
33+
if (options.onunhandledpromiserejection) {
34+
installGlobalUnhandledRejectionHandler();
35+
}
36+
37+
subscribe((stack: TraceKitStackTrace) =>
38+
captureEvent(this.eventFromGlobalHandler(stack)),
39+
);
40+
}
41+
42+
/** TODO */
43+
private eventFromGlobalHandler(stacktrace: TraceKitStackTrace): SentryEvent {
44+
const event = eventFromStacktrace(stacktrace);
45+
// TODO: Make a distinction between 'onunhandledrejection' and 'onerror'
46+
return {
47+
...event,
48+
exception: {
49+
...event.exception,
50+
mechanism: {
51+
handled: false,
52+
type: 'onerror',
53+
},
54+
},
55+
};
56+
}
57+
}

packages/browser/src/sdk.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import { initAndBind } from '@sentry/core';
22
import { BrowserOptions } from './backend';
33
import { BrowserClient } from './client';
4-
import { Breadcrumbs, FunctionToString, OnError, OnUnhandledRejection, SDKInformation, TryCatch } from './integrations';
4+
import { Breadcrumbs, FunctionToString, GlobalHandlers, SDKInformation, TryCatch } from './integrations';
55

66
export const defaultIntegrations = [
7-
new Breadcrumbs(),
87
new FunctionToString(),
9-
new OnError(),
10-
new OnUnhandledRejection(),
11-
new SDKInformation(),
128
new TryCatch(),
9+
new Breadcrumbs(),
10+
new SDKInformation(),
11+
new GlobalHandlers(),
1312
];
1413

1514
/**

0 commit comments

Comments
 (0)
0