-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcatcher.ts
More file actions
243 lines (207 loc) · 5.93 KB
/
catcher.ts
File metadata and controls
243 lines (207 loc) · 5.93 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import { AffectedUser, EventContext, Json } from "@hawk.so/types";
import { validateContext, validateUser } from "./utils/validation";
import { Logger, LogType } from "./utils/log";
import { HawkInitialSettings } from "./types/base-hawk-settings";
import { HawkJavaScriptEvent } from "./types/event";
import Sanitizer from "./modules/sanitizer";
import { Transport } from "./types/transport";
import { UserManager } from "./types/user-manager";
import { isErrorProcessed, markErrorAsProcessed } from "./utils/event";
import { CatcherMessage } from "./types/catcher-message";
import log from "@hawk.so/javascript/dist/utils/log";
import { EventRejectedError } from "./errors";
export abstract class BaseCatcher {
/**
* Catcher Type
*/
protected readonly type: string;
/**
* Current bundle version
*/
private readonly release: string | undefined;
/**
* Any additional data passed by user for sending with all messages
*/
private context: EventContext | undefined;
/**
* Transport for dialog between Catcher and Collector
* (WebSocket decorator by default, or custom via settings.transport)
*/
private readonly transport: Transport;
/**
* Current authenticated user manager
*/
private readonly userManager: UserManager;
/**
* Logger function
*/
private readonly log: Logger;
constructor(
settings: HawkInitialSettings,
) {
this.release = settings.release;
this.userManager = settings.userManager;
this.log = (settings.logger ?? (() => {
})) as Logger;
}
/**
* Send test event from client
*/
public test(): void {
const fakeEvent = new Error('Hawk JavaScript Catcher test message.');
this.send(fakeEvent);
}
/**
* Public method for manual sending messages to the Hawk
* Can be called in user's try-catch blocks or by other custom logic
*
* @param message - what to send
* @param [context] - any additional data to send
*/
public send(message: Error | string, context?: EventContext): void {
void this.formatAndSend(message, context);
}
/**
* Format and send an error
*
* @param error - error to send
* @param integrationAddons - addons spoiled by Integration
* @param context - any additional data passed by user
*/
protected async formatAndSend(
error: Error | string,
context?: EventContext
): Promise<void> {
try {
const isAlreadySentError = isErrorProcessed(error);
if (isAlreadySentError) {
/**
* @todo add debug build and log this case
*/
return;
} else {
markErrorAsProcessed(error, this.log);
}
const errorFormatted = await this.prepareErrorFormatted(error, context);
this.sendErrorFormatted(errorFormatted);
} catch (e) {
if (e instanceof EventRejectedError) {
/**
* Event was rejected by user
*/
return;
}
log('Unable to send error. Seems like it is Hawk internal bug. Please, report it here: https://github.com/codex-team/hawk.javascript/issues/new', 'warn', e);
}
}
/**
* Sends formatted HawkEvent to the Collector
*
* @param errorFormatted - formatted error to send
*/
private sendErrorFormatted(errorFormatted: CatcherMessage): void {
this.transport.send(errorFormatted)
.catch((sendingError) => {
log('Transport sending error', 'error', sendingError);
});
}
/**
* Formats the event
*
* @param error - error to format
* @param context - any additional data passed by user
*/
abstract async prepareErrorFormatted(error: Error | string, context?: EventContext): Promise<CatcherMessage>
/**
* Update the current user information
*
* @param user - New user information
*/
public setUser(user: AffectedUser): void {
if (!validateUser(user, this.log)) {
return;
}
this.userManager.setUser(user);
}
/**
* Update the context data that will be sent with all events
*
* @param context - New context data
*/
public setContext(context: EventContext | undefined): void {
if (!validateContext(context, this.log)) {
return;
}
this.context = context;
}
/**
* Return event type: TypeError, ReferenceError etc
*
* @param error - caught error
*/
protected getType(error: Error | string): HawkJavaScriptEvent['type'] {
if (!(error instanceof Error)) {
return null;
}
return error.name;
}
/**
* Release version
*/
protected getRelease(): HawkJavaScriptEvent['release'] {
return this.release !== undefined ? String(this.release) : null;
}
/**
* Current authenticated user
*/
protected getUser(): HawkJavaScriptEvent['user'] {
return this.userManager.getUser();
}
/**
* Collects additional information
*
* @param context - any additional data passed by user
*/
protected getContext(context?: EventContext): HawkJavaScriptEvent['context'] {
const contextMerged = {};
if (this.context !== undefined) {
Object.assign(contextMerged, this.context);
}
if (context !== undefined) {
Object.assign(contextMerged, context);
}
return Sanitizer.sanitize(contextMerged);
}
/**
* Compose raw data object
*
* @param {Error|string} error — caught error
*/
protected getRawData(error: Error | string): Json | undefined {
if (!(error instanceof Error)) {
return;
}
const stack = error.stack !== undefined ? error.stack : '';
return {
name: error.name,
message: error.message,
stack,
};
}
/**
* Return event title
*
* @param error - event from which to get the title
*/
protected getTitle(error: Error | string): string {
const notAnError = !(error instanceof Error);
/**
* Case when error is 'reason' of PromiseRejectionEvent
* and reject() provided with text reason instead of Error()
*/
if (notAnError) {
return error.toString() as string;
}
return (error as Error).message;
}
}