Home > .NET, ASP.NET > Automatic reporting of client-side script exceptions

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
thequalityof 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 applicationmay 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 // -->
 14
 15 

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 isthe 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
isthe leader in secret management and sharing within companies and
teams.

Categories: .NET, ASP.NET
  1. http://
    December 7, 2005 at 8:46 pm

    Or use Ajax.NET… 😉 like I’m doing already for some months.

  2. http://
    December 8, 2005 at 1:54 am

    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.

  3. Jonathan Cogley
    December 8, 2005 at 5:39 am

    The nice thing with the Image approach is it is very simple and is unlikely to fail compared to a more sophisticated (and possibly more risky) technique such as Ajax.

    BTW – Have you tried our Ajax Remote Scripting implementation? http://www.thycotic.com/dotnet_remotescripting.html

  4. Arsenios
    September 3, 2007 at 2:39 am

    Nice

  5. Dmitris
    September 21, 2007 at 11:38 pm

    Sorry 😦

  6. PADAM SHARMA
    March 11, 2008 at 7:53 am

    its best

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: