In: Categories » » 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 notice
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.
Useful tools and features
related articles
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...
2. 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...
3. 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 ...
4. Understanding JavaScript and Types
Understanding JavaScript and Types Problem You want to work around the fact that JavaScript does not have types declared for its variables. Theory JavaScript code does not have any variables with a declared type. The lack of typed variables is apparent when you declare functions. That said, not having typed variable declarations does not mean JavaScript has no types or no type safety. Let’s start out with the simple declaration of a function, as illustrated by the following ex...
5. Coding Using Conventions and Not Configurations
Coding Using Conventions and Not Configurations Problem You want to make your JavaScript constructs more efficient by applying the Rails “convention over configuration” principle to them. Theory You may already be familiar with the programming platform Ruby on Rails, which is used to build Web applications. The focus of this recipe is not Ruby on Rails, but one aspect of Ruby on Rails namely, convention over configuration (see http://en.wikipedia.org/wiki/ Ruby_on_Rails for m...
6. Advantage of parameterless functions in JavaScript
Using Parameterless Functions Problem You want to take advantage of parameterless functions in JavaScript. Theory JavaScript functions for the most part have parameters. You may think that the previous sentence states the obvious after all, without parameters, what data could be passed to a function? JavaScript has the ability to declare functions that have no parameters, even though the caller of the function has passed parameters to the function. For example, let’s look at...
7. JavaScripot Functions
Treating Functions Like Objects Problem You want to take advantage of the fact that functions are objects (remember, everything is an object in JavaScript). Theory Many people think that a function is some keyword used in JavaScript. A function is also an object that can be manipulated. Knowing that a function is an object makes it very interesting from the perspective of writing JavaScript code, because the code can treat the function like another other object. This mean...
8. Implementing an Error and Exception Handling Strategy
Implementing an Error and Exception Handling Strategy Problem You want to implement a clean error and exception handling strategy in your applications, to make them run more smoothly. Theory Of course, you might argue that one error is a dialog box and the other is generated in the JavaScript console. The fact that one browser uses a dialog box to show an error and the other does not is a browser issue, not an error issue. A concise way of classifying the two errors is to ...
9. Understanding the Behavior of Variables When Implementing Recursion
Understanding the Behavior of Variables When Implementing Recursion Problem You want to implement recursion in JavaScript, and you also want to understand how variables will behave under those circumstances. Theory In JavaScript, you do not need to declare the variable type, or even declare the variable. For example, the following code works perfectly: if( counter == 1) { buffer = "counter is 1"; } document.getEle...
10. Using Functions to Initialize and Make Decisions JavaScript
Using Functions to Initialize and Make Decisions Problem You want to use functions to initialize and make decisions. Theory Usually when you write a piece of code where a change of logic needs to take place based on a context, you use a decision structure. For example, say you are implementing a light switch using a program. You turn on the light if the light is off, and you turn off the light if the light is on. The behavior of the program is determined by the conditions. One example behavior t...
