04.11.2015 Views

javascript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 14: Error Handling and Debugging<br />

Communication errors can also occur when using Ajax communication. Issues and<br />

errors surrounding Ajax are discussed in Chapter 17 .<br />

Distinguishing between Fatal and Nonfatal Errors<br />

One of the most important parts of any error - handling strategy is to determine whether or not an error is<br />

fatal. One or more of the following identifies a nonfatal error:<br />

❑<br />

❑<br />

❑<br />

❑<br />

It won ’ t interfere with the user ’ s main tasks.<br />

It affects only a portion of the page.<br />

Recovery is possible.<br />

Repeating the action may result in success.<br />

In essence, nonfatal errors aren ’ t a cause for concern. For example, Yahoo! Mail ( http://mail.yahoo.com )<br />

has a feature that allows users to send SMS messages from the interface. If for some reason SMS messages<br />

don ’t work, it ’ s a nonfatal error because that is not the application ’ s primary function. The primary use case<br />

for Yahoo! Mail is to read and write e-mail messages, and as long as the user can do that, there is no reason<br />

to interrupt the user experience. Nonfatal errors don ’ t require you to send an explicit message to the user —<br />

you may be able to replace the area of the page that is affected with a message indicating that the<br />

functionality isn ’ t available, but it ’ s not necessary to interrupt the user.<br />

Fatal errors, on the other hand, are identified by one or more of the following:<br />

❑<br />

❑<br />

❑<br />

The application absolutely cannot continue .<br />

The error significantly interferes with the user ’ s primary objective .<br />

Other errors will occur as a result .<br />

It ’ s vitally important to understand when a fatal error occurs in JavaScript so appropriate action can be<br />

taken. When a fatal error occurs, you should send a message to the user immediately to let them know<br />

that they will not be able to continue what they were doing. If the page must be reloaded for the<br />

application to work, then you should tell the user this and provide a button that automatically reloads<br />

the page.<br />

You must also make sure that your code doesn ’ t dictate what is and is not a fatal error. Nonfatal and fatal<br />

errors are primarily indicated by their affect on the user. Good code design means that an error in one<br />

part of the application shouldn ’ t unnecessarily affect another part that, in reality, isn ’ t related at all. For<br />

example, consider a personalized home page such as My Yahoo! ( http://my.yahoo.com ) that has<br />

multiple independent modules on the page. If each module has to be initialized using a JavaScript call,<br />

you may see code that looks something like this:<br />

for (var i=0, len=mods.length; i < len; i++){<br />

mods[i].init(); //possible fatal error<br />

}<br />

486

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!