Skip to content

Commit

Permalink
Extends RetryAfterPlugin with a clearer error message
Browse files Browse the repository at this point in the history
  • Loading branch information
waldekmastykarz committed Nov 21, 2023
1 parent f4d0b2b commit b9cb052
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions dev-proxy-plugins/Behavior/RetryAfterPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,17 @@ private void ThrottleIfNecessary(ProxyRequestArgs e)
var throttleInfo = throttler.ShouldThrottle(request, throttler.ThrottlingKey);
if (throttleInfo.ThrottleForSeconds > 0)
{
_logger?.LogRequest(new[] { $"Calling {request.Url} before waiting for the Retry-After period.", "Request will be throttled", $"Throttling on {throttler.ThrottlingKey}" }, MessageType.Failed, new LoggingContext(e.Session));
var messageLines = new[] { $"Calling {request.Url} before waiting for the Retry-After period.", "Request will be throttled.", $"Throttling on {throttler.ThrottlingKey}." };
_logger?.LogRequest(messageLines, MessageType.Failed, new LoggingContext(e.Session));

throttler.ResetTime = DateTime.Now.AddSeconds(throttleInfo.ThrottleForSeconds);
UpdateProxyResponse(e, throttleInfo);
UpdateProxyResponse(e, throttleInfo, string.Join(' ', messageLines));
break;
}
}
}

private void UpdateProxyResponse(ProxyRequestArgs e, ThrottlingInfo throttlingInfo)
private void UpdateProxyResponse(ProxyRequestArgs e, ThrottlingInfo throttlingInfo, string message)
{
var headers = new List<HttpHeader>();
var body = string.Empty;
Expand All @@ -82,7 +83,7 @@ private void UpdateProxyResponse(ProxyRequestArgs e, ThrottlingInfo throttlingIn
new GraphErrorResponseError
{
Code = new Regex("([A-Z])").Replace(HttpStatusCode.TooManyRequests.ToString(), m => { return $" {m.Groups[1]}"; }).Trim(),
Message = BuildApiErrorMessage(request),
Message = BuildApiErrorMessage(request, message),
InnerError = new GraphErrorResponseInnerError
{
RequestId = requestId,
Expand All @@ -98,5 +99,5 @@ private void UpdateProxyResponse(ProxyRequestArgs e, ThrottlingInfo throttlingIn
e.ResponseState.HasBeenSet = true;
}

private static string BuildApiErrorMessage(Request r) => $"Some error was generated by the proxy. {(ProxyUtils.IsGraphRequest(r) ? ProxyUtils.IsSdkRequest(r) ? "" : String.Join(' ', MessageUtils.BuildUseSdkForErrorsMessage(r)) : "")}";
private static string BuildApiErrorMessage(Request r, string message) => $"{message} {(ProxyUtils.IsGraphRequest(r) ? ProxyUtils.IsSdkRequest(r) ? "" : string.Join(' ', MessageUtils.BuildUseSdkForErrorsMessage(r)) : "")}";
}

0 comments on commit b9cb052

Please sign in to comment.