0
votes

Here is an issue i'm struggling to solve for quit some time now.
I have a python + GAE backend with a flex client that communicates via JSON. I'm using flex's HttpService bound to a Responder with result and fault callbacks.

When python raises an exception, I don't seem to get it in flex. I did some research and found out that the problem probably lies in the fact that the flash runtime cannot handle http responses with status code other than 200.

So how do I get those exceptions in the client?

Thanks

1
Have you tried the ioerror or securityerror eventlistener. - michael

1 Answers

1
votes

TL;DR - Catch exception in Python and send a custom JSON object, with the message you want to display to the client, back to Flex.

This is the pattern I follow. The example I am going to give is in C# but I am assuming the same principles can be applied to Python.

I handle the exception in the web service and send a JSON object containing the error I would like to display to the user.

Here I am catching the error:

try
{
    string jsonResponse = doStuff();

    return Json(jsonResponse, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
    return Json(new GetTokenError(ex.Message), JsonRequestBehavior.AllowGet);
}

Here is the return object I use to convert to JSON and return to the client:

/// <summary>
/// Used for displaying consistent error messages
/// </summary>
public class GetTokenError
{
    /// <summary>
    /// Ctor
    /// </summary>
    /// <param name="error">Error message to display</param>
    public GetTokenError(string error)
    {
        this.error = error;
    }

    /// <summary>
    /// The error to display
    /// </summary>
    public string error { get; set; }
}

Using this method will always return a 200 unless there is something wrong with your web server, the connection between the client and web server, or gremlins have infected your machine.

As for the flex side, here are my result handlers:

var http:HTTPService = new HTTPService();
http.method = "POST";
http.resultFormat = HTTPService.RESULT_FORMAT_TEXT;
http.url = "http://url.com";
http.addEventListener(ResultEvent.RESULT, function(evt:ResultEvent):void {
    try {
        var ret:Object = JSON.decode(evt.result.toString());

        if (ret.error)
        {
            // handle server error
        }

        // go about task

    } catch (e:Error) {
        // handle local error
    }
});

http.addEventListener(FaultEvent.FAULT, function(ex:FaultEvent):void {
    // handle error with the call
});

http.send();