Merge pull request #8780 from delphinus35/feature/update-stacktrace-js

Update definition for stacktrace-js
This commit is contained in:
Mohamed Hegazy
2016-06-26 15:30:21 -07:00
committed by GitHub
2 changed files with 60 additions and 28 deletions
+24 -12
View File
@@ -1,24 +1,36 @@
/// <reference path="stacktrace-js.d.ts" />
function interestingFn() {
function interestingFn(): string {
return 'https://github.com/exceptionless/Exceptionless';
}
var callback = function(stackframes:StackTrace.StackFrame[]) {
var stringifiedStack = stackframes.map(function(sf:StackTrace.StackFrame) {
const callback = (stackframes: StackTrace.StackFrame[]) => {
const stringifiedStack = stackframes.map((sf: StackTrace.StackFrame): string => {
console.log(sf.functionName);
console.log(sf.args);
console.log(sf.fileName);
console.log(sf.lineNumber);
console.log(sf.columnNumber);
console.log(sf.source);
console.log(sf.isEval);
console.log(sf.isNative);
return sf.toString();
}).join('\n');
console.log(stringifiedStack);
};
var errorCallback = function(err:Error) { console.log(err.message); };
const errorCallback = (err: Error) => console.log(err.message);
const logger = (stackframes: StackTrace.StackFrame[]) => console.log(stackframes);
const options: StackTrace.StackTraceOptions = {
filter: (stackframe: StackTrace.StackFrame) => true,
sourceCache: {},
offline: false
};
const error = new Error('BOOM!');
StackTrace.get();
StackTrace.get(options).then(logger);
StackTrace.fromError(error, options).then(logger);
StackTrace.generateArtificially(options).then(logger);
// Somewhere else...
var error = new Error('BOOM!');
StackTrace.fromError(error);
StackTrace.generateArtificially();
StackTrace.instrument(interestingFn, callback, errorCallback);
StackTrace.deinstrument(interestingFn);
const instrumented: () => string = StackTrace.instrument(interestingFn, callback, errorCallback);
const original: () => string = StackTrace.deinstrument(interestingFn);
+36 -16
View File
@@ -4,21 +4,35 @@
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare namespace StackTrace {
export interface SourceCache {
[key: string]: string | Promise<string>;
}
/**
* Options for StackTrace
* @param filter Function(StackFrame => Boolean) - Only include stack entries matching for which filter returns true
* @param sourceCache Object (String URL => String Source) - Pre-populate source cache to avoid network requests
* @param offline Boolean (default: false) - Set to true to prevent all network requests
*/
export interface StackTraceOptions {
filter?: (stackFrame:StackFrame) => boolean;
sourceCache?: { URL:string };
offline?: boolean;
filter?: (stackFrame: StackFrame) => boolean;
sourceCache?: SourceCache;
offline?: boolean;
}
export interface StackFrame {
constructor(functionName:string, args:any, fileName:string, lineNumber:number, columnNumber:number): StackFrame;
constructor(functionName: string, args: any, fileName: string, lineNumber: number, columnNumber: number): StackFrame;
functionName?:string;
args?:any;
fileName?:string;
lineNumber?:number;
columnNumber?:number;
toString():string;
functionName: string;
args: any;
fileName: string;
lineNumber: number;
columnNumber: number;
source: string;
isEval: boolean;
isNative: boolean;
toString(): string;
}
/**
@@ -34,7 +48,7 @@ declare namespace StackTrace {
* @param options Object for options
* @return Array[StackFrame]
*/
export function fromError(error:Error, options?:StackTraceOptions): Promise<StackFrame[]>;
export function fromError(error: Error, options?: StackTraceOptions): Promise<StackFrame[]>;
/**
* Use StackGenerator to generate a backtrace.
@@ -49,25 +63,31 @@ declare namespace StackTrace {
*
* @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 {Function} errback optional function to call with error if unable to get stack trace.
* @param {Object} thisArg optional context object (e.g. window)
* @return {Function} instrumented function
*/
export function instrument(fn:() => void, callback:(stackFrames:StackFrame[]) => void, errorCallback:(error:Error) => void, thisArg?:any): void;
export function instrument<TFunc extends Function>(fn: TFunc, callback: (stackFrames: StackFrame[]) => void, errback?: (error: Error) => void, thisArg?: any): TFunc;
/**
* Given a function that has been instrumented,
* revert the function to it's original (non-instrumented) state.
*
* @param fn {Function}
* @return {Function} original function
*/
export function deinstrument(fn:() => void): void;
export function deinstrument<TFunc extends Function>(fn: TFunc): TFunc;
/**
* Given an Array of StackFrames, serialize and POST to given URL.
*
* @param stackframes - Array[StackFrame]
* @param url - URL as String
* @return Promise<any>
* @return Promise<string>
*/
export function report(stackframes: StackFrame[], url: string): Promise<any>;
export function report(stackframes: StackFrame[], url: string): Promise<string>;
}
declare module "stacktrace-js" {
export = StackTrace;
}