コード例 #1
0
        /// <summary>
        /// Reads an <see cref="HttpResponseMessage"/> to parse a <see cref="SpotifyApiError"/>.
        /// </summary>
        /// <param name="response">The <see cref="HttpResponseMessage"/>.</param>
        /// <returns>An instance of <see cref="SpotifyApiError"/>.</returns>
        public static async Task <SpotifyApiError> ReadErrorResponse(HttpResponseMessage response)
        {
            // if no content
            if (response.Content == null)
            {
                return(null);
            }

            // if not JSON content type
            if (response.Content.Headers.ContentType?.MediaType != "application/json")
            {
                return(null);
            }

            var content = await response.Content.ReadAsStringAsync();

            Logger.Debug(content, nameof(SpotifyApiErrorException));

            // if empty body
            if (string.IsNullOrWhiteSpace(content))
            {
                return(null);
            }

            var error = new SpotifyApiError {
                Json = content
            };

            // interrogate properties to detect error json type
            var deserialized = JsonConvert.DeserializeObject(content) as JObject;

            // if no error property
            if (!deserialized.ContainsKey("error"))
            {
                return(error);
            }

            switch (deserialized["error"].Type)
            {
            case JTokenType.Object:
                error.Message = deserialized["error"].Value <string>("message");
                break;

            case JTokenType.String:
                error.Message = deserialized["error_description"].Value <string>();
                break;
            }

            return(error);
        }
コード例 #2
0
 public SpotifyApiErrorException(HttpStatusCode statusCode, SpotifyApiError spotifyApiError) : base(spotifyApiError?.Message)
 {
     HttpStatusCode  = statusCode;
     SpotifyApiError = spotifyApiError;
 }