I have implemented client side exception logging using window.onerror
, where I retrieve the current error and stack trace and send it to the server using AJAX
window.onerror = function(message, url, line) {
var stackTrace = printStackTrace(); //get stack trace
//send message, url, line and stackTrace to the server using an ajax call
}
where printStackTrace
is a function provided by this library: http://stacktracejs.com/
The problem is that in production all JavaScript files are minified so the stack trace and the line number are not really helpful as all errors are being reported on Line 1 in the file which is normal as the minified version contains a single line of code. For example:
Message: Object doesn't support property or method 'indexOf'
URL: http://[server]/[site]/content/combined/combined.635EE367354E6DF721593CAC56FECF95.min.js
Line: 1
Can this be improved using source maps or does that work only when Developer Tools is active ?
What I would like is to get the full stack trace using the source maps (or at least the real line number) when an error occurs for a user who doesn't have the developer tools activated/source maps enabled. Is this possible at all ?
A source map is a file that maps from the transformed source to the original source, enabling the browser to reconstruct the original source and present the reconstructed original in the debugger.
The New Debug Tool To get started, make sure your Honeybadger project's language is set to "Client-side JavaScript" under Project Settings → Edit, then navigate to Project Settings → Source Maps → Debug Tool. Follow the instructions on the page.
In a sense, source maps are the decoder ring to your secret (minified) code. Using Webpack, specifying devtool: "source-map" in your Webpack config will enable source maps, and Webpack will output a sourceMappingURL directive in your final, minified file.
You can definitely get all of the information contained in the source maps and engineer a stack trace on your own but AFAIK there isn't an elegant solution for this yet. Either way it would require ajaxing down the map file at minimum and probably the raw source file. And you will have some tough cross browser issues since not all browsers support source maps fully yet.
It looks like stack trace has this in their plan but doesn't have anybody executing on it yet: https://github.com/eriwen/javascript-stacktrace/issues/44
Get Sentry does do this as a part of their js logging utility but it's wrapped inside of a more multi purpose tool and I believe it only supports full stack trace logging in Chrome for now: https://github.com/getsentry/raven-js
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With