-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.ts
More file actions
171 lines (149 loc) · 5.25 KB
/
index.ts
File metadata and controls
171 lines (149 loc) · 5.25 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { BacktraceClient, BacktraceStringAttachment, createBacktraceReduxMiddleware } from '@backtrace/browser';
import { BacktraceSessionReplayModule } from '@backtrace/session-replay';
import { configureStore, createSlice } from '@reduxjs/toolkit';
import { SUBMISSION_URL } from './consts';
const client = BacktraceClient.builder({
url: SUBMISSION_URL,
name: '@backtrace/browser-example',
version: '0.0.1',
userAttributes: {
'custom-attribute': 'test',
'custom-annotation': {
prop1: true,
prop2: 123,
},
},
database: {
enable: true,
},
})
.useModule(
new BacktraceSessionReplayModule({
maxEventCount: 100,
}),
)
.build();
interface DemoState {
count: number;
}
const initialState: DemoState = {
count: 0,
};
const counterSlice = createSlice({
name: 'Redux-Demo',
initialState,
reducers: {
setCount(state, action) {
state.count = action.payload;
},
},
});
const { setCount } = counterSlice.actions;
const store = configureStore({
reducer: {
counter: counterSlice.reducer,
},
middleware: (getDefaultMiddleware) => {
const backtraceMiddleware = createBacktraceReduxMiddleware(client);
return getDefaultMiddleware().concat(backtraceMiddleware);
},
});
function getCount() {
return store.getState().counter.count;
}
function parseNotExistingDomElement(): string {
const element = document.getElementById('not-existing-id') as HTMLElement;
return element.outerText.split('\n')[1].toString();
}
const sendErrorButton = document.getElementById('send-error') as HTMLElement;
const sendMessageButton = document.getElementById('send-message') as HTMLElement;
const sendUnhandledExceptionButton = document.getElementById('send-unhandled-exception') as HTMLElement;
const sendPromiseRejectionButton = document.getElementById('send-promise-rejection') as HTMLElement;
const generateMetricButton = document.getElementById('generate-metric') as HTMLElement;
const sendMetricsButton = document.getElementById('send-metrics') as HTMLElement;
const reduxDemoButton = document.getElementById('redux-demo') as HTMLElement;
const incrementCounterButton = document.getElementById('increment') as HTMLElement;
const decrementCounterButton = document.getElementById('decrement') as HTMLElement;
const clearCounterButton = document.getElementById('clear') as HTMLElement;
const sendReduxErrorButton = document.getElementById('send-redux-error') as HTMLElement;
async function sendHandledException() {
try {
console.log('send-error click');
parseNotExistingDomElement();
} catch (err) {
await client.send(err as Error, { action: 'send-error' }, [
new BacktraceStringAttachment('test.txt', 'sample attachment content'),
]);
}
}
async function sendMessage() {
console.log('send-message click');
await client.send('test-message', { action: 'send-message' }, [
new BacktraceStringAttachment('test.txt', 'sample attachment content'),
]);
}
function generateMetric() {
console.log('generate-metric click');
if (!client.metrics) {
console.log('metrics are unavailable');
return;
}
client.metrics.addSummedEvent('click');
}
function sendMetrics() {
console.log('send-metrics click');
if (!client.metrics) {
console.log('metrics are unavailable');
return;
}
client.metrics.send();
}
function unhandledPromiseRejection() {
console.log('unhandled promise rejection');
return new Promise(() => {
throw new Error('Promise rejection');
});
}
function unhandledException() {
console.log('unhandled exception');
throw new Error('unhandled exception');
}
function handleReduxButtonClick() {
const reduxDemoContainer = document.getElementById('redux-demo-container') as HTMLElement;
const mainButtonsContainer = document.getElementById('main-buttons-container') as HTMLElement;
reduxDemoContainer.style.display = 'block';
mainButtonsContainer.style.display = 'none';
}
function repaintCount() {
const countEl = document.getElementById('count') as HTMLElement;
const count = store.getState().counter.count;
countEl.innerText = String(count);
}
function handleIncrementClick() {
console.log('increment counter click');
const count = getCount();
store.dispatch(setCount(count + 1));
repaintCount();
}
function handleDecrementClick() {
console.log('decrement counter click');
const count = getCount();
store.dispatch(setCount(count - 1));
repaintCount();
}
function handleClearClick() {
console.log('clear counter click');
store.dispatch(setCount(0));
repaintCount();
}
sendErrorButton.onclick = sendHandledException;
sendMessageButton.onclick = sendMessage;
generateMetricButton.onclick = generateMetric;
sendMetricsButton.onclick = sendMetrics;
sendUnhandledExceptionButton.onclick = unhandledException;
sendPromiseRejectionButton.onclick = unhandledPromiseRejection;
reduxDemoButton.onclick = handleReduxButtonClick;
incrementCounterButton.onclick = handleIncrementClick;
decrementCounterButton.onclick = handleDecrementClick;
clearCounterButton.onclick = handleClearClick;
sendReduxErrorButton.onclick = sendHandledException;