-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBacktraceClient.ts
More file actions
120 lines (111 loc) · 4.31 KB
/
BacktraceClient.ts
File metadata and controls
120 lines (111 loc) · 4.31 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import {
BacktraceCoreClient,
BacktraceReport,
DebugIdContainer,
VariableDebugIdMapProvider,
} from '@backtrace/sdk-core';
import { AGENT } from './agentDefinition.js';
import { BacktraceBrowserRequestHandler } from './BacktraceBrowserRequestHandler.js';
import { BacktraceBrowserSessionProvider } from './BacktraceBrowserSessionProvider.js';
import { BacktraceConfiguration } from './BacktraceConfiguration.js';
import { BacktraceClientBuilder } from './builder/BacktraceClientBuilder.js';
import { BacktraceClientSetup } from './builder/BacktraceClientSetup.js';
import { getStackTraceConverter } from './converters/getStackTraceConverter.js';
import { BrowserBacktraceStorage } from './storage/BrowserBacktraceStorage.js';
export class BacktraceClient<O extends BacktraceConfiguration = BacktraceConfiguration> extends BacktraceCoreClient<O> {
private readonly _disposeController: AbortController = new AbortController();
constructor(clientSetup: BacktraceClientSetup<O>) {
super({
sdkOptions: AGENT,
stackTraceConverter: getStackTraceConverter(),
requestHandler: new BacktraceBrowserRequestHandler(clientSetup.options),
debugIdMapProvider: new VariableDebugIdMapProvider(window as DebugIdContainer),
sessionProvider: new BacktraceBrowserSessionProvider(),
...clientSetup,
database: {
storage: new BrowserBacktraceStorage(),
...clientSetup.database,
},
});
this.captureUnhandledErrors(
clientSetup.options.captureUnhandledErrors,
clientSetup.options.captureUnhandledPromiseRejections,
);
}
public static builder(options: BacktraceConfiguration): BacktraceClientBuilder {
return new BacktraceClientBuilder({ options });
}
/**
* Initializes the client. If the client already exists, the available instance
* will be returned and all other options will be ignored.
* @param options client configuration
* @param build builder
* @returns backtrace client
*/
public static initialize(
options: BacktraceConfiguration,
build?: (builder: BacktraceClientBuilder) => void,
): BacktraceClient {
if (this.instance) {
return this.instance;
}
const builder = this.builder(options);
build && build(builder);
this._instance = builder.build();
return this._instance as BacktraceClient;
}
/**
* Returns created BacktraceClient instance if the instance exists.
* Otherwise undefined.
*/
public static get instance(): BacktraceClient | undefined {
return this._instance as BacktraceClient;
}
/**
* Disposes the client and all client callbacks
*/
public dispose(): void {
this._disposeController.abort();
super.dispose();
BacktraceClient._instance = undefined;
}
private captureUnhandledErrors(captureUnhandledExceptions = true, captureUnhandledRejections = true) {
if (captureUnhandledExceptions) {
window.addEventListener(
'error',
(errorEvent: ErrorEvent) => {
this.send(
new BacktraceReport(errorEvent.error ?? errorEvent.message, {
'error.type': 'Unhandled exception',
}),
);
},
{
signal: this._disposeController.signal,
},
);
}
if (captureUnhandledRejections) {
window.addEventListener(
'unhandledrejection',
(errorEvent: PromiseRejectionEvent) => {
this.send(
new BacktraceReport(
errorEvent.reason,
{
'error.type': 'Unhandled exception',
},
[],
{
classifiers: ['UnhandledPromiseRejection'],
},
),
);
},
{
signal: this._disposeController.signal,
},
);
}
}
}