Skip to content

Commit

Permalink
Implement new error code and more error fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nihlus committed Jan 18, 2023
1 parent d7034f6 commit a94096d
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@ namespace Remora.Discord.API.Abstractions.Objects;
/// <summary>
/// Represents an error reported by the REST API.
/// </summary>
/// <remarks>
/// This is a combination of the "normal" REST error and the rate limiting error.
/// </remarks>
[PublicAPI]
public interface IRestError
{
/// <summary>
/// Gets the error code.
/// </summary>
DiscordError Code { get; }
Optional<DiscordError> Code { get; }

/// <summary>
/// Gets the per-property error details.
Expand All @@ -48,4 +51,14 @@ public interface IRestError
/// Gets a descriptive error message.
/// </summary>
string Message { get; }

/// <summary>
/// Gets a value indicating whether you are globally rate limited.
/// </summary>
Optional<bool> IsGlobal { get; }

/// <summary>
/// Gets the time (in seconds) after which the request should be retried.
/// </summary>
Optional<float> RetryAfter { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,11 @@ public enum DiscordError
/// </summary>
TagNamesMustBeUnique = 40061,

/// <summary>
/// A service resource is being rate limited.
/// </summary>
ServiceResourceIsBeingRateLimited = 40062,

/// <summary>
/// There are no tags available that can be set by non-moderators.
/// </summary>
Expand Down
6 changes: 4 additions & 2 deletions Backend/Remora.Discord.API/API/Objects/Errors/RestError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ namespace Remora.Discord.API.Objects;
[PublicAPI]
public record RestError
(
DiscordError Code,
Optional<DiscordError> Code,
Optional<IReadOnlyDictionary<string, OneOf<IPropertyErrorDetails, IReadOnlyList<IErrorDetails>>>> Errors,
string Message
string Message,
Optional<bool> IsGlobal,
Optional<float> RetryAfter
) : IRestError;
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,8 @@ private static JsonSerializerOptions AddWebhookObjectConverters(this JsonSeriali
/// <returns>The options, with the converters added.</returns>
private static JsonSerializerOptions AddErrorObjectConverters(this JsonSerializerOptions options)
{
options.AddDataObjectConverter<IRestError, RestError>();
options.AddDataObjectConverter<IRestError, RestError>()
.WithPropertyName(e => e.IsGlobal, "global");
options.AddDataObjectConverter<IErrorDetails, ErrorDetails>();

return options;
Expand Down
4 changes: 3 additions & 1 deletion Backend/Remora.Discord.Rest/Polly/DiscordRateLimitPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,12 @@ bool continueOnCapturedContext
bucketIdentifier = endpoint;
}

var getValue = await cache.RetrieveAsync<RateLimitBucket>(
var getValue = await cache.RetrieveAsync<RateLimitBucket>
(
CacheKey.LocalizedStringKey(nameof(Polly), bucketIdentifier),
cancellationToken
);

if (getValue.IsDefined(out var rateLimitBucket))
{
// We don't reset route-specific rate limits ourselves; that's the responsibility of the returned headers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@
]
}
},
"message": "Invalid Form Body"
"message": "Invalid Form Body",
"global": true,
"retry_after": 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"message": "Invalid Form Body"
}

0 comments on commit a94096d

Please sign in to comment.