Last active
June 9, 2016 08:37
-
-
Save friedemannsommer/a0d7c77238e38479bd0f254d8e0e3b6f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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