Fix: errors when using rate limiting (#811)

* Fix: errors when using rate limiting
Add: QuotaExceededError class for requesting too much
Add: QuotaExceededError error code
Add: Add an error when limit is reached
Reflact: Extract GetResponseMessage method for getting default or configured response message for requ

* Fix: modify check_we_have_considered_all_errors_in_these_tests for adding a new OcelotErrorCode
This commit is contained in:
Weidaicheng 2019-03-13 07:07:39 +08:00 committed by Thiago Loureiro
parent 799abf55c4
commit e76a51ffc9
4 changed files with 27 additions and 3 deletions

View File

@ -37,6 +37,7 @@
CouldNotFindPlaceholderError, CouldNotFindPlaceholderError,
CouldNotFindAggregatorError, CouldNotFindAggregatorError,
CannotAddPlaceholderError, CannotAddPlaceholderError,
CannotRemovePlaceholderError CannotRemovePlaceholderError,
QuotaExceededError
} }
} }

View File

@ -70,6 +70,9 @@ namespace Ocelot.RateLimit.Middleware
// break execution // break execution
await ReturnQuotaExceededResponse(context.HttpContext, options, retrystring); await ReturnQuotaExceededResponse(context.HttpContext, options, retrystring);
// Set Error
context.Errors.Add(new QuotaExceededError(this.GetResponseMessage(options)));
return; return;
} }
} }
@ -117,7 +120,7 @@ namespace Ocelot.RateLimit.Middleware
public virtual Task ReturnQuotaExceededResponse(HttpContext httpContext, RateLimitOptions option, string retryAfter) public virtual Task ReturnQuotaExceededResponse(HttpContext httpContext, RateLimitOptions option, string retryAfter)
{ {
var message = string.IsNullOrEmpty(option.QuotaExceededMessage) ? $"API calls quota exceeded! maximum admitted {option.RateLimitRule.Limit} per {option.RateLimitRule.Period}." : option.QuotaExceededMessage; var message = this.GetResponseMessage(option);
if (!option.DisableRateLimitHeaders) if (!option.DisableRateLimitHeaders)
{ {
@ -128,6 +131,14 @@ namespace Ocelot.RateLimit.Middleware
return httpContext.Response.WriteAsync(message); return httpContext.Response.WriteAsync(message);
} }
private string GetResponseMessage(RateLimitOptions option)
{
var message = string.IsNullOrEmpty(option.QuotaExceededMessage)
? $"API calls quota exceeded! maximum admitted {option.RateLimitRule.Limit} per {option.RateLimitRule.Period}."
: option.QuotaExceededMessage;
return message;
}
private Task SetRateLimitHeaders(object rateLimitHeaders) private Task SetRateLimitHeaders(object rateLimitHeaders)
{ {
var headers = (RateLimitHeaders)rateLimitHeaders; var headers = (RateLimitHeaders)rateLimitHeaders;

View File

@ -0,0 +1,12 @@
using Ocelot.Errors;
namespace Ocelot.RateLimit
{
public class QuotaExceededError : Error
{
public QuotaExceededError(string message)
: base(message, OcelotErrorCode.QuotaExceededError)
{
}
}
}

View File

@ -125,7 +125,7 @@ namespace Ocelot.UnitTests.Responder
// If this test fails then it's because the number of error codes has changed. // If this test fails then it's because the number of error codes has changed.
// You should make the appropriate changes to the test cases here to ensure // You should make the appropriate changes to the test cases here to ensure
// they cover all the error codes, and then modify this assertion. // they cover all the error codes, and then modify this assertion.
Enum.GetNames(typeof(OcelotErrorCode)).Length.ShouldBe(36, "Looks like the number of error codes has changed. Do you need to modify ErrorsToHttpStatusCodeMapper?"); Enum.GetNames(typeof(OcelotErrorCode)).Length.ShouldBe(37, "Looks like the number of error codes has changed. Do you need to modify ErrorsToHttpStatusCodeMapper?");
} }
private void ShouldMapErrorToStatusCode(OcelotErrorCode errorCode, HttpStatusCode expectedHttpStatusCode) private void ShouldMapErrorToStatusCode(OcelotErrorCode errorCode, HttpStatusCode expectedHttpStatusCode)