DeserializeError() public static method

public static DeserializeError ( string jsonErrorText ) : Exception
jsonErrorText string
return System.Exception
Esempio n. 1
0
 internal void DispatchResults(WoopsaJsonData jsonData)
 {
     if (jsonData.IsArray)
     {
         for (int i = 0; i < jsonData.Length; i++)
         {
             WoopsaJsonData      item = jsonData[i];
             int                 id   = item[WoopsaFormat.KeyId];
             WoopsaClientRequest request;
             if (_clientRequestsById.TryGetValue(id, out request))
             {
                 WoopsaJsonData result = item[WoopsaFormat.KeyResult];
                 if (result.ContainsKey(WoopsaFormat.KeyError))
                 {
                     request.Result = new WoopsaClientRequestResult()
                     {
                         Error      = WoopsaFormat.DeserializeError(result.AsText),
                         ResultType = WoopsaClientRequestResultType.Error
                     };
                 }
                 else if (request.Request.Verb == WoopsaFormat.VerbMeta)
                 {
                     request.Result = new WoopsaClientRequestResult()
                     {
                         Meta       = WoopsaFormat.DeserializeMeta(result.AsText),
                         ResultType = WoopsaClientRequestResultType.Meta
                     };
                 }
                 else
                 {
                     request.Result = new WoopsaClientRequestResult()
                     {
                         Value      = WoopsaFormat.DeserializeWoopsaValue(result.AsText),
                         ResultType = WoopsaClientRequestResultType.Value
                     };
                 }
                 request.IsDone = true;
             }
             else
             {
                 throw new WoopsaException(
                           string.Format("MultiRequest received a result for an unkwnon request Id={0}", id));
             }
         }
     }
     else
     {
         throw new WoopsaException("MultiRequest response has invalid format");
     }
 }
        private string Request(string path, NameValueCollection postData, TimeSpan timeout)
        {
            if (!_terminating)
            {
                var request = (HttpWebRequest)WebRequest.Create(Url + path);
                request.CachePolicy = _cachePolicy;

                // TODO : affiner l'optimisation de performance
                request.ServicePoint.UseNagleAlgorithm = false;
                request.ServicePoint.Expect100Continue = false;

                HttpWebResponse response = null;

                lock (_pendingRequests)
                    _pendingRequests.Add(request);
                try
                {
                    try
                    {
                        if (Username != null)
                        {
                            request.Credentials = new NetworkCredential(Username, Password);
                        }

                        request.Timeout = (int)timeout.TotalMilliseconds;

                        if (postData != null)
                        {
                            request.Method      = "POST";
                            request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
                        }
                        else
                        {
                            request.Method = "GET";
                        }

                        request.Accept = "*/*";

                        if (postData != null)
                        {
                            StringBuilder stringBuilder = new StringBuilder();
                            for (var i = 0; i < postData.Count; i++)
                            {
                                string key = postData.AllKeys[i];
                                stringBuilder.AppendFormat(i == postData.Count - 1 ? "{0}={1}" : "{0}={1}&",
                                                           HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(postData[key]));
                            }
                            using (var writer = new StreamWriter(request.GetRequestStream()))
                                writer.Write(stringBuilder.ToString());
                        }

                        response = (HttpWebResponse)request.GetResponse();
                        SetLastCommunicationSuccessful(true);
                    }
                    catch (WebException exception)
                    {
                        // This could be an HTTP error, in which case
                        // we actually have a response (with the HTTP
                        // status and error)
                        response = (HttpWebResponse)exception.Response;
                        if (response == null)
                        {
                            SetLastCommunicationSuccessful(false);
                            // Sometimes, we can make the request, but the server dies
                            // before we get a reply - in that case the Response
                            // is null, so we re-throw the exception
                            throw;
                        }
                    }
                    catch (Exception)
                    {
                        SetLastCommunicationSuccessful(false);
                        throw;
                    }

                    string resultString;
                    using (var reader = new StreamReader(response.GetResponseStream()))
                    {
                        resultString = reader.ReadToEnd();
                    }

                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        if (response.ContentType == MIMETypes.Application.JSON)
                        {
                            var exception = WoopsaFormat.DeserializeError(resultString);
                            throw exception;
                        }

                        throw new WoopsaException(response.StatusDescription);
                    }

                    return(resultString);
                }
                finally
                {
                    lock (_pendingRequests)
                        _pendingRequests.Remove(request);
                    if (response != null)
                    {
                        response.Close();
                    }
                }
            }
            else
            {
                throw new ObjectDisposedException(GetType().Name);
            }
        }