...
Note |
---|
It is important to not use the static |
Error Handling
A new error-handling call back has been added to allow cleaner custom error logic (previously, error handling could be incorporated directly into the ProcessHttpRequestAsync
method). This error callback is called for any exception thrown in ProcessHttpRequestAsync
, including when non success (2XX) codes are returned from the server (this requires the Ensure Success
checkbox to be checked).
By default, any exception thrown in ProcessHttpRequestAsync
will be logged and the message retried indefinitely. If you do not wish to retry indefinitely, you can add custom logic as follows:
Code Block | ||
---|---|---|
| ||
public class CustomHttpSender : BaseHttpSender
{
public override async Task ProcessHttpRequestAsync(RequestContext request)
{
await SendAsync(request).ConfigureAwait(false);
}
public override void OnError(IMessageContext context, HttpErrorEventArgs args)
{
// Capture the scenario where a 500 is return and we've retried the message at least 3 times
if (args.RequestContext.HttpResponse?.StatusCode == HttpStatusCode.InternalServerError && args.TotalRetries > 3)
{
// response code 500
args.ShouldRetry = false;
context.WriteEvent(EventSeverity.Error, "Failed to send message 3 times. Erroring message.");
return;
}
// some other exception, message will be retried indefinitely
context.WriteEvent(EventSeverity.Error, $"Failed to send message (retry {args.TotalRetries}): {args.Exception.Message}");
}
} |
In the above example, we are inspecting the HttpResponse
object (which is contained in the HttpErrorEventArgs.RequestContext
property) for the status code. If it’s InternalServerError
(500) and the TotalRetries
is at least 3, then we set ShouldRetry
to false
. This causes the message to be moved to the error queue.