Skip to content

Commit

Permalink
fix: Handle transient error for HTTP request
Browse files Browse the repository at this point in the history
Closes #276
  • Loading branch information
kamil-mrzyglod committed Sep 13, 2024
1 parent 20258b7 commit 5c27650
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions ace/WhatIf/AzureWhatIfHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public AzureWhatIfHandler(string scopeId,
return data;
}

private async Task<HttpResponseMessage> SendInitialRequest(CancellationToken cancellationToken)
private async Task<HttpResponseMessage> SendInitialRequest(CancellationToken cancellationToken, int retryCount = 0)
{
var token = GetToken(cancellationToken);
var request = new HttpRequestMessage(HttpMethod.Post, CreateUrlBasedOnScope());
Expand All @@ -152,8 +152,23 @@ private async Task<HttpResponseMessage> SendInitialRequest(CancellationToken can
request.Content = new StringContent(templateContent, Encoding.UTF8, "application/json");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);

var response = await httpClient.Value.SendAsync(request, cancellationToken);
return response;
try
{
var response = await httpClient.Value.SendAsync(request, cancellationToken);
return response;
}
catch(IOException ex)
{
if(retryCount < 3)
{
this.logger.LogError(ex, "Error while sending request to What If API. Retrying...");
await Task.Delay(5000, cancellationToken);
return await SendInitialRequest(cancellationToken, retryCount + 1);
}

this.logger.LogError(ex, "Error while sending request to What If API.");
throw;
}
}

private string CreateUrlBasedOnScope()
Expand Down

0 comments on commit 5c27650

Please sign in to comment.