Monday, March 26, 2012

window.onerror and Toolkit

I am implementing logging of client-side errors to ASP.NET Health Monitoring using a technique similar to this one:

http://www.newtonsoft.com/blog/archive/2006/05/02/Logging-JavaScript-Errors-To-ASP.NET.aspx

This code adds a client-side handler to window.onerror, and posts via xmlHttp to an HttpHandler that logs the exception via Health Monitoring. To test, I have a page with a simple javascript error (<a href="http://links.10026.com/?link=http://forums.asp.net/AddPost.aspx?ForumID=1022#" onclick="throw 'test exception';">Throw error</a>) AND a Ajax Control Toolkit Calendar extender with OnClientHide set to throw a test exception.

This works in both FireFox and IE for the most part, but there are a few issues I'm having trouble getting to the bottom of:

-In IE the message is "Exception Thrown and Not caught". I would prefer to get the actual message. In FF, the message is "uncaught exception: test exception", which is more useful IMO. How can I
get the actual message in IE javascript?

-In FireFox, the "simple" example above works, but in the "Ajax Control Toolkit Calendar" example, the error is displayed in the FF error dialog, but not caught by window.onerror. It acts as if there is something intercepting the error and not causing it to fire "onerror". I've looked in the source and docs, but can't find where that would be. Any ideas how to get this working with MS Ajax + toolkit?


Have you considered using the MS AJAXsupport for exceptions. Instead of throw 'test exception' you could have Error.create('testException') and in the handler get the message.

Regarding the window.onerror event not getting fired in the Calendar case, could you post a small example that we could try out?


Thanks for the reply and link... 'throw Error.create(...);' does help address the first issue, where IE wasn't returning the actual error message. I'll have to see about the best way to implement this, since it may be used in some cases on pages that do not use MS Ajax, in which case Error.create() wouldn't exist. Also, though, I don't always have control over the error that is thrown. For example, if there were an error in a 3rd party control's javascript, then it may not use this pattern to throw exceptions...

I'm still finding the behavior different between IE and FF inside a CalendarExtender. Below is an example. You'll need to copy into a project that references MS Ajax & Ajax Control Toolkit and modify the @.Page tag to get it to work.

In this case I'm just handling all errors and showing a messagebox. (In the real code, I post it via xmlHttp to an HttpHandler...). In FireFox, the behavior is that the "Simple Example" works as expected, while the "Ajax Example" does not result in a call to window.onerror. The weird thing is that FireFox does in fact register an exception in the Error Console (Ctrl-Shift J), but somehow it skips window.onerror.

Thanks again for your help!

<%@. Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ClientLoggingSpike._Default" %><%@. Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"> <title>Untitled Page</title> <script type="text/javascript"> window.onerror = function(message, url, line) {alert(message + ":" + url + ":" + line);}; function throwTestException() { throw Error.create('Test Exception'); } </script></head><body> <form id="form1" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server" /> Simple Example<br /> <a href="#" onclick="throwTestException();">Throw Exception</a><br /><br /> Ajax Example<br /> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Calendar" /> <cc1:CalendarExtender ID="CalendarExtender1" runat="server" PopupButtonID="Button1" TargetControlID="TextBox1" OnClientHidden="throwTestException"> </cc1:CalendarExtender> </div> </form></body></html>

I see the issue however I am not sure why FF is causing that. TheASP.NET AJAX team may know more about this since they work encapsulating these browser eccentricities. You might get an answer on their forum.
Thanks for your help. I've posted over there and will see what they say.

No comments:

Post a Comment