Logging Clientside JavaScript Errors on Server-ThrowExceptions

Exception or error:

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)?

How to solve:

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);
};

###

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.

###

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!

###

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.

###

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

###

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 😉

###

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

###

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?

Leave a Reply

Your email address will not be published. Required fields are marked *