forked from exceptionless/Exceptionless.JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeErrorParser.ts
More file actions
57 lines (48 loc) · 1.62 KB
/
NodeErrorParser.ts
File metadata and controls
57 lines (48 loc) · 1.62 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
import { parse as fromError } from "stack-trace";
import {
ErrorInfo,
EventPluginContext,
IErrorParser,
ParameterInfo,
StackFrameInfo,
} from "@exceptionless/core";
export class NodeErrorParser implements IErrorParser {
public parse(context: EventPluginContext, 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: any[]): StackFrameInfo[] {
const frames: StackFrameInfo[] = [];
for (const frame of stackFrames) {
frames.push({
name: frame.getMethodName() || frame.getFunctionName(),
parameters: getParameters(frame.getArgs()),
file_name: frame.getFileName(),
line_number: frame.getLineNumber() || 0,
column: frame.getColumnNumber() || 0,
declaring_type: frame.getTypeName(),
data: {
is_native: frame.isNative() ||
(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 || []),
});
}
}