コード例 #1
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="errorResponse">The response that caused the exception</param>
 /// <param name="wex">The original web exception.  This becomes the inner exception of ths exception</param>
 public ChargifyException(HttpWebResponse errorResponse, WebException wex) :
     base(string.Format("The server returned '{0}' with the status code {1} ({1:d}).", errorResponse.StatusDescription, errorResponse.StatusCode), wex)
 {
     _statusDescription = errorResponse.StatusDescription;
     _statusCode        = errorResponse.StatusCode;
     // if there are any errors, parse them for user consumption
     _errors = ChargifyError.ParseChargifyErrors(errorResponse);
 }
コード例 #2
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="errorResponse">The response that caused the exception</param>
 /// <param name="wex">The original web exception.  This becomes the inner exception of ths exception</param>
 public ChargifyException(HttpWebResponse errorResponse, WebException wex) :
     base($"The server returned '{errorResponse.StatusDescription}' with the status code {errorResponse.StatusCode} ({errorResponse.StatusCode:d}).", wex)
 {
     StatusDescription = errorResponse.StatusDescription;
     StatusCode        = errorResponse.StatusCode;
     // if there are any errors, parse them for user consumption
     ErrorMessages = ChargifyError.ParseChargifyErrors(errorResponse);
 }
コード例 #3
0
        /// <summary>
        /// Method to parse the errors returned from Chargify after an unsuccessful interaction
        /// </summary>
        /// <param name="response">The HttpWebResponse instance which contains the error response</param>
        /// <returns>The list of errors returned from Chargify</returns>
        internal static List <ChargifyError> ParseChargifyErrors(HttpWebResponse response)
        {
            if (response != null)
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    string errorResponse        = reader.ReadToEnd();
                    List <ChargifyError> errors = new List <ChargifyError>();

                    // Response is frequently " " ...
                    if (string.IsNullOrEmpty(errorResponse.Trim()))
                    {
                        return(errors);
                    }

                    if (errorResponse.IsXml())
                    {
                        // New way - Linq-y
                        XDocument xdoc    = XDocument.Parse(errorResponse);
                        var       results = from e in xdoc.Descendants("errors")
                                            select new ChargifyError
                        {
                            Message = e.Value
                        };
                        errors = results.ToList();
                    }
                    else if (errorResponse.IsJSON())
                    {
                        // slightly different json response from the usual
                        int        position = 0;
                        JsonObject obj      = JsonObject.Parse(errorResponse, ref position);
                        if (obj.ContainsKey("errors"))
                        {
                            JsonArray array = obj["errors"] as JsonArray;
                            for (int i = 0; i <= array.Length - 1; i++)
                            {
                                if (((array.Items[i] as JsonString) != null) && (!string.IsNullOrEmpty((array.Items[i] as JsonString).Value)))
                                {
                                    JsonString    errorStr = array.Items[i] as JsonString;
                                    ChargifyError anError  = new ChargifyError(errorStr);
                                    if (!errors.Contains(anError))
                                    {
                                        errors.Add(anError);
                                    }
                                }
                            }
                        }
                    }
                    return(errors);
                }
            }
            else
            {
                throw new ChargifyNetException("Unknown Error");
            }
        }
コード例 #4
0
        /// <summary>
        /// Method to parse the errors returned from Chargify after an unsuccessful interaction
        /// </summary>
        /// <param name="response">The HttpWebResponse instance which contains the error response</param>
        /// <returns>The list of errors returned from Chargify</returns>
        internal static List<ChargifyError> ParseChargifyErrors(HttpWebResponse response)
        {
            if (response != null)
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    string errorResponse = reader.ReadToEnd();
                    List<ChargifyError> errors = new List<ChargifyError>();

                    // Response is frequently " " ...
                    if (string.IsNullOrEmpty(errorResponse.Trim())) return errors;

                    if (errorResponse.IsXml())
                    {
                        // New way - Linq-y
                        XDocument xdoc = XDocument.Parse(errorResponse);
                        if (xdoc.Descendants("error").Count() > 0)
                        {
                            var results = from e in xdoc.Descendants("error")
                                          select new ChargifyError
                                          {
                                              Message = e.Value
                                          };
                            errors = results.ToList();
                        }
                        else
                        {
                            var results = from e in xdoc.Descendants("errors")
                                          select new ChargifyError
                                          {
                                              Message = e.Value
                                          };
                            errors = results.ToList();
                        }
                    }
                    else if (errorResponse.IsJSON())
                    {
                        // slightly different json response from the usual
                        int position = 0;
                        JsonObject obj = JsonObject.Parse(errorResponse, ref position);
                        if (obj.ContainsKey("errors"))
                        {
                            JsonArray array = obj["errors"] as JsonArray;
                            for (int i = 0; i <= array.Length - 1; i++)
                            {
                                if (((array.Items[i] as JsonString) != null) && (!string.IsNullOrEmpty((array.Items[i] as JsonString).Value)))
                                {
                                    JsonString errorStr = array.Items[i] as JsonString;
                                    ChargifyError anError = new ChargifyError(errorStr);
                                    if (!errors.Contains(anError))
                                    {
                                        errors.Add(anError);
                                    }
                                }
                            }
                        }
                    }
                    return errors;
                }
            }
            else
            {
                throw new ChargifyNetException("Unknown Error");
            }
        }