Montag, Juli 11, 2005

Handling Exceptions

Also in AJAX architectures failures in the communication and in the execution may raise errors. Fortunately when using a WebServices based framework these cases are also defines in the SOAP communication protocol.

The answer to a call to a WebService in the SOAP format will under normal conditions return the result of the method:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <CalcPrimeFactorsResponse xmlns="http://www.mathertel.de/CalcFactorsService/">
      <CalcPrimeFactorsResult>2 2</CalcPrimeFactorsResult>
    </CalcPrimeFactorsResponse>
  </soap:Body>
</soap:Envelope>

In the case of an error the information about the failure of the server-side execution will be passed back to the client.

In the simplest case this information is shown to the user but also other reactions may be appropriate like reloading the whole page.

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <soap:Fault>
    <faultcode>soap:Server</faultcode>
    <faultstring>Server was unable to process request.
---> Input string was not in a correct format.</faultstring>
    <detail />
    </soap:Fault>
  </soap:Body>
</soap:Envelope>

The client-side implementation allows the handling of the exceptions that occur at the level of the WebService proxies an on the level of the AJAX Actions.

In both cases there is an onException event available and a specialized method handling these exceptions can be plugged in. This method gets passed the thrown exception as a parameter and can organize the further functionality of the page.

There is usable method defined in ajax.js that can be used for showing exceptions:

proxies.alertException(ex):

This method shows the exception object in a readable format using an alert box. This method can be used in proxies.service.method.onException as well as in action.onException.

Keine Kommentare: