Search code examples
c#.net-coredotnet-httpclientpollyretry-logic

Catching Internal Sealed Exceptions


Using the ConfigurePrimaryHttpMessageHandler / AddPolicyHandler retry pattern how can you perform a retry for an exception that you can't reference because of internal sealed?

Ideally I'd like to do this:

.AddPolicyHandler(HttpPolicyExtensions
            .HandleTransientHttpError() //408, 5xx
            .Or<SocksException>()
            .WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(3, retryAttempt))));

But unfortunately SocksException looks like this:

internal sealed class SocksException : IOException
{
    public SocksException(string message) : base(message) { }

    public SocksException(string message, Exception innerException) : base(message, innerException) { }
}

And thus cannot be referenced directly. Will catching IOException suffice?

Edit: I've tried by trying to catch IOException, which you would think would work...but it doesn't. Not sure what else I could try.


Solution

  • So, it turns out that with Polly this is working but confusingly on the last retry if it still fails the exception is rethrown which lead me to believe that it wasn't catching it...

    If you console log your retry you can see that it is.

    Hopefully this helps someone!