Skip to content

Commit

Permalink
Revisit list of transient errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dluc committed Nov 18, 2024
1 parent ee6215b commit 39432f2
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 5 deletions.
14 changes: 9 additions & 5 deletions service/Abstractions/Diagnostics/HttpErrors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ public static class HttpErrors
// Errors that might disappear by retrying
private static readonly HashSet<int> s_transientErrors =
[
(int)HttpStatusCode.InternalServerError,
(int)HttpStatusCode.BadGateway,
(int)HttpStatusCode.ServiceUnavailable,
(int)HttpStatusCode.GatewayTimeout,
(int)HttpStatusCode.InsufficientStorage
(int)HttpStatusCode.RequestTimeout, // 408
(int)HttpStatusCode.PreconditionFailed, // 412
(int)HttpStatusCode.Locked, // 423
(int)HttpStatusCode.TooManyRequests, // 429
(int)HttpStatusCode.InternalServerError, // 500
(int)HttpStatusCode.BadGateway, // 502
(int)HttpStatusCode.ServiceUnavailable, // 503
(int)HttpStatusCode.GatewayTimeout, // 504
(int)HttpStatusCode.InsufficientStorage // 507
];

public static bool IsTransientError(this HttpStatusCode statusCode)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright (c) Microsoft. All rights reserved.

using System.Net;
using Microsoft.KernelMemory.Diagnostics;

namespace Microsoft.KM.Abstractions.UnitTests.Diagnostics;

public sealed class HttpErrorsTests
{
[Fact]
public void ItRecognizesErrorsFromNulls()
{
HttpStatusCode? statusCode = null;

Assert.False(statusCode.IsTransientError());
Assert.False(statusCode.IsFatalError());
}

[Theory]
[InlineData(HttpStatusCode.Continue)] // 100
[InlineData(HttpStatusCode.SwitchingProtocols)] // 101
[InlineData(HttpStatusCode.Processing)] // 102
[InlineData(HttpStatusCode.EarlyHints)] // 103
[InlineData(HttpStatusCode.OK)] // 200
[InlineData(HttpStatusCode.Created)] // 201
[InlineData(HttpStatusCode.Accepted)] // 202
[InlineData(HttpStatusCode.NonAuthoritativeInformation)] // 203
[InlineData(HttpStatusCode.NoContent)] // 204
[InlineData(HttpStatusCode.ResetContent)] // 205
[InlineData(HttpStatusCode.Ambiguous)] // 300
[InlineData(HttpStatusCode.Moved)] // 301
[InlineData(HttpStatusCode.Found)] // 302
[InlineData(HttpStatusCode.RedirectMethod)] // 303
[InlineData(HttpStatusCode.NotModified)] // 304
[InlineData(HttpStatusCode.UseProxy)] // 305
[InlineData(HttpStatusCode.Unused)] // 306
[InlineData(HttpStatusCode.RedirectKeepVerb)] // 307
[InlineData(HttpStatusCode.PermanentRedirect)] // 308
public void ItRecognizesErrors(HttpStatusCode statusCode)
{
Assert.False(statusCode.IsTransientError());
Assert.False(statusCode.IsFatalError());
}

[Theory]
[InlineData(HttpStatusCode.RequestTimeout)] // 408
[InlineData(HttpStatusCode.PreconditionFailed)] // 412
[InlineData(HttpStatusCode.Locked)] // 423
[InlineData(HttpStatusCode.TooManyRequests)] // 429
[InlineData(HttpStatusCode.InternalServerError)] // 500
[InlineData(HttpStatusCode.BadGateway)] // 502
[InlineData(HttpStatusCode.ServiceUnavailable)] // 503
[InlineData(HttpStatusCode.GatewayTimeout)] // 504
[InlineData(HttpStatusCode.InsufficientStorage)] // 507
public void ItRecognizesTransientErrors(HttpStatusCode statusCode)
{
Assert.True(statusCode.IsTransientError());
Assert.False(statusCode.IsFatalError());
}

[Theory]
[InlineData(HttpStatusCode.BadRequest)] // 400
[InlineData(HttpStatusCode.Unauthorized)] // 401
[InlineData(HttpStatusCode.PaymentRequired)] // 402
[InlineData(HttpStatusCode.Forbidden)] // 403
[InlineData(HttpStatusCode.NotFound)] // 404
[InlineData(HttpStatusCode.MethodNotAllowed)] // 405
[InlineData(HttpStatusCode.NotAcceptable)] // 406
[InlineData(HttpStatusCode.ProxyAuthenticationRequired)] // 407
[InlineData(HttpStatusCode.Conflict)] // 409
[InlineData(HttpStatusCode.Gone)] // 410
[InlineData(HttpStatusCode.LengthRequired)] // 411
[InlineData(HttpStatusCode.RequestEntityTooLarge)] // 413
[InlineData(HttpStatusCode.RequestUriTooLong)] // 414
[InlineData(HttpStatusCode.UnsupportedMediaType)] // 415
[InlineData(HttpStatusCode.RequestedRangeNotSatisfiable)] // 416
[InlineData(HttpStatusCode.ExpectationFailed)] // 417
[InlineData(HttpStatusCode.UnprocessableContent)] // 422
[InlineData(HttpStatusCode.UpgradeRequired)] // 426
[InlineData(HttpStatusCode.RequestHeaderFieldsTooLarge)] // 431
[InlineData(HttpStatusCode.UnavailableForLegalReasons)] // 451
[InlineData(HttpStatusCode.NotImplemented)] // 501
[InlineData(HttpStatusCode.HttpVersionNotSupported)] // 505
[InlineData(HttpStatusCode.LoopDetected)] // 508
[InlineData(HttpStatusCode.NotExtended)] // 510
[InlineData(HttpStatusCode.NetworkAuthenticationRequired)] // 511
public void ItRecognizesFatalErrors(HttpStatusCode statusCode)
{
Assert.False(statusCode.IsTransientError());
Assert.True(statusCode.IsFatalError());
}
}

0 comments on commit 39432f2

Please sign in to comment.