forked from exceptionless/Exceptionless.JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeErrorPlugin.ts
More file actions
73 lines (63 loc) · 2.12 KB
/
NodeErrorPlugin.ts
File metadata and controls
73 lines (63 loc) · 2.12 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
import {
IEventPlugin,
ErrorInfo,
EventPluginContext,
KnownEventDataKeys,
IgnoredErrorProperties,
isEmpty,
StackFrameInfo,
stringify
} from "@exceptionless/core";
import { parse as fromError } from "stack-trace";
export class NodeErrorPlugin implements IEventPlugin {
public priority = 30;
public name = "NodeErrorPlugin";
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;
}
}
}
}
private parse(exception: Error): Promise<ErrorInfo> {
function getStackFrames(stackFrames: any[]): StackFrameInfo[] {
const frames: StackFrameInfo[] = [];
for (const frame of stackFrames) {
frames.push({
name: frame.methodName || frame.functionName,
parameters: [], // TODO: See if there is a way to get this.
file_name: frame.fileName,
line_number: frame.lineNumber || 0,
column: frame.columnNumber || 0,
declaring_type: frame.typeName,
data: {
is_native: frame.native || (frame.fileName && frame.fileName[0] !== "/" && frame.fileName[0] !== "."),
},
});
}
return frames;
}
const result = fromError(exception);
if (!result) {
throw new Error("Unable to parse the exception stack trace.");
}
return Promise.resolve({
type: exception.name || "Error",
message: exception.message,
stack_trace: getStackFrames(result || []),
});
}
}