Polly icon indicating copy to clipboard operation
Polly copied to clipboard

Handle different exceptions with custom behavior [ forking logging by exception type on retry ]

Open justConfused opened this issue 7 years ago • 5 comments

After reading https://github.com/App-vNext/Polly/issues/104 Is any functionality planned to have one policy handle multiple exceptions with each having custom behavior.

Policy
    .Handle<FooException>(() -> log.Error("Foo"))
    .Handle<BarException>(() -> log.Error("Bar"))
    .Execute(() -> DoSomething())

Thanks

justConfused avatar Feb 27 '19 02:02 justConfused

Hi @confusedIamHowBoutU , thanks for the question. PolicyWrap already provides equivalent functionality, and there are no plans to have one policy handle multiple exceptions differently in any way other than PolicyWrap.

There is also no intention to develop a long-running chaining syntax to result in equivalent PolicyWrap outputs (though somebody could develop it as a Polly.Contrib if they wanted to). When PolicyWrap was designed, we moved away from the long-running chaining syntax, as it was found to be limiting (/to lead to significantly more complexity) for defining policy instances which could be recombined in different ways in different PolicyWraps.

To elaborate on that: Stateless policy instances can be re-used without consequence. For stateful policies circuit-breaker and bulkhead, on the other hand, it is functionally significant whether you re-use the same instance or use different instances, across call sites. So a common requirement might be to have retry -> circuit-breaker -> timeout, using a common retry policy across all endpoints, variant circuit-breaker instances with a breaker per downstream system called, and a common timeout policy. A long-running chaining syntax doesn't lend itself to achieving that degree of control. Concretely:

Policy
    .Handle<FooException>()
    .Retry(3)
    .Handle<BarException()
    .CircuitBreaker(/* etc */) // with this kind of beyond-one-policy chaining syntax, we concluded it was not elegant to extract the circuit-breaker policy instance from here as an instance that can be re-used elsewhere. 
    .Timeout(30);

reisenberger avatar Feb 27 '19 08:02 reisenberger

One more thought:

Policy
    .Handle<FooException>(() -> log.Error("Foo"))
    .Handle<BarException>(() -> log.Error("Bar"))
    .Execute(() -> DoSomething())

possibly suggests combining policies via an exclusive-or switch-case-ry: "if a then DoA else if b then DoB" (etc) (as opposed to the functional-composition, nested-function nature of PolicyWrap).

For the logging example given this looks logical and simple, and it could also work for Fallback. But my view is that it could only make sense (remain simple) to combine multiple predicates-and-consequences within a single policy instance, for these simpler kinds of action. For anything beyond (retry or circuit-breaker), reasoning about the meaning and usage (especially in combination with the pre-existing PolicyWrap) becomes complicated. Because of this limited applicability, I doubt we will invest time taking Polly in that direction.

For specific cases, building one's own extension methods to achieve a particular syntax is always an option.

If @confusedIamHowBoutU there is anything else we can help with, let us know. If logging is a particular interest, see also Polly.Contrib.LoggingPolicy.

Hope this helps!

reisenberger avatar Feb 27 '19 20:02 reisenberger

@reisenberger thanks for insight that super help full.

What is guidance around following pattern?

Policy
    .Handle<FooException>()
    .Handle<BarException>()
    .onRertry(3, (exp, i) -> {
         if (exp is FooException){
               log.error("foo")
         }
         else if (exp is BarException){
               log.error("Bar")
         }
    })
    .Execute(() -> DoSomething())

justConfused avatar Feb 28 '19 00:02 justConfused

Thanks @confusedIamHowBoutU . From:

Policy
    .Handle<FooException>()
    .Handle<BarException>()
    .onRertry(3, (exp, i) -> {
         if (exp is FooException){
               log.error("foo")
         }
         else if (exp is BarException){
               log.error("Bar")
         }
    })
    .Execute(() -> DoSomething())

it is clearer that the question is about variant logging on retry (not about different flavours of policy behaviour in a single policy, in the direction of #140 or PolicyWrap).


Step 1 of the Polly Readme demonstrates .Or<BarException>(), see the example labelled // Multiple exception types

The retry section of the readme shows syntax for correctly configuring onRetry:; see the third example.

Step 3 of the readme shows syntax examples putting it all together; the second example there executes through a policy which has just been configured to log in the onRetry.

reisenberger avatar Feb 28 '19 06:02 reisenberger

And here's a quick working example: https://dotnetfiddle.net/Sipste

Polly-Samples also contains many more developed examples.

reisenberger avatar Feb 28 '19 07:02 reisenberger