Web DevCenter
oreilly.comSafari Books Online.Conferences.
MySQL Conference and Expo April 14-17, 2008, Santa Clara, CA

Sponsored Developer Resources

Web Columns
Adobe GoLive
Essential JavaScript
Megnut

Web Topics
All Articles
Browsers
ColdFusion
CSS
Database
Flash
Graphics
HTML/XHTML/DHTML
Scripting Languages
Tools
Weblogs

Atom 1.0 Feed RSS 1.0 Feed RSS 2.0 Feed

Learning Lab






Using the Mozilla SOAP API
Pages: 1, 2, 3, 4, 5

Handling a SOAPFault

A SOAP fault is usually generated when something goes horribly wrong with the call: a missing parameter, an invalid method, etc. When such a fault occurs, a SOAPFault object is generated which contains information about what went wrong during the SOAP operation.



To test for a SOAPFault, the script must check the fault property of the SOAPResponse object. If no fault has occurred, the value of this property will be null. Otherwise, it's a SOAPFault object.

A SOAPFault object has the following properties:

  • faultCode: the numeric code of the fault.
  • faultString: a string, usually containing a message about the fault.
  • faultNamespaceURI: the namespace URI of the fault.
  • element: a pointer to the Document Object Model (DOM) node in the returned SOAP XML message that represents the fault.
  • detail: another pointer to a DOM node containing the fault details.

The handleSOAPResponse function below uses faultCode and faultString to warn the user that a fault has occurred. If you want, you can change the warning messages or add code to provide alternate behavior in the case of a fault.

function handleSOAPResponse (response,call,error)
{
    if (error != 0)
    { 
        alert("Service failure");
        return false;
    } else 
    {
        var fault = response.fault; 
        if (fault != null) { 
            var code = fault.faultCode; 
            var msg = fault.faultString; 
            alert("SOAP Fault:\n\n" +
                "Code: "  + code +
                "\n\nMessage: " + msg
            );
            return false;
        } else 
        {
            return response;
        }
    }
}

Note that in the case of either a fault or a service error, a value of "false" is returned to the callback specified in callGoogle. If the response passes these checks, the script returns the full SOAPResponse to the callback.

Using the callGoogle Function

I'm going to backtrack for a moment and look at how to use the callGoogle function to make a SOAP call.

First, you must prepare an array of parameters required by the service method. As mentioned above, the Google Web API provides three service methods. One of them, doSpellingSuggestion, requires two parameters: the word or phrase you want to check, and a Google API key.

The second argument required by callGoogle is an array containing the parameters needed to make the call. This is easily done with the SOAPParameter constructor, which requires two arguments: the value and name of the parameter to be added, in that order.

// create the parameters array
var params = new Array();

// replace 'yourKeyHere' with your Google key
params[0] = new SOAPParameter(yourKeyHere, "key");
params[1] = new SOAPParameter("brintey speers","phrase");

// now call the Google service
callGoogle("doSpellingSuggestion",params,parseResult);

Here's a more complex example for the doGoogleSearch method:

var p = new Array();
p[0] = new SOAPParameter(yourKeyHere,"key");
p[1] = new SOAPParameter("Mozilla","q"); // the search query
p[2] = new SOAPParameter(0,"start");
p[3] = new SOAPParameter(10,"maxResults");
p[4] = new SOAPParameter(false,"filter");
p[5] = new SOAPParameter("","restrict");
p[6] = new SOAPParameter(false,"safeSearch");
p[7] = new SOAPParameter("","lr");
p[8] = new SOAPParameter("Latin-1","ie");
p[9] = new SOAPParameter("Latin-1","oe");

callGoogle("doGoogleSearch",p,parseResult);

In the above code, parseResult is the callback function we want to handle the processing of a successful (no-fault, no-error) SOAP operation. Since the handleSOAPResponse function takes care of the error-checking, you don't have to worry about that in your callbacks. If an error or fault is found, the value returned to parseResult will simply be false.

Parsing the SOAPResponse

The last piece of our puzzle is actually writing the parseResult callback function to handle the SOAP response. The first order of business is to see if the result handed to parseResult after a SOAP operation is indeed a SOAPResponse object:

function parseResult(result)
{
    if (result==false)
    {
         return;
    }
}

The above script exits without warning if no SOAPResponse is available. A more effective implementation would provide an alert or some alternate behavior.

Pages: 1, 2, 3, 4, 5

Next Pagearrow