public async Task ReadErrorResponse_AlternateErrorFormat_ReturnsNotNull() { // arrange const string content = "{\"error\":\"invalid_grant\",\"error_description\":\"Invalid authorization code\"}"; var response = new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent(content, Encoding.Unicode, "application/json") }; var j = JsonConvert.DeserializeObject(content) as JObject; var t = j["error"].Type; Debug.WriteLine($"error type = {t}"); Debug.WriteLine($"error_description = {j["error_description"].Value<string>()}"); if (j.ContainsKey("foo")) { var t2 = j["foo"].Type; Debug.WriteLine($"foo type = {t2}"); } // act var error = await SpotifyApiErrorException.ReadErrorResponse(response); // assert Assert.IsNotNull(error); }
public async Task ReadErrorResponse_ValidJson_ReturnsNotNull() { // arrange const string content = @"{ ""error"": { ""status"": 404, ""message"": ""No active device found"" } }"; var response = new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new StringContent(content, Encoding.Unicode, "application/json") }; var j = JsonConvert.DeserializeObject(content) as JObject; var t = j["error"].Type; Debug.WriteLine($"error type = {t}"); Debug.WriteLine($"error.message = {j["error"].Value<string>("message")}"); Debug.WriteLine($"error.foo = {j["error"].Value<string>("foo")}"); // act var error = await SpotifyApiErrorException.ReadErrorResponse(response); // assert Assert.IsNotNull(error); }
public async Task ReadErrorResponse_NoContent_ReturnsNull() { // arrange var response = new HttpResponseMessage(HttpStatusCode.NotFound); // act var error = await SpotifyApiErrorException.ReadErrorResponse(response); // assert Assert.IsNull(error); }
/// <summary> /// Checks the reponse from the Spotify Server for an error. /// </summary> /// <param name="response"></param> /// <returns>If a Spotify API Error message is parsed, a <see cref="SpotifyApiErrorException"/> is thrown. /// If any other error is returned a <see cref="HttpResponseMessageException"/> if thrown. If no error /// the method returns void.</returns> public static async Task CheckForErrors(HttpResponseMessage response) { if (!response.IsSuccessStatusCode) { var error = await SpotifyApiErrorException.ReadErrorResponse(response); if (error != null) { throw new SpotifyApiErrorException(response.StatusCode, error); } response.EnsureSuccessStatusCode(); // not a Spotify API Error so throw HttpResponseMessageException } }
public async Task ReadErrorResponse_EmptyPlainText_ReturnsNull() { // arrange var response = new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new StringContent("", Encoding.Unicode, "text/plain") }; // act var error = await SpotifyApiErrorException.ReadErrorResponse(response); // assert Assert.IsNull(error); }
public static IMessageActivity SpotifyError(ConversationInfo info, SpotifyApiErrorException ex, string command) { var heroCard = NewHeroCard(); heroCard.Buttons.Add( new CardAction { Title = $"Try again", Value = $"{RingoBotHelper.RingoHandleIfGroupChat(info)}{command}", Type = ActionTypes.ImBack, }); return(MessageAttachment( heroCard, $"Ringo can't talk to Spotify right now 🤔 Please try again in a minute. Spotify says: \"{ex.Message}\"")); }