Skip to content

Instantly share code, notes, and snippets.

@friedemannsommer
Last active Jun 9, 2016
Embed
What would you like to do?
function stacktrace() {
var stack;
function trace(fn) {
return (typeof fn !== 'function')
? []
: trace(fn.caller).concat([fn.toString().split('(')[0].substring(9)]);
}
try {
throw new Error('stacktrace');
}
catch (e) {
if (typeof e.stack === 'string') {
stack = e.stack;
}
else {
var arrayStack = trace(arguments.callee.caller);
arrayStack.shift();
arrayStack.reverse();
stack = arrayStack.join('\n\t');
}
}
return stack;
}
function CustomError(message) {
this.name = 'CustomError';
this.message = message || 'Custom Error occured';
this.stack = stacktrace();
}
CustomError.prototype = Object.create(Error.prototype);
CustomError.prototype.constructor = CustomError;
function stacktrace():string {
let stack:string;
function trace(fn:Function):Array<string> {
return (typeof fn !== 'function')
? []
: trace(fn.caller).concat([fn.toString().split('(')[0].substring(9)]);
}
try {
throw new Error('stacktrace');
}
catch(e) {
if(typeof e.stack === 'string'){
stack = e.stack;
}
else {
let arrayStack:Array<string> = trace(arguments.callee.caller);
arrayStack.shift();
arrayStack.reverse();
stack = arrayStack.join('\n\t');
}
}
return stack;
}
function CustomError(message):void {
this.name = 'CustomError';
this.message = message || 'Custom Error occured';
this.stack = stacktrace();
}
CustomError.prototype = Object.create(Error.prototype);
CustomError.prototype.constructor = CustomError;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment