Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>TL;DR</strong> - Catch exception in Python and send a custom JSON object, with the message you want to display to the client, back to Flex.</p> <p>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.</p> <p>I handle the exception in the web service and send a JSON object containing the error I would like to display to the user.</p> <p>Here I am catching the error:</p> <pre><code>try { string jsonResponse = doStuff(); return Json(jsonResponse, JsonRequestBehavior.AllowGet); } catch (Exception ex) { return Json(new GetTokenError(ex.Message), JsonRequestBehavior.AllowGet); } </code></pre> <p>Here is the return object I use to convert to JSON and return to the client:</p> <pre><code>/// &lt;summary&gt; /// Used for displaying consistent error messages /// &lt;/summary&gt; public class GetTokenError { /// &lt;summary&gt; /// Ctor /// &lt;/summary&gt; /// &lt;param name="error"&gt;Error message to display&lt;/param&gt; public GetTokenError(string error) { this.error = error; } /// &lt;summary&gt; /// The error to display /// &lt;/summary&gt; public string error { get; set; } } </code></pre> <p>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.</p> <p>As for the flex side, here are my result handlers:</p> <pre><code>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(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload