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 />

On its surface, this code appears fine: the init() method is called on each module. The problem is that<br />

an error in any module ’ s init() method will cause all modules that come after it in the array to never<br />

be initialized. If the error occurs on the first module, then none of the modules on the page will be<br />

initialized. Logically, this doesn ’ t make sense since each module is an independent entity that isn ’ t<br />

reliant on any other module for its functionality. It ’ s the structure of the code that makes this type of<br />

error fatal. Fortunately, the code can be rewritten as follows to make an error in any one module<br />

nonfatal:<br />

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

try {<br />

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

} catch (ex){<br />

//handle error here<br />

}<br />

}<br />

By adding a try - catch statement into the for loop, any error when a module initializes will not<br />

prevent other modules from initializing. When an error occurs in this code, it can be handled<br />

independently and in a way that doesn ’ t interfere with the user experience.<br />

Log Errors to the Server<br />

A common practice in web applications is to have a centralized error log where important errors are<br />

written for tracking purposes. Database and server errors are regularly written to the log and categorized<br />

through some common API. With complex web applications, it ’ s recommended that you also log<br />

JavaScript errors back to the server. The idea is to log the errors into the same system used for server - side<br />

errors and categorize them as having come from the front end. Using the same system allows for the<br />

same analytics to be performed on the data regardless of the error ’ s source.<br />

To set up a JavaScript error - logging system, you ’ ll first need a page or server entry point on the server<br />

that can handle the error data. The page need not do anything more than take data from the query string<br />

and save it to an error log. This page can then be used with code such as the following:<br />

function logError(sev, msg){<br />

var img = new Image();<br />

img.src = “log.php?sev=” + encodeURIComponent(sev) + “ & msg=” +<br />

encodeURIComponent(msg);<br />

}<br />

The logError() function accepts two arguments: a severity and the error message. The severity may be<br />

numbers or strings, depending on the system you ’ re using. An Image object is used to send the request<br />

because of its flexibility, as described here:<br />

❑<br />

❑<br />

The Image object is available in all browsers, even those that don ’ t support the<br />

XMLHttpRequest object.<br />

Cross - domain restrictions don ’ t apply. Often there is one server responsible for handling error<br />

logging from multiple servers, and XMLHttpRequest would not work in that situation.<br />

487

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

Saved successfully!

Ooh no, something went wrong!