Home arrow AJAX & Prototype arrow Google's Closure Compiler Service API: Displaying Warnings and Errors

Google's Closure Compiler Service API: Displaying Warnings and Errors

This penultimate part of the series offers a quick overview of the debugging capabilities that the Closure Compiler Service API provides for free. Activating the API’s debugger is as easy as assigning the values “warnings” and “errors” to its “output_info” parameter.

  1. Google's Closure Compiler Service API: Displaying Warnings and Errors
  2. Debugging with the Closure Compiler Service API
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
October 13, 2010

print this article



Using the new web service released by Google in the last few weeks, called Closure Compiler Service API, is a straightforward process that can be mastered with minor hassles. The service will let you optimize your JavaScript snippets by means of a set of intuitive parameters, which allow you to control certain aspects related to the compiler’s behavior. These include the level of optimization that will be applied to the source JavaScript code, the format in which the output will be returned to the client, and even what kind of information will be displayed on screen once the corresponding compiling process has been executed.

Of course, if you've already read the previous installments of this series, you probably have clear idea of how to perform some of the aforementioned tasks through the Closure Compiler Service API. In those tutorials I developed a few basic examples that showed, among other things, how to apply different optimization algorithms to a sample JavaScript file, and how to serve the compiled output first in “text/javascript” format, then in XML and finally in JSON.

It’s valid to stress, though, that the Closure Compiler Service API comes with many other handy features that also deserve a close analysis -- such as its ability to debug target JavaScript code by echoing the errors and warnings that might occur during the compiling process. As with other facets of the API, the behavior of its built-in debugger can be controlled easily by direct manipulation of its “output_info” argument.

With that said, in this penultimate part of the series, I’m going to set up a few additional examples that will demonstrate how to display errors and warnings via the argument just mentioned. So start reading right now!

Playing with the “output_info” argument: returning compiled JavaScript code to the client

As I said at the beginning, the Closure Compiler Service API can return to the client different kinds of information, according to the values assigned to its “output_info” parameter. If no value is passed to this argument, the compiler simply will return by default the compiled JavaScript code. This is the expected behavior in most use cases. Nevertheless, it’s possible to instruct the API to display errors and warnings with the same ease.

But before I start demonstrating how to accomplish that (and for the sake of completeness), here’s an example that uses the aforementioned “output_info” argument in a standard fashion, as it sends back to the client only the optimized JavaScript code:     


// example using output_info = compiled_code
try {
    // include the autoloader class
    require_once 'Autoloader.php';

    // create an instance of the file handler class and read the specified JavaScript file
    $fileHandler = new FileHandler;
    $js = $fileHandler->read();
    // build the array of arguments that will be passed to the closure compiler API
    $data = array(
         'js_code' => $js,
         'compilation_level' => 'ADVANCED_OPTIMIZATIONS',
         'output_format' => 'text',
         'output_info' => 'compiled_code'
    // create an instance of the ClosureCompilerHandler class
    $compilerHandler = new ClosureCompilerHandler($data);
    // query the closure compiler API and get the compiled JavaScript function
    $response = $compilerHandler->sendRequest();
    // print to screen the compiled JavaScript function
    echo $response;
    displays the following
    var a=document.createElement("div");a.setAttribute("id","divid");a.setAttribute("class","divclass");document.getElementsByTagName("body")[0].appendChild(a);
catch (Exception $e) {
    echo $e->getMessage();

As you can see above, the value assigned to the “output_info” parameter is the string “compiled_code,” which tells the compiler to do exactly that: return the compiled JavaScript, regardless of the level of optimization applied to it. While this is the default behavior of the compiler, since most of the time a client querying its API will expect to receive the optimized output for further processing, it’s feasible to return the warning and errors raised during the compiling process.

If you’re wondering how this can be done, the answer is simple: assigning the value “warnings” to the “output_info” parameter will cause the compiler to send to the client the corresponding warnings, while a value of “errors” will return only the potential errors. That’s not rocket science, is it?

If you’re interested in seeing the debugging capabilities of the Closure Compiler Service API in action, in the next section I’m going to build another example that will show how to make the API to display the warnings raised during the optimization process.

To see how this example will be developed, jump forward and keep reading.   

>>> More AJAX & Prototype Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Best AJAX Tutorials for Forms
- The Best AJAX Tutorials
- 8 Great Ajax Tutorials
- Using Ajax and jQuery
- Using Ajax and jQuery with HTML Forms
- Ajax.org Offers Cloud9 IDE for JavaScript
- Java Technologies Provider ICEsoft Releases ...
- Using Recaptcha in AJAX Prototype Framework ...
- Google's Closure Compiler Service API: Addit...
- Installing Google Web Toolkit: Introducing t...
- Google's Closure Compiler Service API: Displ...
- Google's Closure Compiler Service API: Deliv...
- Google's Closure Compiler Service API: the A...
- Google's Closure Compiler Service API: the S...
- Google's Closure Compiler Service API: Optim...

Developer Shed Affiliates


Dev Shed Tutorial Topics: