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
55 lines (46 loc) · 1.56 KB
/
NodeErrorParser.ts
File metadata and controls
55 lines (46 loc) · 1.56 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
import { parse as fromError } from 'stack-trace'
import {
EventPluginContext,
IError,
IErrorParser,
IParameter,
IStackFrame
} from '@exceptionless/core';
export class NodeErrorParser implements IErrorParser {
public parse(context: EventPluginContext, exception: Error): Promise<IError> {
function getParameters(parameters: string | string[]): IParameter[] {
const params: string[] = (typeof parameters === 'string' ? [parameters] : parameters) || [];
const items: IParameter[] = [];
for (const param of params) {
items.push({ name: param });
}
return items;
}
function getStackFrames(stackFrames: any[]): IStackFrame[] {
const frames: IStackFrame[] = [];
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 || [])
});
}
}