Monday, March 26, 2012

Will Application_Error catch any unhandled exception during atlas postback ?

Looks like atlas postback unhandled exception is not comming to Application_Error handler.

Is this right ? or is there any way to redirect any atlas postback unhandled exception to "Application_Error" ?

Thanks,

Perhaps you can utilize the Error Template tag as well as OnPageError event to handle exceptions you encounter in your application.

<atlas:ScriptManager EnablePartialRendering="true"OnPageError="Page_ErrorHandler" runat="server">
<ErrorTemplate>
<div style='width: 450px; height: 300px; padding: 10px; border: solid 3px black; background: #ffd; text-align: left;'>
<h1>Server Error</h1>
<p>Oops looks like we're still working on some code. Administrators have been notified and this emerging issue will be fixed.</p>
<p><input id="okButton" type="button" value="OK" runat="server"/></p>
</div>
</ErrorTemplate>
</atlas:ScriptManager>

Here is the handler for the error.

Import System.Net.Mail

C#

protectedvoid Page_ErrorHandler(object sender, PageErrorEventArgs e)
{

MailMessage mail = new MailMessage();
mail.From = new MailAddress("me@.mycompany.com");
mail.To.Add(you@.yourcompany.com);

mail.Subject = "Error Occured My Application";
mail.Body = "Error Message: e.Error.Message<br /><br />Stack Trace: e.Error.StackTrace<br /><br />Source: e.Error.Source<br /><br />Help Link: e.Error.HelpLink";
mail.IsBodyHtml = true;
smtp.Credentials = new NetworkCredential("username", "secret");
SmtpClient smtp = new SmtpClient("127.0.0.1");
smtp.Send(mail);

//Or perhaps insert it into a database. Or if you feel like it... perhaps the Windows Event Viewer

}

VB

SubPage_ErrorHandler (sender As Object, e As PageErrorEventArgs)

Dim mail As MailMessage = New MailMessage
mail.From = New MailAddress("me@.mycompany.com")
mail.To.Add("you@.yourcompany.com")
mail.Subject = "Error Occured My Application"
mail.Body = "Error Message: e.Error.Message<br /><br />Stack Trace: e.Error.StackTrace<br /><br />Source: e.Error.Source<br /><br />Help Link: e.Error.HelpLink"
mail.IsBodyHtml = True
smtp.Credentials = New NetworkCredential("username", "secret")
Dim smtp As SmtpClient = New SmtpClient("127.0.0.1")
smtp.Send(mail)

End Sub

You can also throw the credentials into the web.config inside the <System.Net> tag like so.

<system.net>
<mailSettings>
<smtpfrom="me@.mycompany.com">
<networkhost="127.0.0.1" port="25" userName="myUsername" password="secret" defaultCredentials="true" />
</smtp>
</mailSettings>
</system.net>

Hopefully that will help a little bit.

JoeWeb


Proposed solution is great. But it requires to create Page_ErrorHandler in every Page whereScriptManager is present. It leads to duplication of code. And it very easy to miss it somewhere.

We're logging all unhandled exception in ASP.NET application onproduction environment and regulary analyze them. Errors that occur during Atlas post-back are not handled because Application_Error is not fired.

Is there any solution to exception handling during Atlas post-backs like Application_Error which is global for all application?

Thanks in advance


What about this? Implement your error handling method for the script manager event as static in a class, and onload of any page with the script manager, all you have to do is assign that static method to the event handler... then you won't be duplicating any code, and its only 1 line of code to add per page.


The ErrorTemplate is no longer supported with Ajax and these are two methods you can use..

In codebehind - I always pull in the scriptmanager so that I can ref it and manage it.

( on your page init)

SM = (ScriptManager)GetControl(skin,"SM");
SM.AsyncPostBackError +=new EventHandler<AsyncPostBackErrorEventArgs>(SM_AsyncPostBackError);

then new event...

void SM_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
{
SM.AsyncPostBackErrorMessage = e.Exception.Message;
bllLogging.RecordError("GridViewModal ScriptManager Error", e.Exception, Severity.Severe, "", "");
}

The BllLogging is my custom error recording routine I use to track error - replace with your own or re-throw it if you have a global exception handler...

In the example above - I use this in a base class which all of my other classes derive from so I only have to code it once...

Alternatively you can also do this on your pages or include in a mycustom.js

<ajax:ScriptManager ID="SM" EnablePartialRendering="true" ScriptMode="Auto" runat="server" />
<script type="text/javascript">
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(OnEndRequest);

function OnEndRequest(sender,args)
{ // *** Check for errors

if (args.get_error() != undefined /* && args.get_error().httpStatusCode == '500' */)
{
//debugger;
var errorMessage = args.get_error().message
alert("Custom Error Handling:\r\n" + args.get_error().message);
args.set_errorHandled(true);
//var ErrorDisplay = $get("ErrorDisplay_Message");
//ErrorDisplay.innerHTML = "Custom Error Handling:\r\n" + args.get_error().message;
}

}
}
</script>

There also some additional things you can do with assigning custom errors and the likes and is well documented on their documentation site under scriptmanager references...

However, be careful when using the custom error feature - the code behind version does quite well logging actual messages but when in custom mode - whatever is provided as the error text is used (which hides the actual error message...)

No comments:

Post a Comment