Flash Remoting MX returns a Status event instead of a result if either of the following happens:
You write status event handlers to respond to the errors, typically by displaying an error message or logging the error information. In some cases, you might be able to include recovery code, such as code to retry a call to a busy server, in the error handler.
When Flash Remoting MX receives a Status event, Flash passes an error object that contains information about the error to the status event handler. The error object has the following format:
Flash Remoting MX supports the following hierarchy of error handling:
getService call:getService call, the following happens:
a. If the responder has a function with a name of the form functionName_Status, where functionName is the name of the service function that you called, Flash Remoting MX returns the status information for the function to that method.
b. If the default responder object specified in the getService call has a function named onStatus, Flash Remoting MX returns the status to that function.
onStatus method. When you use this technique, the responder object must also have an onResult method to handle the error. _root.onStatus, Flash Remoting MX returns the status to that function.
_global.System.onStatus, Flash Remoting MX returns the status to that function.
This hierarchy adds two levels, items 2 and 3, to the result-handling scheme described in "Result-handling hierarchy". As a result, you can define handlers for all otherwise-unhandled status events on a level or in the entire application.
Flash applications use error-handling code less often than server applications, because Flash does not report errors to the user when viewing movies. However, Flash Remoting MX does make error information available to the movie ActionScript, and your application can use this information. The error handling hierarchy lets you handle errors with any degree of granularity. For example:
Error handling can be particularly useful for remote services, because the user's experience depends on remote data that might not always be retrieved, and it might not be obvious that data is not retrieved. Also, in some cases, the error-handling code can even recover from a transient error. For example, if a service function fails due to a time-out, it might be appropriate for the Flash application to try the request a second time. If the request fails a second time, the movie could then display a message to the user and post a message to the server to log the error.
The following section, "Example: error handling using unique result objects" shows how you can apply error handling to a Flash Remoting MX application. For more information about selecting from the status event-handling hierarchy, see "Result-handling strategies", which discusses strategies for selecting from the similar result-handling hierarchy.
The following example adds error handling to the example in the "Example: specifying unique result objects in service function calls". The error handlers for the temperature and forecast display custom messages to the user. The general error handler displays a generalized message. All handlers call a function to report the error to a log on the server.
#include "NetServices.as"
#include "NetDebug.as"
// Initialization code, run once for each movie instance.
if (inited == null)
{
inited = true;
NetServices.setDefaultGatewayURL("http://apps.myco.com/flashservices/gateway")
gatewayConnection = NetServices.createGatewayConnection();
// Do not specify a default responder object when creating the service object.
weatherService = gatewayConnection.getService("flashExamples.weatherStation");
}
// Temperature result handler object
function tempResult()
{
this.onResult = function(temperature)
{
temperatureIndicator.text = temperature;
}
this.onStatus = function (status)
{
temperatureIndicator.text = "No Temperature Available";
_global.logStatus(Status);
}
}
//Forecast result handler object
function forecastResult()
{
this.onResult = function (forecast)
{ forecastIndicator.text = forecast; }
this.onStatus = function (status)
{
forecastIndicator.text = "No Forecast Available";
_global.logStatus(Status);
}
}
// General result handler object
function generalResult ()
{
this.onResult = function (result)
{ generalMessageBox.text = result; }
this.onStatus = function (status)
{
generalMessageBox.text = "An error occurred. Please try later";
_global.logStatus(Status);
}
}
// Call the service functions and specify the result handler as the first argument.
// In a real application, these calls would be initiated by user actions in the
// Flash application.
weatherService.getTemperature(new tempResult(), "New York" );
weatherService.getForecast(new forecastResult(), "Chicago" );
weatherService.getServiceStatus( new generalResult(), "San Francisco" );
weatherService.getUsageStats(new generalResult());
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/flashremoting/mx/Using_Flash_Remoting_MX/UseActionScript8.htm