Polly icon indicating copy to clipboard operation
Polly copied to clipboard

After final failure wrap and throw a new exception

Open bunceg opened this issue 11 years ago • 8 comments

Hi,

I'm doing something like this:

return await Policy .Handle(ex => _strategy.IsTransient(ex)) .WaitAndRetryAsync(new[] { TimeSpan.FromMilliseconds(250), TimeSpan.FromMilliseconds(500), TimeSpan.FromMilliseconds(750) }, (ex, retryCount) => { retries = retries + 1; //TODO: Log }) .ExecuteAsync(() => _decoratedAsync.ExecuteAsync(query)).ConfigureAwait(false);

This is a generic handler where I inject the strategy and the code to be run. However in some cases I need the handler to catch a final (e.g.) "Service Slow" exception and then raise a Service Unavailable instead. As this is a generic handler, it's only applicable in a few cases so I don't really want to write a custom handler with a try - catch for this.

Can this be done in Polly? (maybe use the retry count?)

bunceg avatar Apr 01 '15 16:04 bunceg

I think we should come at this from a different direction, we should add to the policy builder a new method called AfterFinalRetry(Action action) and you an do what ever you'd like with the exception (wrap it, rethrow it, ignore it, w\e). Of course the default behavior should remain the same (throw the exception after final retry), but if this method has been invoked on the policy it should override the default behavior.

I will try to do it this weekend...

yftachman avatar Apr 30 '15 16:04 yftachman

sounds good to me, thanks

bunceg avatar Apr 30 '15 16:04 bunceg

Hey, made this fork https://github.com/yftachman/Polly

should contain the changes you need. if there are any problems with it let me know. i'm not that familiar with this project so problems may arise

Example syntax: Policy.Handle() .AfterFinalRetryFailure(ex => AfterFinalRetryLogic(ex)) .Retry(2) ..Execute(() => Logic());

gl hf :)

yftachman avatar Apr 30 '15 21:04 yftachman

I'd like a little more consistency with the .NET framework.

Instead of having a AfterFinalRetryFailure, perhaps something more generic where you tell it when you want a delegate to run.

We have this in TPL. Task.Factory.StartNew(() => {}) .ContinueWith(task => { /* Do stuff*/ }, TaskContinuationOptions.OnlyOnFaulted)

I guess this means it would come back to a solution which uses a Policy

badmotorfinger avatar Jun 17 '15 03:06 badmotorfinger

This would be super useful...

tapmantwo avatar Jul 07 '15 15:07 tapmantwo

@michael-wolfenden is this something you would consider merging in to the main branch?

badmotorfinger avatar Jul 13 '15 00:07 badmotorfinger

I have just released the latest version (2.2.3) with includes post execution support. This should allow you do what you are suggesting.

michael-wolfenden avatar Aug 09 '15 06:08 michael-wolfenden

Thanks for this

bunceg avatar Aug 09 '15 08:08 bunceg