Error Handling in Ajax Controls

Both the AjaxMethodCallback and HoverPanel controls support client side error handling that is enabled through an error callback handler. When a callback is made to the server, you assign an Error callback handler that is called whenever an error occurs as part of the callback or the client code that is firing and/or handling the callback. The method is passed an error object that contains a message and detail property as well as an identifying isCallbackError property that can be used to check whether an object is in fact an error object.

Remember that all callbacks are asynchronous so the errors too are fired asynchronously even if an error occurs in the client code before it calls back on the server. All callback handlers are simlpy functions that are called with a single result object parameter which is a CallbackException object which contains a message property. The handler looks like this:

function OnPageError(error)
{
	alert(error.message); // showStatus("Error: " + error.message, 4000);
}

If you have multiple callbacks you can route all of them to the same error handler, or if you need specific error handling you can route to a specific error handler that performs custom operations. The latter might be necessary if at the start of the callback you disable or hide controls and they need to be made visible again when the call completes.

The error handling hook up is easy, but it varies slightly depending on which mechanism you use.

AjaxMethodCallback:

For method callbacks you pass the error handler as part of the proxy method call or - if you are explicitly using the client callback object - on the wwCallbackMethod object's callMethod() function.

function GetCustomerEntity()
{
    CustomerCallback.GetCustomerEntity($('txtCustomer').val(),
                   function(result) {
                      // do something with result
                   },onPageError);
}
function onPageError(error)
{
	alert("Error: " + error.message);
}

If you're explicitly using the AjaxMethodCallback object the code looks like this:

var ExceptionCallback = Callback_GetCallback();
ExceptionCallback.postbackMode = "Post";  // pass form vars to server
ExceptionCallback.callMethod('ThrowException',[$("txtSaySomething").value],
                             ThrowException_Callback,onPageError);

HoverPanel

The error handler is hooked as part of the Control.startCallback() function:

function ShowPanel()
{
	LookupPanel.startCallback(event,"id=" + Id.toString(),null,onPageError);
}
function onPageError(error)
{
	alert(error.message);
}

HttpClient Object

The more low level HttpClient object also uses a callback error handler in the same fashion and it too fires an error handler when an error occurs.

Http = new HttpClient();     
var Result = Http.send("http://localhost/default.asp",null,HttpCallback,onPageError);

function HttpCallback(Result)
{
	$("divResult").innerHTML = Result;
}
function onPageError(error)
{
	alert(error.message);
}


© West Wind Technologies, 1996-2016 • Updated: 12/19/15
Comment or report problem with topic