GetGraphException() private method

private GetGraphException ( WebException exception ) : FacebookApiException
exception WebException
return FacebookApiException
        /// <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);
        }
        internal protected virtual object Api(string path, IDictionary <string, object> parameters, HttpMethod httpMethod, Type resultType)
        {
            var mergedParameters = FacebookUtils.Merge(null, parameters);

            if (!mergedParameters.ContainsKey("access_token") && !string.IsNullOrEmpty(AccessToken))
            {
                mergedParameters["access_token"] = AccessToken;
            }

            Uri    requestUrl;
            string contentType;

            byte[] postData = BuildRequestData(path, mergedParameters, httpMethod, out requestUrl, out contentType);

            var jsonString = MakeRequest(httpMethod, requestUrl, postData, contentType);

            var json = JsonSerializer.Current.DeserializeObject(jsonString);

            FacebookApiException facebookApiException =
                ExceptionFactory.GetGraphException(json) ??
                ExceptionFactory.CheckForRestException(DomainMaps, requestUrl, json);

            if (facebookApiException != null)
            {
                throw facebookApiException;
            }

            return(resultType == null ? json : JsonSerializer.Current.DeserializeObject(jsonString, resultType));
        }
        private FacebookApiEventArgs GetApiEventArgs(AsyncCompletedEventArgs e, string json, out HttpMethod httpMethod)
        {
            var state = (WebClientStateContainer)e.UserState;

            httpMethod = state.Method;

            var cancelled = e.Cancelled;
            var userState = state.UserState;
            var error     = e.Error;

            // Check for Graph Exception
            var webException = error as WebExceptionWrapper;

            if (webException != null)
            {
                error = ExceptionFactory.GetGraphException(webException) ?? (Exception)webException.ActualWebException;
            }

            if (error == null)
            {
                var jsonObj = JsonSerializer.Current.DeserializeObject(json);

                // we need to check for graph exception here again coz fb return 200ok
                // for https://graph.facebook.com/i_dont_exist
                error = ExceptionFactory.GetGraphException(jsonObj) ??
                        ExceptionFactory.CheckForRestException(DomainMaps, state.RequestUri, jsonObj);
            }

            var args = new FacebookApiEventArgs(error, cancelled, userState, json, state.IsBatchRequest);

            return(args);
        }
        private static string MakeRequest(HttpMethod httpMethod, Uri requestUrl, byte[] postData, string contentType)
        {
            var request = (HttpWebRequest)WebRequest.Create(requestUrl);

            request.Method = FacebookUtils.ConvertToString(httpMethod); // Set the http method GET, POST, etc.

            if (postData != null)
            {
                request.ContentLength = postData.Length;
                request.ContentType   = contentType;
                using (Stream dataStream = request.GetRequestStream())
                {
                    dataStream.Write(postData, 0, postData.Length);
                }
            }

            var       responseData = string.Empty;
            Exception exception    = null;

            try
            {
                var response = (HttpWebResponse)request.GetResponse();
                using (var streamReader = new StreamReader(response.GetResponseStream()))
                {
                    responseData = streamReader.ReadToEnd();
                }

                response.Close();
            }
            catch (WebException ex)
            {
                exception = (Exception)ExceptionFactory.GetGraphException(ex) ?? ex;
            }
            finally
            {
                if (exception != null)
                {
                    throw exception;
                }
            }

            return(responseData);
        }