forked from exceptionless/Exceptionless.JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrowserErrorPlugin.ts
More file actions
90 lines (77 loc) · 2.59 KB
/
BrowserErrorPlugin.ts
File metadata and controls
90 lines (77 loc) · 2.59 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
import {
ErrorInfo,
EventPluginContext,
IEventPlugin,
IgnoredErrorProperties,
KnownEventDataKeys,
ParameterInfo,
StackFrameInfo,
stringify,
isEmpty
} from "@exceptionless/core";
import {
fromError,
StackFrame
} from "stacktrace-js";
export class BrowserErrorPlugin implements IEventPlugin {
public priority = 30;
public name = "BrowserErrorPlugin";
public async run(context: EventPluginContext): Promise<void> {
const exception = context.eventContext.getException();
if (exception) {
context.event.type = "error";
if (context.event.data && !context.event.data[KnownEventDataKeys.Error]) {
const result = await this.parse(exception);
if (result) {
const exclusions = context.client.config.dataExclusions.concat(IgnoredErrorProperties);
const additionalData = JSON.parse(stringify(exception, exclusions)) as unknown;
if (!isEmpty(additionalData)) {
if (!result.data) {
result.data = {};
}
result.data["@ext"] = additionalData;
}
context.event.data[KnownEventDataKeys.Error] = result;
}
}
}
}
public async parse(exception: Error): Promise<ErrorInfo> {
function getParameters(parameters: string | string[]): ParameterInfo[] {
const params: string[] = (typeof parameters === "string" ? [parameters] : parameters) || [];
const items: ParameterInfo[] = [];
for (const param of params) {
items.push({ name: param });
}
return items;
}
function getStackFrames(stackFrames: StackFrame[]): StackFrameInfo[] {
const ANONYMOUS: string = "<anonymous>";
const frames: StackFrameInfo[] = [];
for (const frame of stackFrames) {
const fileName: string = frame.getFileName();
frames.push({
name: (frame.getFunctionName() || ANONYMOUS).replace("?", ANONYMOUS),
parameters: getParameters(frame.getArgs()),
file_name: fileName,
line_number: frame.getLineNumber() || 0,
column: frame.getColumnNumber() || 0,
data: {
is_native: frame.getIsNative() || (fileName && fileName[0] !== "/" && fileName[0] !== ".")
}
});
}
return frames;
}
const result: StackFrame[] = await fromError(exception);
if (!result) {
throw new Error("Unable to parse the exception stack trace.");
}
// TODO: Test with reference error.
return Promise.resolve({
type: exception.name || "Error",
message: exception.message,
stack_trace: getStackFrames(result || [])
});
}
}