예제 #1
0
        /// <summary>
        /// Checks an <see cref="IRestResponse" /> for exceptions or invalid responses. Throws an exception when necessary.
        /// </summary>
        /// <param name="response">The response.</param>
        static void CheckResponseExceptions(IRestResponse response)
        {
            if (response.StatusCode != HttpStatusCode.OK && response.StatusCode != HttpStatusCode.Created)
            {
                string json    = Encoding.UTF8.GetString(response.RawBytes);
                string message = "";
                var    error   = new ConvertKitError();

                if (string.IsNullOrEmpty(json) == false)
                {
                    var parsed = JToken.Parse(string.IsNullOrEmpty(json) ? "{}" : json);

                    if (parsed.Any(x => x.Path == "errors"))
                    {
                        message     = parsed.Value <string>("message");
                        error.Error = parsed.Value <string>("error");
                    }
                    else
                    {
                        error.Error = $"Response did not indicate success. Status: {(int)response.StatusCode} {response.StatusDescription}.";
                        message     = json;
                    }
                }

                throw new ConvertKitException(response.StatusCode, error, message);
            }

            if (response.ErrorException != null)
            {
                //Checking this second, because Shopify errors sometimes return incomplete objects along with errors,
                //which cause Json deserialization to throw an exception. Parsing the Shopify error is more important
                //than throwing this deserialization exception.
                throw response.ErrorException;
            }
        }
 public ConvertKitException(HttpStatusCode httpStatusCode, ConvertKitError convertKitError, string message) : base(message)
 {
     HttpStatusCode  = httpStatusCode;
     ConvertKitError = convertKitError;
 }