I would like to report stack traces from uncaught exceptions in my JavaScript app, but the problem is that the included JavaScript is a Browserify bundle. This means that when I obtain the stack of an exception, it refers to positions within the bundle file, even if the JavaScript bundle contains source maps!
How can I translate the file positions within the stack to the original source files? I guess it involves some use of source maps?
Below is an example program that prints the stack trace of an exception:
index.html
<script src="/bundle.js"></script>
index.js
window.onerror = (message, url, line, column, error) => {
console.log(`An exception was caught for URL ${url}, line ${line}:`, error.stack)
}
const thrower = () => {
throw new Error(`Catch me if you can`)
}
const callingThrower = () => {
thrower()
}
callingThrower()
Generate the Bundle
# The --debug flag enables inline source maps
browserify --debug -o bundle.js index.js
Program Output
An exception was caught for URL http://localhost:8000/bundle.js, line 7: Error: Catch me if you can
at thrower (http://localhost:8000/bundle.js:7:9)
at callingThrower (http://localhost:8000/bundle.js:11:3)
at Object.1 (http://localhost:8000/bundle.js:14:1)
at s (http://localhost:8000/bundle.js:1:254)
at e (http://localhost:8000/bundle.js:1:425)
at http://localhost:8000/bundle.js:1:443
Browsers
I've tested with Chrome and Firefox on OS X.