I’m trying to generate a stacktrace in JavaScript. I have an implementation that works very well in Chrome, but there is an issue in Firefox: native Exceptions like DOMExceptions don’t have a .stack property.
<div id="test"></div>
<script type="text/javascript">
try {
var range = document.createRange();
range.setStart(document.getElementById("test"), -1); // throws Exception
} catch (e) {
console.log(e.stack);
}
</script>
This will give Error: Index or size was negative, or greater than the allowed value. in Chrome.
at http://test/test.html:5:8
But in Firefox I get undefined – sad fais 🙁
I know about Eriwen’s stacktrace script and stracktracejs and my implementation is inspired by it. I’ll consider using the caller.callee fallback but I’d rather not since I can’t get the file and line number.
Do you know a better solution?
I could wrap suspect calls in try-catch statements but I don’t know whether a call is suspect before I get the actual error and then I will fix the code so it will never throw again.
I could catch the Exception and rethrow as a new Error(e.message) but then I lose the stacktrace as well.
I tried
e.locationbut this results in a known bug in Firefox.Error: Permission denied for <file://> to create wrapper for object of class UnnamedClass.I can access
e.filenameande.lineNumber. This is the solution I’m going with now.