Inheritance: FacebookApiException
Exemplo n.º 1
0
        /// <summary>
        /// Gets the graph exception if possible.
        /// </summary>
        /// <param name="result">The web request result object to check for exception information.</param>
        /// <returns>A Facebook API exception or null.</returns>
        internal static FacebookApiException GetGraphException(object result)
        {
            // Note: broke down GetGraphException into different method for unit testing.
            FacebookApiException resultException = null;

            if (result != null)
            {
                var responseDict = result as IDictionary <string, object>;
                if (responseDict != null)
                {
                    if (responseDict.ContainsKey("error"))
                    {
                        var error = responseDict["error"] as IDictionary <string, object>;
                        if (error != null)
                        {
                            var errorType    = error["type"] as string;
                            var errorMessage = error["message"] as string;

                            // Check to make sure the correct data is in the response
                            if (!String.IsNullOrEmpty(errorType) && !String.IsNullOrEmpty(errorMessage))
                            {
                                // We don't include the inner exception because it is not needed and is always a WebException.
                                // It is easier to understand the error if we use Facebook's error message.
                                if (errorType == "OAuthException")
                                {
                                    resultException = new FacebookOAuthException(errorMessage, errorType);
                                }
                                else if (errorType == "API_EC_TOO_MANY_CALLS" || (errorMessage != null && errorMessage.Contains("request limit reached")))
                                {
                                    resultException = new FacebookApiLimitException(errorMessage, errorType);
                                }
                                else
                                {
                                    resultException = new FacebookApiException(errorMessage, errorType);
                                }
                            }
                        }
                    }
                }
            }

            return(resultException);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the rest exception if possible.
        /// </summary>
        /// <param name="result">The web request result object to check for exception information.</param>
        /// <returns>The Facebook API exception or null.</returns>
        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);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets the rest exception if possible.
        /// </summary>
        /// <param name="result">The web request result object to check for exception information.</param>
        /// <returns>The Facebook API exception or null.</returns>
        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;
        }
Exemplo n.º 4
0
        /// <summary>
        /// Gets the graph exception if possible.
        /// </summary>
        /// <param name="result">The web request result object to check for exception information.</param>
        /// <returns>A Facebook API exception or null.</returns>
        internal static FacebookApiException GetGraphException(object result)
        {
            // Note: broke down GetGraphException into different method for unit testing.
            FacebookApiException resultException = null;
            if (result != null)
            {
                var responseDict = result as IDictionary<string, object>;
                if (responseDict != null)
                {
                    if (responseDict.ContainsKey("error"))
                    {
                        var error = responseDict["error"] as IDictionary<string, object>;
                        if (error != null)
                        {
                            var errorType = error["type"] as string;
                            var errorMessage = error["message"] as string;

                            // Check to make sure the correct data is in the response
                            if (!String.IsNullOrEmpty(errorType) && !String.IsNullOrEmpty(errorMessage))
                            {
                                // We don't include the inner exception because it is not needed and is always a WebException.
                                // It is easier to understand the error if we use Facebook's error message.
                                if (errorType == "OAuthException")
                                {
                                    resultException = new FacebookOAuthException(errorMessage, errorType);
                                }
                                else if (errorType == "API_EC_TOO_MANY_CALLS" || (errorMessage != null && errorMessage.Contains("request limit reached")))
                                {
                                    resultException = new FacebookApiLimitException(errorMessage, errorType);
                                }
                                else
                                {
                                    resultException = new FacebookApiException(errorMessage, errorType);
                                }
                            }
                        }
                        else
                        {
                            long? errorNumber = null;
                            if (responseDict["error"] is long)
                                errorNumber = (long)responseDict["error"];
                            if (errorNumber == null && responseDict["error"] is int)
                                errorNumber = (int)responseDict["error"];
                            string errorDescription = null;
                            if (responseDict.ContainsKey("error_description"))
                                errorDescription = responseDict["error_description"] as string;
                            if (errorNumber != null && !string.IsNullOrEmpty(errorDescription))
                            {
                                if (errorNumber == 190)
                                {
                                    resultException = new FacebookOAuthException(errorDescription, "API_EC_PARAM_ACCESS_TOKEN");
                                }
                                else
                                {
                                    resultException = new FacebookApiException(errorDescription, errorNumber.Value.ToString());
                                }
                            }
                        }
                    }
                }
            }

            return resultException;
        }
Exemplo n.º 5
0
        internal static Exception GetException(HttpHelper httpHelper, object result)
        {
            if (result == null)
            {
                return(null);
            }

            var responseDict = result as IDictionary <string, object>;

            if (responseDict == null)
            {
                return(null);
            }

            FacebookApiException resultException = null;

            if (httpHelper != null)
            {
                var response    = httpHelper.HttpWebResponse;
                var responseUri = response.ResponseUri;

                // legacy rest api
                if (responseUri.Host == "api.facebook.com" ||
                    responseUri.Host == "api-read.facebook.com" ||
                    responseUri.Host == "api-video.facebook.com" ||
                    responseUri.Host == "api.beta.facebook.com" ||
                    responseUri.Host == "api-read.beta.facebook.com" ||
                    responseUri.Host == "api-video.facebook.com")
                {
                    if (responseDict.ContainsKey("error_code"))
                    {
                        string errorCode = responseDict["error_code"].ToString();
                        string errorMsg  = null;
                        if (responseDict.ContainsKey("error_msg"))
                        {
                            errorMsg = responseDict["error_msg"] as string;
                        }

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

                    return(null);
                }
            }

            // graph api error
            if (responseDict.ContainsKey("error"))
            {
                var error = responseDict["error"] as IDictionary <string, object>;
                if (error != null)
                {
                    var errorType    = error["type"] as string;
                    var errorMessage = error["message"] as string;
                    int errorCode    = 0;

                    if (error.ContainsKey("code"))
                    {
                        int.TryParse(error["code"].ToString(), out errorCode);
                    }

                    // Check to make sure the correct data is in the response
                    if (!string.IsNullOrEmpty(errorType) && !string.IsNullOrEmpty(errorMessage))
                    {
                        // We don't include the inner exception because it is not needed and is always a WebException.
                        // It is easier to understand the error if we use Facebook's error message.
                        if (errorType == "OAuthException")
                        {
                            resultException = new FacebookOAuthException(errorMessage, errorType, errorCode);
                        }
                        else if (errorType == "API_EC_TOO_MANY_CALLS" || (errorMessage.Contains("request limit reached")))
                        {
                            resultException = new FacebookApiLimitException(errorMessage, errorType);
                        }
                        else
                        {
                            resultException = new FacebookApiException(errorMessage, errorType, errorCode);
                        }
                    }
                }
                else
                {
                    long?errorNumber = null;
                    if (responseDict["error"] is long)
                    {
                        errorNumber = (long)responseDict["error"];
                    }
                    if (errorNumber == null && responseDict["error"] is int)
                    {
                        errorNumber = (int)responseDict["error"];
                    }
                    string errorDescription = null;
                    if (responseDict.ContainsKey("error_description"))
                    {
                        errorDescription = responseDict["error_description"] as string;
                    }
                    if (errorNumber != null && !string.IsNullOrEmpty(errorDescription))
                    {
                        if (errorNumber == 190)
                        {
                            resultException = new FacebookOAuthException(errorDescription, "API_EC_PARAM_ACCESS_TOKEN");
                        }
                        else
                        {
                            resultException = new FacebookApiException(errorDescription, errorNumber.Value.ToString(CultureInfo.InvariantCulture));
                        }
                    }
                }
            }

            return(resultException);
        }
Exemplo n.º 6
0
        internal static Exception GetException(HttpHelper httpHelper, object result)
        {
            if (result == null)
                return null;

            var responseDict = result as IDictionary<string, object>;
            if (responseDict == null)
                return null;

            FacebookApiException resultException = null;

            if (httpHelper != null)
            {
                var response = httpHelper.HttpWebResponse;
                var responseUri = response.ResponseUri;

                // legacy rest api
                if (responseUri.Host == "api.facebook.com" ||
                    responseUri.Host == "api-read.facebook.com" ||
                    responseUri.Host == "api-video.facebook.com" ||
                    responseUri.Host == "api.beta.facebook.com" ||
                    responseUri.Host == "api-read.beta.facebook.com" ||
                    responseUri.Host == "api-video.facebook.com")
                {
                    if (responseDict.ContainsKey("error_code"))
                    {
                        string errorCode = responseDict["error_code"].ToString();
                        string errorMsg = null;
                        if (responseDict.ContainsKey("error_msg"))
                            errorMsg = responseDict["error_msg"] as string;

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

            // graph api error
            if (responseDict.ContainsKey("error"))
            {
                var error = responseDict["error"] as IDictionary<string, object>;
                if (error != null)
                {
                    var errorType = error["type"] as string;
                    var errorMessage = error["message"] as string;

                    // Check to make sure the correct data is in the response
                    if (!string.IsNullOrEmpty(errorType) && !string.IsNullOrEmpty(errorMessage))
                    {
                        // We don't include the inner exception because it is not needed and is always a WebException.
                        // It is easier to understand the error if we use Facebook's error message.
                        if (errorType == "OAuthException")
                            resultException = new FacebookOAuthException(errorMessage, errorType);
                        else if (errorType == "API_EC_TOO_MANY_CALLS" || (errorMessage.Contains("request limit reached")))
                            resultException = new FacebookApiLimitException(errorMessage, errorType);
                        else
                            resultException = new FacebookApiException(errorMessage, errorType);
                    }
                }
                else
                {
                    long? errorNumber = null;
                    if (responseDict["error"] is long)
                        errorNumber = (long)responseDict["error"];
                    if (errorNumber == null && responseDict["error"] is int)
                        errorNumber = (int)responseDict["error"];
                    string errorDescription = null;
                    if (responseDict.ContainsKey("error_description"))
                        errorDescription = responseDict["error_description"] as string;
                    if (errorNumber != null && !string.IsNullOrEmpty(errorDescription))
                    {
                        if (errorNumber == 190)
                            resultException = new FacebookOAuthException(errorDescription, "API_EC_PARAM_ACCESS_TOKEN");
                        else
                            resultException = new FacebookApiException(errorDescription, errorNumber.Value.ToString(CultureInfo.InvariantCulture));
                    }
                }
            }

            return resultException;
        }