From 8adc0063addb9daf7a3c996fc320301bbaef45a8 Mon Sep 17 00:00:00 2001 From: Tim <13688718+Mahsaap@users.noreply.github.com> Date: Wed, 13 Nov 2024 13:40:18 -0400 Subject: [PATCH 1/4] Create TwitchErrorResponse.cs Redo of 387 PR to remove extra commits from switching from Master to Dev branch. - Add Twitch Error Response object. --- .../HttpCallHandlers/TwitchErrorResponse.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 TwitchLib.Api.Core/HttpCallHandlers/TwitchErrorResponse.cs diff --git a/TwitchLib.Api.Core/HttpCallHandlers/TwitchErrorResponse.cs b/TwitchLib.Api.Core/HttpCallHandlers/TwitchErrorResponse.cs new file mode 100644 index 00000000..ebab57df --- /dev/null +++ b/TwitchLib.Api.Core/HttpCallHandlers/TwitchErrorResponse.cs @@ -0,0 +1,16 @@ +using Newtonsoft.Json; + +namespace TwitchLib.Api.Core.HttpCallHandlers +{ + public class TwitchErrorResponse + { + [JsonProperty("error")] + public string Error; + + [JsonProperty("status")] + public int Status; + + [JsonProperty("message")] + public string Message; + } +} From 6598b6986bed70888a05c6e71e014a97d3c32871 Mon Sep 17 00:00:00 2001 From: Tim <13688718+Mahsaap@users.noreply.github.com> Date: Wed, 13 Nov 2024 13:41:58 -0400 Subject: [PATCH 2/4] Update TwitchHttpClient.cs redo 378 --- .../HttpCallHandlers/TwitchHttpClient.cs | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/TwitchLib.Api.Core/HttpCallHandlers/TwitchHttpClient.cs b/TwitchLib.Api.Core/HttpCallHandlers/TwitchHttpClient.cs index 0e713e75..03af599a 100644 --- a/TwitchLib.Api.Core/HttpCallHandlers/TwitchHttpClient.cs +++ b/TwitchLib.Api.Core/HttpCallHandlers/TwitchHttpClient.cs @@ -6,6 +6,7 @@ using System.Text; using System.Threading.Tasks; using Microsoft.Extensions.Logging; +using Newtonsoft.Json; using TwitchLib.Api.Core.Common; using TwitchLib.Api.Core.Enums; using TwitchLib.Api.Core.Exceptions; @@ -120,34 +121,36 @@ public async Task RequestReturnResponseCodeAsync(string url, string method, return (int)response.StatusCode; } - private void HandleWebException(HttpResponseMessage errorResp) + private async Task HandleWebException(HttpResponseMessage errorResp) { + var bodyContent = await errorResp.Content.ReadAsStringAsync(); + var deserializedError = JsonConvert.DeserializeObject(bodyContent); + switch (errorResp.StatusCode) { case HttpStatusCode.BadRequest: - throw new BadRequestException("Your request failed because either: \n 1. Your ClientID was invalid/not set. \n 2. Your refresh token was invalid. \n 3. You requested a username when the server was expecting a user ID.", errorResp); + throw new BadRequestException($"{deserializedError.Error} - {deserializedError.Message}", errorResp); case HttpStatusCode.Unauthorized: var authenticateHeader = errorResp.Headers.WwwAuthenticate; if (authenticateHeader == null || authenticateHeader.Count <= 0) - throw new BadScopeException("Your request was blocked due to bad credentials (Do you have the right scope for your access token?).", errorResp); - throw new TokenExpiredException("Your request was blocked due to an expired Token. Please refresh your token and update your API instance settings.", errorResp); + throw new BadScopeException($"{deserializedError.Error} - {deserializedError.Message}", errorResp); + throw new TokenExpiredException($"{deserializedError.Error} - {deserializedError.Message}", errorResp); case HttpStatusCode.NotFound: - throw new BadResourceException("The resource you tried to access was not valid.", errorResp); + throw new BadResourceException($"{deserializedError.Error} - {deserializedError.Message}", errorResp); case (HttpStatusCode)429: - errorResp.Headers.TryGetValues("Ratelimit-Reset", out var resetTime); - throw new TooManyRequestsException("You have reached your rate limit. Too many requests were made", resetTime.FirstOrDefault(), errorResp); + errorResp.Headers.TryGetValues($"Ratelimit-Reset", out var resetTime); + throw new TooManyRequestsException($"{deserializedError.Error} - {deserializedError.Message}", resetTime.FirstOrDefault(), errorResp); case HttpStatusCode.BadGateway: - throw new BadGatewayException("The API answered with a 502 Bad Gateway. Please retry your request", errorResp); + throw new BadGatewayException($"{deserializedError.Error} - {deserializedError.Message}", errorResp); case HttpStatusCode.GatewayTimeout: - throw new GatewayTimeoutException("The API answered with a 504 Gateway Timeout. Please retry your request", errorResp); + throw new GatewayTimeoutException($"{deserializedError.Error} - {deserializedError.Message}", errorResp); case HttpStatusCode.InternalServerError: - throw new InternalServerErrorException("The API answered with a 500 Internal Server Error. Please retry your request", errorResp); + throw new InternalServerErrorException($"{deserializedError.Error} - {deserializedError.Message}", errorResp); case HttpStatusCode.Forbidden: - throw new BadTokenException("The token provided in the request did not match the associated user. Make sure the token you're using is from the resource owner (streamer? viewer?)", errorResp); + throw new BadTokenException($"{deserializedError.Error} - {deserializedError.Message}", errorResp); default: - throw new HttpRequestException("Something went wrong during the request! Please try again later"); + throw new HttpRequestException($"Something went wrong during the request! Please try again later \n {deserializedError.Message}"); } } - } -} \ No newline at end of file +} From 5687121a0ed839b3131dfdfeaabfca6be79d64f9 Mon Sep 17 00:00:00 2001 From: Tim <13688718+Mahsaap@users.noreply.github.com> Date: Wed, 13 Nov 2024 13:44:55 -0400 Subject: [PATCH 3/4] Update TwitchHttpClient.cs add awaits to HandleWebException --- TwitchLib.Api.Core/HttpCallHandlers/TwitchHttpClient.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TwitchLib.Api.Core/HttpCallHandlers/TwitchHttpClient.cs b/TwitchLib.Api.Core/HttpCallHandlers/TwitchHttpClient.cs index 03af599a..655c7f0c 100644 --- a/TwitchLib.Api.Core/HttpCallHandlers/TwitchHttpClient.cs +++ b/TwitchLib.Api.Core/HttpCallHandlers/TwitchHttpClient.cs @@ -44,7 +44,7 @@ public async Task PutBytesAsync(string url, byte[] payload) var response = await _http.PutAsync(new Uri(url), new ByteArrayContent(payload)).ConfigureAwait(false); if (!response.IsSuccessStatusCode) - HandleWebException(response); + await HandleWebException(response); } /// @@ -95,7 +95,7 @@ public async Task> GeneralRequestAsync(string url, str return new KeyValuePair((int)response.StatusCode, respStr); } - HandleWebException(response); + await HandleWebException(response); return new KeyValuePair(0, null); } From 66e3e8ad7da19b04648d7526052fd18130053b3e Mon Sep 17 00:00:00 2001 From: Tim <13688718+Mahsaap@users.noreply.github.com> Date: Wed, 13 Nov 2024 13:55:21 -0400 Subject: [PATCH 4/4] fix string for HttpRequestException --- TwitchLib.Api.Core/HttpCallHandlers/TwitchHttpClient.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TwitchLib.Api.Core/HttpCallHandlers/TwitchHttpClient.cs b/TwitchLib.Api.Core/HttpCallHandlers/TwitchHttpClient.cs index 655c7f0c..c53f79ae 100644 --- a/TwitchLib.Api.Core/HttpCallHandlers/TwitchHttpClient.cs +++ b/TwitchLib.Api.Core/HttpCallHandlers/TwitchHttpClient.cs @@ -149,7 +149,7 @@ private async Task HandleWebException(HttpResponseMessage errorResp) case HttpStatusCode.Forbidden: throw new BadTokenException($"{deserializedError.Error} - {deserializedError.Message}", errorResp); default: - throw new HttpRequestException($"Something went wrong during the request! Please try again later \n {deserializedError.Message}"); + throw new HttpRequestException($"{deserializedError.Error} - {deserializedError.Message}"); } } }