Untitled

 avatar
unknown
csharp
5 months ago
1.2 kB
8
Indexable
public class ExceptionHandlingFilter<T> : IFilter<ConsumeContext<T>> where T : class
{
    public async Task Send(ConsumeContext<T> context, IPipe<ConsumeContext<T>> next)
    {
        try
        {
            // Proceed with normal message processing
            await next.Send(context);
        }
        catch (Exception ex)
        {
            if (IsUnrecoverable(ex))
            {
                Console.WriteLine($"Unrecoverable exception: {ex.Message}. Skipping retries.");
                // Notify that this message has failed without retrying further
                await context.NotifyFaulted(ex);
            }
            else
            {
                Console.WriteLine($"Recoverable exception: {ex.Message}. Applying retry policy.");
                // Re-throw for the retry policy to handle
                throw;
            }
        }
    }

    private bool IsUnrecoverable(Exception exception)
    {
        // Define logic for unrecoverable exceptions
        return exception is UnrecoverableException;
    }

    public void Probe(ProbeContext context)
    {
        context.CreateScope("ExceptionHandlingFilter");
    }
}
Editor is loading...
Leave a Comment