RateLimitRejectedException
Exception thrown when a delegate executed through a IRateLimitPolicy is rate-limited.
using System;
using System.Runtime.CompilerServices;
namespace Polly.RateLimit
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public class RateLimitRejectedException
{
public TimeSpan RetryAfter { get; set; }
public RateLimitRejectedException(TimeSpan retryAfter)
: this(retryAfter, DefaultMessage(retryAfter))
{
}
public RateLimitRejectedException(TimeSpan retryAfter, Exception innerException)
: this(DefaultMessage(retryAfter), innerException)
{
SetRetryAfter(retryAfter);
}
public RateLimitRejectedException(TimeSpan retryAfter, string message)
: this(message)
{
SetRetryAfter(retryAfter);
}
public RateLimitRejectedException(TimeSpan retryAfter, string message, Exception innerException)
: this(message, innerException)
{
SetRetryAfter(retryAfter);
}
private static string DefaultMessage(TimeSpan retryAfter)
{
DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(64, 1);
defaultInterpolatedStringHandler.AppendLiteral("The operation has been rate-limited and should be retried after ");
defaultInterpolatedStringHandler.AppendFormatted(retryAfter);
return defaultInterpolatedStringHandler.ToStringAndClear();
}
private void SetRetryAfter(TimeSpan retryAfter)
{
if (retryAfter < TimeSpan.Zero)
throw new ArgumentOutOfRangeException("retryAfter", retryAfter, "The retryAfter parameter must be a TimeSpan greater than or equal to TimeSpan.Zero.");
RetryAfter = retryAfter;
}
}
}