forked from exceptionless/Exceptionless.JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorPlugin.ts
More file actions
121 lines (101 loc) · 3.75 KB
/
ErrorPlugin.ts
File metadata and controls
121 lines (101 loc) · 3.75 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
/// <reference path="../../references.ts" />
module Exceptionless {
export class ErrorPlugin implements IEventPlugin {
public priority:number = 30;
public name:string = 'ErrorPlugin';
run(context:Exceptionless.EventPluginContext): Promise<any> {
var exception = context.contextData.getException();
if (exception == null) {
return Promise.resolve();
}
if (!context.event.data) {
context.event.data = {};
}
context.event.type = 'error';
if (!!context.event.data['@error']) {
return Promise.resolve();
}
return StackTrace.fromError(exception).then(
(stackFrames: StackTrace.StackFrame[]) => this.processError(context, exception, stackFrames),
() => this.onParseError(context)
);
}
private processError(context:Exceptionless.EventPluginContext, exception:Error, stackFrames: StackTrace.StackFrame[]): Promise<any> {
var error:IError = {
message: exception.message,
stack_trace: this.getStackFrames(context, stackFrames || [])
};
context.event.data['@error'] = error;
return Promise.resolve();
}
private onParseError(context:Exceptionless.EventPluginContext): Promise<any> {
context.cancel = true;
return Promise.reject(new Error('Unable to parse the exceptions stack trace. This exception will be discarded.'))
}
private getStackFrames(context:Exceptionless.EventPluginContext, stackFrames:StackTrace.StackFrame[]): IStackFrame[] {
var frames:IStackFrame[] = [];
for (var index = 0; index < stackFrames.length; index++) {
frames.push({
name: stackFrames[index].functionName,
//parameters: stackFrames[index].args, // TODO: need to verify arguments.
file_name: stackFrames[index].fileName,
line_number: stackFrames[index].lineNumber,
column: stackFrames[index].columnNumber
});
}
return frames;
}
}
}
declare module StackTrace {
interface StackTraceOptions {
filter?: (stackFrame:StackFrame) => boolean;
sourceCache?: { URL:string };
offline?: boolean;
}
interface StackFrame {
constructor(functionName:string, args:any, fileName:string, lineNumber:number, columnNumber:number);
functionName?:string;
args?:any;
fileName?:string;
lineNumber?:number;
columnNumber?:number;
toString():string;
}
/**
* Get a backtrace from invocation point.
* @param options Options Object
* @return Array[StackFrame]
*/
function get(options: StackTraceOptions): Promise<StackFrame[]>;
/**
* Given an error object, parse it.
* @param error Error object
* @param options Object for options
* @return Array[StackFrame]
*/
function fromError(error:Error, options?:StackTraceOptions): Promise<StackFrame[]>;
/**
* Use StackGenerator to generate a backtrace.
* @param options Object options
* @returns Array[StackFrame]
*/
function generateArtificially(options: StackTraceOptions): Promise<StackFrame[]>;
/**
* Given a function, wrap it such that invocations trigger a callback that
* is called with a stack trace.
*
* @param {Function} fn to be instrumented
* @param {Function} callback function to call with a stack trace on invocation
* @param {Function} errorCallback optional function to call with error if unable to get stack trace.
* @param {Object} thisArg optional context object (e.g. window)
*/
function instrument(fn:() => void, callback:(stackFrames:StackFrame[]) => void, errorCallback:() => void, thisArg:any): void;
/**
* Given a function that has been instrumented,
* revert the function to it's original (non-instrumented) state.
*
* @param fn {Function}
*/
function deinstrument(fn:() => void): void;
}