SWX instance: how to set a fault handler

The SWX class also provides a fault handler that you can use to handler errors during SWX RPC calls. To test it out, modify your code so that it matches the listing below:

import org.swxformat.SWX;
 
var swx:SWX = new SWX();
swx.gateway = "http://localhost:8888/php/swx.php";
swx.encoding = "GET";
swx.debug = true;
swx.timeout = 2; // seconds
 
var callParameters:Object =
{
    serviceClass: "Calculator",
    method: "addNumbers",
    args: [35, 7],
    result: [this, resultHandler],
    timeout: [this, timeoutHandler],
    fault: [this, faultHandler]
}
 
swx.call(callParameters);
 
function resultHandler(event:Object)
{
    status.text = event.result;
}
 
function timeoutHandler()
{
    status.text = "Call timed out!";
}
 
function faultHandler(event:Object)
{
    status.text = event.fault.message;
}

And modify your class so that it generates an error:

<?php
    class Calculator
    {
        function addNumbers($n1, $n2)
        {
            return $n3; // $n3 does not exist!
        }
    }
?>

Test your FLA and you should get something along the lines of Error 8: Undefined variable: n3 in /htdocs/swx/trunk/php/services/Calculator.php, line 7 in the status text field in Flash.

The fault handler also returns API-specific fault codes (e.g., Flickr API error codes) back to Flash.

4 Responses to “SWX instance: how to set a fault handler”


  1. 1 Victor

    Hey Aral, i was just getting into SWX, and wanted to know how the progress handler works when you make a call, what parameters if recieves exactly. Thanks! SWX rocks

  2. 2 Florian

    If you cannot send a new SWX call after a failed call, here is the solution:

    function faultHandler(event:Object)
    {
    trace (event.fault.message);

    // cancel all calls
    swx.cancelAllCalls();

    }

    It took my a really long time to figure that there is a function called cancelAllCalls() :) Before, my application just didn’t do anything after a failed call.

  3. 3 Adam Royle

    I just discovered this too - seems like a bug to me, since calling this code trace()’s the following error:

    Warning: Cannot cancel loading clip since it has already loaded: http://blah/php/swx.php

  4. 4 Aral Balkan

    Hi guys,

    I am going to look into this. Thanks for reporting it.

Leave a Reply




Bad Behavior has blocked 10565 access attempts in the last 7 days.