Skip to content

Commit 5ba67d3

Browse files
author
Guy Fankam
committed
Update HTTP retry scenarios
1 parent 2c20b05 commit 5ba67d3

1 file changed

Lines changed: 28 additions & 28 deletions

File tree

src/common/Http.cs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -227,45 +227,45 @@ protected override async ValueTask<bool> ShouldRetryAsync(HttpMessage message, E
227227

228228
private static bool ShouldRetryInner(HttpMessage message, Exception? exception)
229229
{
230-
try
231-
{
232-
return
233-
(message, exception) switch
234-
{
235-
({ Response.Status: 422 or 409 }, _) when HasManagementApiRequestFailedError(message.Response) => true,
236-
({ Response.Status: 412 }, _) => true,
237-
({ Response.Status: 429 }, _) => true,
238-
_ => false
239-
};
240-
}
241-
catch (InvalidOperationException)
230+
var response = message.Response;
231+
var errorCode = GetErrorCode(response).IfNone(() => string.Empty);
232+
var errorMessage = GetErrorMessage(response).IfNone(() => string.Empty);
233+
234+
return response.Status switch
242235
{
243-
return false;
244-
}
236+
429 => true,
237+
412 => true,
238+
422 or 409 when errorCode.Equals("ManagementApiRequestFailed", StringComparison.OrdinalIgnoreCase) => true,
239+
409 when errorMessage.Contains("Operation on the API is in progress", StringComparison.OrdinalIgnoreCase)
240+
&& (errorCode.Equals("Conflict", StringComparison.OrdinalIgnoreCase)
241+
|| errorCode.Equals("PessimisticConcurrencyConflict", StringComparison.OrdinalIgnoreCase)) => true,
242+
_ => false
243+
};
245244
}
246245

247-
private static bool HasManagementApiRequestFailedError(Response response) =>
248-
TryGetErrorCode(response)
249-
.Where(code => code.Equals("ManagementApiRequestFailed", StringComparison.OrdinalIgnoreCase))
250-
.IsSome;
246+
private static Option<string> GetErrorCode(Response response) =>
247+
GetError(response)
248+
.Bind(error => error.GetStringProperty("code"))
249+
.ToOption();
251250

252-
private static Option<string> TryGetErrorCode(Response response)
251+
private static Result<JsonObject> GetError(Response response)
253252
{
254253
try
255254
{
256-
var result = from node in JsonNodeModule.From(response.Content)
257-
from jsonObject in node.AsJsonObject()
258-
from errorJsonObject in jsonObject.GetJsonObjectProperty("error")
259-
from errorCode in errorJsonObject.GetStringProperty("code")
260-
select errorCode;
261-
262-
return result.ToOption();
255+
return from content in JsonObjectModule.From(response.Content)
256+
from error in content.GetJsonObjectProperty("error")
257+
select error;
263258
}
264-
catch (Exception exception) when (exception is ArgumentNullException or NotSupportedException or JsonException)
259+
catch (Exception exception) when (exception is ArgumentNullException or NotSupportedException or JsonException or InvalidOperationException)
265260
{
266-
return Option.None;
261+
return Error.From(exception);
267262
}
268263
}
264+
265+
private static Option<string> GetErrorMessage(Response response) =>
266+
GetError(response)
267+
.Bind(error => error.GetStringProperty("message"))
268+
.ToOption();
269269
}
270270

271271
public sealed class LoggingPolicy(ILogger logger) : HttpPipelinePolicy

0 commit comments

Comments
 (0)