Facebook.ExceptionFactory.GetRestException C# (CSharp) Method

GetRestException() static private method

Gets the rest exception if possible.
static private GetRestException ( object result ) : FacebookApiException
result object The web request result object to check for exception information.
return FacebookApiException
        internal static FacebookApiException GetRestException(object result)
        {
            // The REST API does not return a status that causes a WebException
            // even when there is an error. For this reason we have to parse a
            // successful response to see if it contains error information.
            // If it does have an error message we throw a FacebookApiException.
            FacebookApiException resultException = null;
            if (result != null)
            {
                var resultDict = result as IDictionary<string, object>;
                if (resultDict != null)
                {
                    if (resultDict.ContainsKey("error_code"))
                    {
                        string error_code = resultDict["error_code"].ToString();
                        string error_msg = null;
                        if (resultDict.ContainsKey("error_msg"))
                        {
                            error_msg = resultDict["error_msg"] as string;
                        }

                        // Error Details: http://wiki.developers.facebook.com/index.php/Error_codes
                        if (error_code == "190")
                        {
                            resultException = new FacebookOAuthException(error_msg, error_code);
                        }
                        else if (error_code == "4" || error_code == "API_EC_TOO_MANY_CALLS" || (error_msg != null && error_msg.Contains("request limit reached")))
                        {
                            resultException = new FacebookApiLimitException(error_msg, error_code);
                        }
                        else
                        {
                            resultException = new FacebookApiException(error_msg, error_code);
                        }
                    }
                }
            }

            return resultException;
        }

Usage Example

Ejemplo n.º 1
0
        /// <summary>
        /// Processes the batch result.
        /// </summary>
        /// <param name="result">
        /// The json result.
        /// </param>
        /// <returns>
        /// Batch result.
        /// </returns>
        internal static object ProcessBatchResult(object result)
        {
            Contract.Requires(result != null);
            Contract.Ensures(Contract.Result <object>() != null);

            IList <object> list = new JsonArray();

            var resultList = (IList <object>)result;

            foreach (var row in resultList)
            {
                var body      = (string)((IDictionary <string, object>)row)["body"];
                var exception = ExceptionFactory.GetGraphException(body);

                object jsonObject = null;
                if (exception == null)
                {
                    // check for rest exception
                    jsonObject = JsonSerializer.Current.DeserializeObject(body);
                    exception  = ExceptionFactory.GetRestException(jsonObject);
                }

                list.Add(exception ?? jsonObject);
            }

            return(list);
        }