Skip to content
Advertisement

Stack traces that utilise source mapping

Overview:
The stack trace output in the browser console is not the same trace that is returned when Error.stack is called. The console stack trace seems to take into account sourcemaps whereas the Error.stack stack trace does not.

Console Output
Here is the default stack trace that is output to the console.

JavaScript

Error.stack Output
Here is the stack trace from Error.stack:

JavaScript

Source Code:
Here is the code that I used for this experiment:

JavaScript

Question:
Is it possible to programmatically obtain a stack trace that includes references to files & lines based on the associated sourcemap?

Edit: console.trace and new Error().stack

note: I didn’t use window.onerror for these examples, instead I wrapped the embedded JS in a try…catch and attempted to utilise these approaches within the catch. The reason for this was because the stack trace didn’t provide any trace into the embedded JS for either method when used within window.onerror.

console.trace() works the best, but of course the output can’t be captured. Even still, this does not work as expected. The output contains a stack trace that points to the console.trace() line, and little else.

JavaScript

new Error().stack does not work as expected either. It does contain a stack trace, but it’s not using the sourcemap.

JavaScript

Advertisement

Answer

Unfortunately, this is browser-specific feature is not currently supported in either Firefox or Chrome (and I am unfamiliar with its support in other browsers).

In Chrome, you can follow feature request currently being implemented at https://code.google.com/p/chromium/issues/detail?id=357958

Depending on your use case and if you have some means of capturing the sourcemaps themselves, Mozilla has an excellent sourcemapping tool that will let you map your sources/stacktraces at https://github.com/mozilla/source-map/.

Advertisement