2

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.

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Steji
  • 580
  • 1
  • 6
  • 17
  • 2
    As the 'SocksException' is derived from 'IOException', you might try by catching 'IOException' like this -> .Or() – Md. Faisal Habib Jun 06 '23 at 20:59
  • Thank you, that's the best idea I could come up with - wasn't sure if there was something more I could do – Steji Jun 07 '23 at 19:35
  • 1
    When you say "you would think would work...but it doesn't", what do you mean? – Ian Newson Jun 29 '23 at 10:27
  • well, it didn't appear to be catching the error and retrying - I've since realised it actually did, but on the final retry if it fails it will rethrow the caught exception – Steji Jul 17 '23 at 21:26

2 Answers2

2

A bit fragile solution but with reflection you can do the following:

.Or<Exception>(ex => ex.GetType().Name == "SocksException")
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
0

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!

Steji
  • 580
  • 1
  • 6
  • 17