Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do JavaScript source maps work when catching errors?

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 ?

like image 389
SzilardD Avatar asked Oct 11 '13 06:10

SzilardD


People also ask

What does generate source map do?

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.

How do you debug a source map?

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.

What is source map Webpack?

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.


1 Answers

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

like image 101
chrisst Avatar answered Oct 22 '22 22:10

chrisst