DynamicHttpErrorHandler Class

Namespace
Kampute.HttpClient.ErrorHandlers
Assembly
  • Kampute.HttpClient.dll

Definition

Provides a dynamic mechanism to handle HTTP error status codes and determine retry logic for HTTP requests.
public class DynamicHttpErrorHandler : IHttpErrorHandler
Inheritance
Implements

Remarks

This class implements the IHttpErrorHandler interface, allowing for custom and dynamic error handling strategies to be defined at runtime. It encapsulates a delegate that is invoked to determine the retry logic for failed HTTP requests, making it highly flexible and adaptable to various error handling scenarios.

Since this class always returns true for CanHandle(HttpStatusCode), it represents a "catch-all" handler that can be used as a fall-back when no other specific error handlers are suitable.

Constructors

DynamicHttpErrorHandler(Func<HttpRequestErrorContext, CancellationToken, Task<HttpErrorHandlerResult>>)

Initializes a new instance of the DynamicHttpErrorHandler class.
public DynamicHttpErrorHandler(Func<HttpRequestErrorContext, CancellationToken, Task<HttpErrorHandlerResult>> asyncHandler)

Parameters

asyncHandler Func<HttpRequestErrorContext, CancellationToken, Task<HttpErrorHandlerResult>>
The asynchronous delegate to handle HTTP error status codes and decide on retry logic.

Exceptions

ArgumentNullException
Thrown if asyncHandler is null.

Remarks

The delegate receives the following parameters:
  • contextProvides context about the HTTP request resulting in a failure response. It is encapsulated within an HttpResponseErrorContext instance, allowing for an informed decision on the retry strategy.
  • cancellationTokenA CancellationToken for canceling the operation.

Methods

CanHandle(HttpStatusCode)

Determines whether the handler is capable of handling the provided HTTP status code.
public bool CanHandle(HttpStatusCode statusCode)

Parameters

statusCode HttpStatusCode
The HTTP status code to evaluate.

Returns

bool
Always true, indicating that this handler can handle any status code.

DecideOnRetryAsync(HttpResponseErrorContext, CancellationToken)

Invokes the configured asynchronous delegate to determine whether a failed request should be retried.
protected virtual Task<HttpErrorHandlerResult> DecideOnRetryAsync(HttpResponseErrorContext ctx, CancellationToken cancellationToken)

Parameters

ctx HttpResponseErrorContext
The context containing information about the HTTP response that indicates a failure.
cancellationToken CancellationToken
A token for canceling the operation.

Returns

Task<HttpErrorHandlerResult>
A task that resolves to an HttpErrorHandlerResult, indicating whether the request should be retried.

Explicit Interface Implementations

IHttpErrorHandler.DecideOnRetryAsync(HttpResponseErrorContext, CancellationToken)

Evaluates whether a failed request should be retried based on the error context.
Task<HttpErrorHandlerResult> IHttpErrorHandler.DecideOnRetryAsync(HttpResponseErrorContext ctx, CancellationToken cancellationToken)

Parameters

ctx HttpResponseErrorContext
The context containing information about the HTTP response that indicates a failure.
cancellationToken CancellationToken
A token for canceling the operation.

Returns

Task<HttpErrorHandlerResult>
A task that resolves to an HttpErrorHandlerResult.

Exceptions

ArgumentNullException
Thrown if ctx is null.