For example, the Ext JS Ajax call takes an object with 5 parameters: method, url, params, success and failure. The last parameter is a callback function that is executed if the server sends back a failure.
The good news is that you have to make a small change to the server-side code (only). Another piece of good news is that the code is generic - the snippet will work with most web methods.
To make it work, create a try... block around the code that may throw an error. (This example is written in C#.) Next, add the following catch block:
catch (Exception e) {
HttpResponse Response = HttpContext.Current.Response;
Response.StatusCode = 500;
Response.Write(e.Message);
HttpContext.Current.ApplicationInstance.CompleteRequest();
Response.End();
}
The code above intercepts the error and returns it to the client. If you already had Ajax code listening for failure, you will not have to change the client side at all.
Note: Special thanks to this blog post by Rick Strahl.
Response.StatusCode = 500;
Response.Write(e.Message);
HttpContext.Current.ApplicationInstance.CompleteRequest();
Response.End();
}
The code above intercepts the error and returns it to the client. If you already had Ajax code listening for failure, you will not have to change the client side at all.
Note: Special thanks to this blog post by Rick Strahl.