Sunday, February 1, 2009

Exception handling in Ajax

Exception handling plays an important role in any application development. The way we present our exceptions to the user decides the quality of our software. The way we design a pattern for exception handling in C#, ASP.NET we should also design a similar pattern for implementing Ajax exceptions. If an Ajax exception is not caught, then it should as a pop-up directly on the screen showing the exact technical error message with a code. A layman/user will never know what went wrong behind the scene.

Ajax Exception Message


Different ways of handling exception in Ajax
1. Using the AsyncPostBackErrorMessage property of the Script Manager class
You can set the exception message that needs to be shown to the user to the AsyncPostBackErrorMessage property of the ScriptManager object. This can be achieved in the following way:

scriptManager.AsyncPostBackErrorMessage = "Oops..\nAn error occurred while processing";

Set the above exception message in the catch block of the method. When an exception occurs, this message will be shown to the user in a pop-up window.

2. Using the above method will show the exception message to the user as a pop-up window. It is not a good practice to show a pop-up on error. Better way to do is to add a Label field to the web page and set the text of that label field with the exception message. For more clarity to the user, the exception message can be shown in Red color. Ajax inherently exposes an event OnAsyncPostBackErrorHandler. This event is invoked when an exception occurs in your web page. It is better to set the exception message in this event as shown below:
 protected void OnAsyncPostBackErrorHandler(object sender, AsyncPostBackErrorEventArgs e)
{
scriptManager.AsyncPostBackErrorMessage = "Oops..\nAn error occurred while processing";
}
Is this going to suffice? The answer it NO. Here we have just handled an event and set the required exception message to the AsyncPostBackErrorMessage property of the scriptManager class. We have to handle another event at client side to set this message to the Label control and inform the Ajax manager to not show the ugly pop-up. Following code will be inserted at the client side:
 <script language="javascript" type="text/javascript">
// Register the EndRequest Ajax event
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);

// Here the Ajax EndRequest event is used to supress any ALERT error message and
// show them in the lblStatus instead.
function EndRequestHandler(sender, args) {
if (args.get_error()) {
var msg = args.get_error().description;
msg = msg.replace('Sys.WebForms.PageRequestManagerTimeoutException:', '');
msg = msg.replace('Sys.Webforms.PageRequestManagerServerErrorException:', '');
document.getElementById('ctl00_mainPlaceHolder_lblStatus').innerText = msg;
// This will avoid the pop-up
args.set_errorHandled(true);
}
}

</script>
Hope this helps. Your comments are welcome!

0 comments:

Post a Comment