AJAX :: Handling Errors in AJAX ::
Handling Errors In both implementations of the call method, try-catch blocks surrounded critical operations, such as retrieving the HTTP request data and processing the HTTP request data. Revisiting the code reveals the following generalized approach:
try {
// Critical operations
}
catch( e) {
globals.errorHandler(e);
}
The exception block will surround critical operations so that the script can continue. Depending on the nature of the exception and the browser, an exception can cause the page to stop functioning properly. Then the user can only reload the HTML page and hope for the best. If the page generates an exception while reloading the page, the user has no chance of figuring out how to fix the problem. Using an exception block, you can manage these issues and then attempt to fix the problem. A problem fix could require loading other content or intervention from the user. Without the exception blocks, an error will cause the browser to stop processing the code and display the error. The error display is where the problems begin, because most browsers display errors as little icons, or in an invisible JavaScript console. For developers, it is easy to figure out the source of the error, but users are often bewildered. The generalized approach is effective because of the way the error is routed. In the exception block, there is a general reference to the globals.errorHandler method. globals.errorHandler is defined in a JavaScript file that is loaded right at the beginning of loading an HTML page.
var globals = {
state : new Object(),
errorHandler : function(e) {
},
info : function() {}
}
The globals variable has three data members, but only errorHandler interests us. The default implementation of errorHandler is to do nothing. Of course, you might consider doing nothing a very bad solution. And I agree, it is a bad solution. However, it is necessary to define a default handler; otherwise, the exception handler will generate an exception. Therefore, it is up to the developer to add into every page an error handler that could look as follows:
globals.errorHandler = function( e) {
document.getElementById( "error").innerHTML = e.toString();
}
In this implementation of the error handler, the error is displayed in a span element with an ID of error. The solution is simple, but it’s effective because the user will immediately see what went wrong. |
legal disclaimer
Our website is not responsible for the information contained by this article. Web-articles is a free articles resource.
Suggestion: If you need fresh, daily updated content for your website, feel free to use our service. Click here for more information.
related articles
Given the recent interest in Ajax, you’d be forgiven for thinking it was a new technology. In fact, the XMLHttpRequest object has been around for years. In technical terms, asynchronous JavaScript interaction with the server is nothing new. All of the other elements of the Ajax model have also been around for quite some time: CSS, (X)HTML, and DOM Scripting. Yet in 2005, interest in this methodology soared. Could it really be that simply giving this approach a snappy name like Ajax was responsible for the sudde...
The focus of this article is to provide solutions to some common, general problems and questions that are bound to arise before or during development of Asynchronous JavaScript and XML (Ajax) and Representational State Transfer (REST) applications. These common questions are not always technical in nature, often leaning more toward theory or philosophy of development. The problem with these kinds of questions is that once you begin to think about them, you keep going in a circle and end up where you star...
3. Understanding the Definition and Philosophy of Web Services and SOA
Understanding the Definition and Philosophy of Web Services and SOA Wikipedia offers the following definition of Web services:4 The W3C defines aWeb service as a software system designed to support interoperable machine-to-machine interaction over a network. This definition encompasses many different systems, but in common usage the term refers to those services that use SOAPformatted XML envelopes and have their interfaces described by WSDL. For ex...
4. Understanding the Definition and Philosophy of REST
Understanding the Definition and Philosophy of REST REST is a controversial topic among Web service enthusiasts, because it’s considered to stand for the opposite of what Web services and SOA are trying to achieve. The problem with this thinking is that REST is not in contradiction with the abstract definition of SOA and Web services. REST is in contradiction with technologies such as SOAP, WSDL, and WS-* specifications. The following offers a quick definition of REST:...
5. The Easiest Way to Get Started with Ajax and REST
The Easiest Way to Get Started with Ajax and REST Problem You want to know the best way to get started with writing Ajax and REST. Solution When developing an Ajax and REST application, you must decide on the tools and frameworks you’ll use. The choice is simple: Use whatever you’re using today, and write some Ajax applications. You don’t need to change the tools you’re using today. Whether you’re using ASP.NET, JavaServer Pages (JSP), PHP, Ruby, or Python, you...
6. Testing a Dynamic Contract with Ajax
Coding the Contract Using Test-Driven Development Techniques Coding the contract using agile and test-driven development techniques requires writing a number of tests and implementing aMock URL layer. Problem You want to code the contract using these development techniques. Solution To demonstrate, let’s define a use case, implement the use case as a contract, write a test case(s) to implement the contract, implement the contract in the Mock URL, and finally...
7. Testing the Client Side Logic
Problem You want to effectively test your application’s client-side logic. Theory Testing GUI code tends not to be a productive task because of the complications that arise. The main complication is how to test the correctness of a user interface. Imagine a situation where clicking a button causes a table to be filled with data. Now imagine that when a check box is checked and the button is clicked again, a different table is filled with content. The fact that clicking the same button results in two ...