Logging Clientside JavaScript Errors on Server

JavascriptLoggingError Handling

Javascript Problem Overview


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

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

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.

Solution 3 - Javascript

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!

Solution 4 - Javascript

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.

Solution 5 - Javascript

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?

Solution 6 - Javascript

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

Solution 7 - Javascript

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

Solution 8 - Javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMADMapView Question on Stackoverflow
Solution 1 - JavascriptJonny BuchananView Answer on Stackoverflow
Solution 2 - JavascriptZtyxView Answer on Stackoverflow
Solution 3 - JavascriptKarlView Answer on Stackoverflow
Solution 4 - JavascriptLeandro LagesView Answer on Stackoverflow
Solution 5 - JavascriptMike StoneView Answer on Stackoverflow
Solution 6 - JavascriptclegView Answer on Stackoverflow
Solution 7 - JavascriptAaron PowellView Answer on Stackoverflow
Solution 8 - JavascriptlevelnisView Answer on Stackoverflow