1

There are several libraries available for javascript error logging in a web application. These libraries sound useful during development. We can catch javascript errors at global level using widows.onerror handler.

All looks fine and dandy upto this.

But if we enable bundling and minification in ASP.NET MVC, it becomes hard to spot the actual error since minification causes renaming variables and compressing code thereby changing line numbers. How to deal with such a scenario? I've gone through many posts but couldn't find any satisfactory solution. We want the sensible javascripts errors to get logged in production environment. Any thoughts or suggestions on this?

Tejas Sutar
  • 747
  • 2
  • 11
  • 33
  • Maybe: don't use minification? If your site isn't accessed by 10,000s of *unique* (and *new*) visitors a day, then, weigh the differences and you might find having usable logging outweighs the tiny savings from minification. – freedomn-m Aug 07 '15 at 10:17
  • First of all, if you are getting JavaScript errors it means your code is incorrect. Second point why to log errors from client browser, it's not gonna affect your website... or your website is tested by consumers. Now if you really want to log something, implement tracking mechanism, and store in browser the entire flow of actions and log with error stack trace when error is present. – SilentTremor Aug 07 '15 at 12:17
  • @SilentTremor thanks for reply. Can you please suggest how to implement tracking mechanism you are speaking of? – Tejas Sutar Aug 07 '15 at 12:33

1 Answers1

1

Check out JavaScript source maps, which create a reference between your source scripts and the minified script that get's delivered with the page. It looks like someone has built an extension to the ASP Bundles that enable sourcemaps to be created.

However, even with sourcemaps the errors that are captured from window.onerror will still contain the minified names and locations. You will need to write some code in your error management system to apply sourcemaps to the code (or do it manually) to see the real numbers.

Chrome will automatically apply sourcemaps to errors in its debugger if they are publicly downloadable, but these values are not available programmatically to your error capture.

One last thing to consider is using a hosted service that already solves this problem. You can send error reports elsewhere and have them capture error information and give you reports/alerts about when an important error is happening--and even translate the error with sourcemaps. I recommend (and helped write) TrackJS.

Community
  • 1
  • 1
Todd H. Gardner
  • 630
  • 4
  • 18