Using the Error-Handling Object






Using the Error-Handling Object

Error handling is essential to a successful web application, but it is often overlooked and deemed a boring chore to developers. Using the Observer pattern for Ajax error handling allows us to create an extremely intuitive user experience and is actually quite interesting to program. As you learned in Chapter 9, "Extending the Engine," with HTTP status codes, we can also provide immediate feedback and options to the user when errors occur without disrupting the flow of experience or leaving a user with questions regarding what to do next. Imagine having an object that can control all the alerts in your application. I have created one that is named Alert. This object registers an error method with the ErrorManager object immediately after it is instantiated. This method accepts the three parameters the notifyObservers method passes to observers in its collection. It then concatenates an error string that is displayed in an alert box to the user (see Listing 16.8).

Listing 16.8. Alert Object (Alert.js)

Alert = {};

ErrorManager.registerObserver("Alert.error");

Alert.error = function(message, url, line)
{
    var error = "Error: "+ message +"\n";
    error += "URL: "+ url +"\n";
    error += "Line: "+ line;
    alert(error);
    //ErrorManager.unregisterObserver("Alert.error");
 }

After the error method is called, you can unregister it as an observer by uncommenting the last line in the method. This is only one simple example of how to use the Observer pattern in a web application. There are millions of ways this pattern can bring power to your applications.



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows