Skip to content

Instantly share code, notes, and snippets.

@friedemannsommer
Last active June 9, 2016 08:37
Show Gist options
  • Save friedemannsommer/a0d7c77238e38479bd0f254d8e0e3b6f to your computer and use it in GitHub Desktop.
Save friedemannsommer/a0d7c77238e38479bd0f254d8e0e3b6f to your computer and use it in GitHub Desktop.
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