Skip to content
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

Extends RetryAfterPlugin with a clearer error message #391

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 @@ -25,7 +25,7 @@
pluginEvents.BeforeRequest += OnRequest;
}

private async Task OnRequest(object? sender, ProxyRequestArgs e)

Check warning on line 28 in dev-proxy-plugins/Behavior/RetryAfterPlugin.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 28 in dev-proxy-plugins/Behavior/RetryAfterPlugin.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
if (e.ResponseState.HasBeenSet ||
_urlsToWatch is null ||
Expand Down Expand Up @@ -56,16 +56,17 @@
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 @@
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 @@
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)) : "")}";
}
Loading