From 500f5f434a0aa7e0eb027c6da80f96f9d4c06123 Mon Sep 17 00:00:00 2001 From: Alex Gravely Date: Sun, 18 Feb 2018 19:19:10 -0500 Subject: [PATCH] Add request info to HttpException & RateLimitedException (#957) * Add request info to RateLimitedException * Remove Promise from interface. * Add Request to HttpException. --- src/Discord.Net.Core/Net/HttpException.cs | 6 ++++-- src/Discord.Net.Core/Net/IRequest.cs | 10 ++++++++++ src/Discord.Net.Core/Net/RateLimitedException.cs | 7 +++++-- .../Net/Queue/RequestQueueBucket.cs | 14 +++++++------- .../Net/Queue/Requests/RestRequest.cs | 4 ++-- .../Net/Queue/Requests/WebSocketRequest.cs | 4 ++-- 6 files changed, 30 insertions(+), 15 deletions(-) create mode 100644 src/Discord.Net.Core/Net/IRequest.cs diff --git a/src/Discord.Net.Core/Net/HttpException.cs b/src/Discord.Net.Core/Net/HttpException.cs index 1c872245c8..d0ee65b238 100644 --- a/src/Discord.Net.Core/Net/HttpException.cs +++ b/src/Discord.Net.Core/Net/HttpException.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Net; namespace Discord.Net @@ -8,11 +8,13 @@ public class HttpException : Exception public HttpStatusCode HttpCode { get; } public int? DiscordCode { get; } public string Reason { get; } + public IRequest Request { get; } - public HttpException(HttpStatusCode httpCode, int? discordCode = null, string reason = null) + public HttpException(HttpStatusCode httpCode, IRequest request, int? discordCode = null, string reason = null) : base(CreateMessage(httpCode, discordCode, reason)) { HttpCode = httpCode; + Request = request; DiscordCode = discordCode; Reason = reason; } diff --git a/src/Discord.Net.Core/Net/IRequest.cs b/src/Discord.Net.Core/Net/IRequest.cs new file mode 100644 index 0000000000..d3c708dd55 --- /dev/null +++ b/src/Discord.Net.Core/Net/IRequest.cs @@ -0,0 +1,10 @@ +using System; + +namespace Discord.Net +{ + public interface IRequest + { + DateTimeOffset? TimeoutAt { get; } + RequestOptions Options { get; } + } +} diff --git a/src/Discord.Net.Core/Net/RateLimitedException.cs b/src/Discord.Net.Core/Net/RateLimitedException.cs index e8572f911a..2d34d7bc22 100644 --- a/src/Discord.Net.Core/Net/RateLimitedException.cs +++ b/src/Discord.Net.Core/Net/RateLimitedException.cs @@ -1,12 +1,15 @@ -using System; +using System; namespace Discord.Net { public class RateLimitedException : TimeoutException { - public RateLimitedException() + public IRequest Request { get; } + + public RateLimitedException(IRequest request) : base("You are being rate limited.") { + Request = request; } } } diff --git a/src/Discord.Net.Rest/Net/Queue/RequestQueueBucket.cs b/src/Discord.Net.Rest/Net/Queue/RequestQueueBucket.cs index 2cc4b8a107..2d96ca7964 100644 --- a/src/Discord.Net.Rest/Net/Queue/RequestQueueBucket.cs +++ b/src/Discord.Net.Rest/Net/Queue/RequestQueueBucket.cs @@ -1,4 +1,4 @@ -using Newtonsoft.Json; +using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; #if DEBUG_LIMITS @@ -86,7 +86,7 @@ public async Task SendAsync(RestRequest request) Debug.WriteLine($"[{id}] (!) 502"); #endif if ((request.Options.RetryMode & RetryMode.Retry502) == 0) - throw new HttpException(HttpStatusCode.BadGateway, null); + throw new HttpException(HttpStatusCode.BadGateway, request, null); continue; //Retry default: @@ -106,7 +106,7 @@ public async Task SendAsync(RestRequest request) } catch { } } - throw new HttpException(response.StatusCode, code, reason); + throw new HttpException(response.StatusCode, request, code, reason); } } else @@ -163,7 +163,7 @@ private async Task EnterAsync(int id, RestRequest request) if (!isRateLimited) throw new TimeoutException(); else - throw new RateLimitedException(); + throw new RateLimitedException(request); } lock (_lock) @@ -182,12 +182,12 @@ private async Task EnterAsync(int id, RestRequest request) } if ((request.Options.RetryMode & RetryMode.RetryRatelimit) == 0) - throw new RateLimitedException(); + throw new RateLimitedException(request); if (resetAt.HasValue) { if (resetAt > timeoutAt) - throw new RateLimitedException(); + throw new RateLimitedException(request); int millis = (int)Math.Ceiling((resetAt.Value - DateTimeOffset.UtcNow).TotalMilliseconds); #if DEBUG_LIMITS Debug.WriteLine($"[{id}] Sleeping {millis} ms (Pre-emptive)"); @@ -198,7 +198,7 @@ private async Task EnterAsync(int id, RestRequest request) else { if ((timeoutAt.Value - DateTimeOffset.UtcNow).TotalMilliseconds < 500.0) - throw new RateLimitedException(); + throw new RateLimitedException(request); #if DEBUG_LIMITS Debug.WriteLine($"[{id}] Sleeping 500* ms (Pre-emptive)"); #endif diff --git a/src/Discord.Net.Rest/Net/Queue/Requests/RestRequest.cs b/src/Discord.Net.Rest/Net/Queue/Requests/RestRequest.cs index 8f160273af..bb5840ce29 100644 --- a/src/Discord.Net.Rest/Net/Queue/Requests/RestRequest.cs +++ b/src/Discord.Net.Rest/Net/Queue/Requests/RestRequest.cs @@ -1,11 +1,11 @@ -using Discord.Net.Rest; +using Discord.Net.Rest; using System; using System.IO; using System.Threading.Tasks; namespace Discord.Net.Queue { - public class RestRequest + public class RestRequest : IRequest { public IRestClient Client { get; } public string Method { get; } diff --git a/src/Discord.Net.Rest/Net/Queue/Requests/WebSocketRequest.cs b/src/Discord.Net.Rest/Net/Queue/Requests/WebSocketRequest.cs index 478289b592..81eb40b317 100644 --- a/src/Discord.Net.Rest/Net/Queue/Requests/WebSocketRequest.cs +++ b/src/Discord.Net.Rest/Net/Queue/Requests/WebSocketRequest.cs @@ -1,4 +1,4 @@ -using Discord.Net.WebSockets; +using Discord.Net.WebSockets; using System; using System.IO; using System.Threading; @@ -6,7 +6,7 @@ namespace Discord.Net.Queue { - public class WebSocketRequest + public class WebSocketRequest : IRequest { public IWebSocketClient Client { get; } public string BucketId { get; }