Skip to content

Extends RetryAfterPlugin with a clearer error message #391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) : "")}";
}