Automatic reporting of client-side script exceptions

Error reporting is enormously powerful.  Knowing when your customers are seeing errors is the first step to improving the quality of your application.

In my previous post, I mentioned capturing client-side script exceptions while discussing a dreaded hot fix that had to be made.  You may have been wondering exactly what I was talking about and if it might be useful for your applications?

Hopefully you have already tapped into the Application_Error method in global.asax.cs to publish exceptions (maybe to a log file or as email).  We tend to prefer getting exceptions in email because it is a strong incentive to the development team to fix the problem. :-)  However, Application_Error only catches server side exceptions which means that many of the different browsers hitting your application may be choking on your client-side script and you wouldn't even know!  You may be thinking that you can stop reading now because your application is intranet-based and everyone is using Internet Explorer ... not so! - different versions of Internet Explorer can behave very differently with the same Javascript. 

Our solution is to tap into the onerror event on the window.  You can have your own custom Javascript function fire anytime an error occurs in client-side script.  Now you need call back to the server and cause an Application_Error call by throwing an exception on the server side - this then allows your regular exception publishing to kick in.  We do this in the function below by calling back to the ASPX page using a temporary Image with specific values in the QueryString (onerror_message) - our base page class (you really should have your own PageBase derived from System.Web.UI.Page but that is another discussion) then checks for these messages and throws an exception if they are present.

  1 <script language="javascript">
  2 // <!--
  3 // onerror
  4 window.onerror = window_onerror;
  5 function window_onerror(message,url,lineNumber) {
  6 	var queryString = '?onerror_message=' + escape(message) 
  7 		+ '&onerror_url=' + escape(url) 
  8 		+ '&onerror_lineNumber=' + lineNumber;
  9 	var onerrorImage = new Image();
 10 	onerrorImage.src = queryString;
 11 	return true;
 12 }
 13 // -->
 15 </script>

Please note that the user nevers sees the exception message so it is silently reporting the errors without disrupting the user experience.

Tip:  You can test your page for client-side script exceptions by going to your page and then replacing the URL (in the browser window location textbox) with javascript:CallABogusFunction().  This will make it call back to the server but nothing will be apparent on the screen.  You can also test your page directly by adding ?onerror_message=xyz in the QueryString.

Long live reporting *ALL* your exceptions!

Jonathan Cogley is the CEO and founder of thycotic, a .NET consulting company and ISV in Washington DC.  thycotic has just released Thycotic Secret Server which is a secure web-based solution to both "Where is my Hotmail password?" and "Who has the password for our domain name?".  Secret Server is the leader in secret management and sharing within companies and teams.


  • Or use Ajax.NET... ;) like I'm doing already for some months.

  • I've been doing this for some time. It really lets you know how crappy Internet Explore can be some times. You can get a lot of false positives.

    For instance.

    window.moveTo sometimes cause 'Access denied'.

    I could go on and on.

Comments have been disabled for this content.