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
- object
- DynamicHttpErrorHandler
- Implements
Remarks
Constructors
DynamicHttpErrorHandler(Func<HttpRequestErrorContext, CancellationToken, Task<HttpErrorHandlerResult>>)
Initializes a new instance of the DynamicHttpErrorHandler class.
public DynamicHttpErrorHandler(Func<HttpRequestErrorContext, CancellationToken, Task<HttpErrorHandlerResult>> asyncHandler)Parameters
asyncHandlerFunc<HttpRequestErrorContext, CancellationToken, Task<HttpErrorHandlerResult>>- The asynchronous delegate to handle HTTP error status codes and decide on retry logic.
Exceptions
- ArgumentNullException
- Thrown if
asyncHandlerisnull.
Remarks
The delegate receives the following parameters:
- context–Provides 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.
- cancellationToken–A 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
statusCodeHttpStatusCode- The HTTP status code to evaluate.
Returns
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
ctxHttpResponseErrorContext- The context containing information about the HTTP response that indicates a failure.
cancellationTokenCancellationToken- 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
ctxHttpResponseErrorContext- The context containing information about the HTTP response that indicates a failure.
cancellationTokenCancellationToken- A token for canceling the operation.
Returns
- Task<HttpErrorHandlerResult>
- A task that resolves to an HttpErrorHandlerResult.
Exceptions
- ArgumentNullException
- Thrown if
ctxisnull.

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
truefor CanHandle(HttpStatusCode), it represents a "catch-all" handler that can be used as a fall-back when no other specific error handlers are suitable.