107

Im running a ASP.NET Site where I have problems to find some JavaScript Errors just with manual testing.

Is there a possibility to catch all JavaScript Errors on the Clientside and log them on the Server i.e. in the EventLog (via Webservice or something like that)?

James McMahon
  • 48,506
  • 64
  • 207
  • 283
MADMap
  • 3,132
  • 3
  • 25
  • 31
  • The problem why we are not using JavaScript UnitTesting is because there are too many people contributing to the Site/Content and they are using JavaScript. The Content is nothing we (as developers) should care about, but there are mistakes in the code. So a general solution would be better. – MADMap Sep 23 '08 at 06:50
  • I assume you don't mean average user contributed (else that is XSS hole)... but... you could maybe isolate their JS in try/catch so it at least doesn't affect your own JS... without knowing the dynamic of the site, I don't know if this will help or not... – Mike Stone Sep 23 '08 at 07:06
  • The content is from other Teams in the Company, not from users so it's not an security-risk – MADMap Sep 23 '08 at 08:41
  • [Bugsnag](https://www.bugsnag.com/platforms/javascript/) will automatically log your errors & show them in a dashboard. Works for .NET & JS. – Don P Dec 06 '17 at 20:59

8 Answers8

74

You could try setting up your own handler for the onerror event and use XMLHttpRequest to tell the server what went wrong, however since it's not part of any specification, support is somewhat flaky.

Here's an example from Using XMLHttpRequest to log JavaScript errors:

window.onerror = function(msg, url, line)
{
  var req = new XMLHttpRequest();
  var params = "msg=" + encodeURIComponent(msg) + '&url=' + encodeURIComponent(url) + "&line=" + line;
  req.open("POST", "/scripts/logerror.php");
  req.send(params);
};
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
Jonny Buchanan
  • 61,926
  • 17
  • 143
  • 150
  • I just released a server control that helps you do this at http://www.thecodepage.com/post/JavaScript-Error-Notifications.aspx – Gabriel McAdams Feb 02 '10 at 18:59
  • 3
    Will it loop infinitly if it crashes in the handler? – Jean-Philippe Leclerc Jan 30 '13 at 02:56
  • 3
    @Jean-PhilippeLeclerc Yes. And worse, it will DoS your server endpoint if you ever fall into an error loop. You should add a throttling function to this to prevent a client from hitting the server too fast. Here is an example of this from {Track:js} https://github.com/TrackJs/Tech-Demo/blob/master/src/TrackJs.Demo/Content/js/transmission.js – Todd H. Gardner Oct 28 '13 at 20:33
  • You can also encrypt a token that includes the user's ID, IP, a small random string (to foil known-plaintext attacks if your encryption algorithm is fast but weak), and a timestamp and include that as a JS variable in the page. This can then be submitted with the error report and checked before allowing it to be appended to the logs. This helps prove probable error message authenticity and protects from dumb DoS attacks, but still won't protect you from more sophisticated DoS attempts the way the throttling will. – mormegil Mar 06 '14 at 23:09
  • This thing can overwhelm your server e.g. if you get and error in `setInterval` method. There are many JS error reporting services available on the web. Checkout [ErrLytics](http://errlytics.com). It also gives you analytics of every user action on your website. – Vivek Marakana May 09 '16 at 04:49
  • Are there security risks by exposing an ajax call that will (probably) write to disk any message it receives? I can think of a DoD attack filling the hard drive or slowing its response... How would you protect against it? – FercoCQ Oct 10 '17 at 18:10
  • This is the fixed code: – Arkadiy Bolotov Dec 22 '21 at 09:29
  • @ArkadiyBolotov that's only going to log the first 10 errors. it's a rather simplistic way to protect from loops. It really ought to keep an array of the messages, and if it detects the same message several times in a row, to ignore/filter those. – benjaminhull Sep 23 '22 at 10:50
33

Short answer: Yes, it is possible.

Longer answer: People have already written about how you can (at least partially) solve this issue by writing your own code. However, do note that there are services out there that seems to have made sure the JS code needed works in many browsers. I've found the following:

I can't speak for any of these services as I haven't tried them yet.

Ztyx
  • 14,100
  • 15
  • 78
  • 114
  • 1
    Registered with muscula.com. Service looks very interesting and easy to integrate and use. There is also JavaScript library https://github.com/csnover/TraceKit which allows to get an exception information from client, but you have to develop your own server side loggin mechanism. I wonder if Google Analytics could be used for that – Maksym Kozlenko Sep 05 '12 at 04:15
  • 1
    http://trackjs.com as well, and it tracks what the user and network were doing before the error. – Todd H. Gardner Oct 28 '13 at 20:31
  • In addition to the list above there is also, https://log4sure.com its free and also has real-time monitoring of logs. You can also create your custom log table – Bhavin Apr 25 '15 at 17:55
  • http://www.erroralerts.com/ not found – Kiquenet Nov 23 '15 at 22:33
  • I would also add [errorception](http://www.errorception.com) – yegor256 Aug 29 '16 at 00:39
  • **exceptionhub.com** is no longer in business, the domain is for sale. – Cristian Scutaru Jul 13 '17 at 00:46
  • https://errlytics.com is also there. It also provides front end performance monitoring like page load and ajax call monitoring. – Vivek Marakana Sep 06 '17 at 07:13
  • 2
    [jsnlog.com](http://jsnlog.com/) is free and [open source](https://github.com/mperdeck/jsnlog). – stomy May 28 '19 at 15:27
12

I have just implemented server side error logging on javascript errors on a project at work. There is a mixture of legacy code and new code using jQuery.

I use a combination of window.onerror and wrapping the jQuery event handlers and onready function with an error handling function (see: JavaScript Error Tracking: Why window.onerror Is Not Enough).

  • window.onerror: catches all errrors in IE (and most errors in Firefox), but does nothing in Safari and Opera.
  • jQuery event handlers: catches jQuery event errors in all browsers.
  • jQuery ready function: catches initialisation errors in all browsers.

Once I have caught the error, I add some extra properties to it (url, browser, etc) and then post it back to the server using an ajax call.

On the server I have a small page which just takes the posted arguments and outputs them to our normal server logging framework.

I would like to open source the code for this (as a jQuery plugin). If anyone is interested let me know, it would help to convince the bosses!

Justin Tanner
  • 14,062
  • 17
  • 82
  • 103
Karl
  • 1,615
  • 1
  • 14
  • 20
  • 1
    Any chance that this code will be made available? – Luke Sep 09 '10 at 10:49
  • Unfortunately not :( as I am no longer working for the company where I created that code. But it was only about 100 lines of code, and should be reasonably easy to recreate from the details above. – Karl Sep 14 '10 at 08:58
  • 1
    it's right here guys: http://webcache.googleusercontent.com/search?q=cache:tR3jJqlFUjoJ:blogs.cozi.com/techold/2008/04/javascript-erro.html+tech+2008+javascript+erro+site:blogs.cozi.com&cd=1&hl=en&ct=clnk&gl=us&source=www.google.com – Devin Rhode Aug 07 '11 at 06:22
  • I have tried that, but keeping jquery in the depencdency is a pain and window.onerror sucks for minified code and javascript that is served via cdns – Ankur Agarwal Aug 28 '13 at 13:52
2

If you use Google Analytics, you can log javascript errors into Google Analytics Events.

See this app: http://siteapps.com/app/log_javascript_errors_with_ga-181

Hope it helps.

  • 4
    That link does not work any more. You can find an article about logging Javascript errors with Google Analytics at https://developers.google.com/analytics/devguides/collection/analyticsjs/exceptions – Martin Omander Jun 14 '18 at 10:11
0

You could potentially make an Ajax call to the server from a try/catch, but that's probably about the best you can do.

May I suggest JavaScript unit testing instead? Possibly with JSUnit?

Rob W
  • 341,306
  • 83
  • 791
  • 678
Mike Stone
  • 44,224
  • 30
  • 113
  • 140
  • The problem why we are not using JavaScript UnitTesting is because there are too many people contributing to the Site/Content and they are using JavaScript (but don't have a clue about it!) so a general solution would be better. – MADMap Sep 23 '08 at 06:48
0

Also I recomend using TraceTool utility, it comes with JavaScript support and is very handy for JS monitoring.

cleg
  • 4,862
  • 5
  • 35
  • 52
0

If you're wanting to log the client-side errors back to the server you're going to have to do some kind of server processing. Best bet would be to have a web service which you can access via JavaScript (AJAX) and you pass your error log info to it.

Doesn't 100% solve the problem cuz if the problem is with the web server hosting the web service you're in trouble, you're other option would be to send the info via a standard page via a query string, One method of doing that is via dynamically generating Image tags (which are then removed) as the browser will try and load the source of an image. It gets around cross-domain JavaScript calls nicely though. Keep in mind that you're in trouble if someone has images turned off ;)

Aaron Powell
  • 24,927
  • 18
  • 98
  • 150
0

I've been using Appfail recently, which captures both asp.net and JavaScript errors

levelnis
  • 7,665
  • 6
  • 37
  • 61