Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recursively search all AggregateException InnerExceptions for predicate matches when using HandleInner() #822

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions src/Polly.Specs/Retry/RetrySpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,94 @@ public void Should_call_onretry_with_a_handled_innerexception()
passedToOnRetry.Should().BeSameAs(toRaiseAsInner);
}

[Fact]
public void Should_call_onretry_with_handled_exception_nested_in_aggregate_as_first_exception()
{
Exception passedToOnRetry = null;

var policy = Policy
.HandleInner<DivideByZeroException>()
.Retry(3, (exception, _) => passedToOnRetry = exception);

Exception toRaiseAsInner = new DivideByZeroException();

Exception aggregateException = new AggregateException(
new Exception("First: With Inner Exception",
toRaiseAsInner),
new Exception("Second: Without Inner Exception"));

policy.RaiseException(aggregateException);

passedToOnRetry.Should().BeSameAs(toRaiseAsInner);
}

[Fact]
public void Should_call_onretry_with_handled_exception_nested_in_aggregate_as_second_exception()
{
Exception passedToOnRetry = null;

var policy = Policy
.HandleInner<DivideByZeroException>()
.Retry(3, (exception, _) => passedToOnRetry = exception);

Exception toRaiseAsInner = new DivideByZeroException();

Exception aggregateException = new AggregateException(
new Exception("First: Without Inner Exception"),
new Exception("Second: With Inner Exception",
toRaiseAsInner));

policy.RaiseException(aggregateException);

passedToOnRetry.Should().BeSameAs(toRaiseAsInner);
}

[Fact]
public void Should_call_onretry_with_handled_exception_nested_in_aggregate_inside_another_aggregate_as_first_exception()
{
Exception passedToOnRetry = null;

var policy = Policy
.HandleInner<DivideByZeroException>()
.Retry(3, (exception, _) => passedToOnRetry = exception);

Exception toRaiseAsInner = new DivideByZeroException();

Exception aggregateException = new AggregateException(
new AggregateException(
new Exception("First: With Inner Exception",
toRaiseAsInner),
new Exception("Second: Without Inner Exception")),
new Exception("Exception"));

policy.RaiseException(aggregateException);

passedToOnRetry.Should().BeSameAs(toRaiseAsInner);
}

[Fact]
public void Should_call_onretry_with_handled_exception_nested_in_aggregate_inside_another_aggregate_as_second_exception()
{
Exception passedToOnRetry = null;

var policy = Policy
.HandleInner<DivideByZeroException>()
.Retry(3, (exception, _) => passedToOnRetry = exception);

Exception toRaiseAsInner = new DivideByZeroException();

Exception aggregateException = new AggregateException(
new Exception("Exception"),
new AggregateException(
new Exception("First: Without Inner Exception"),
new Exception("Second: With Inner Exception",
toRaiseAsInner)));

policy.RaiseException(aggregateException);

passedToOnRetry.Should().BeSameAs(toRaiseAsInner);
}

[Fact]
public void Should_not_call_onretry_when_no_retries_are_performed()
{
Expand Down
9 changes: 7 additions & 2 deletions src/Polly/PolicyBuilder.OrSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,13 @@ internal static ExceptionPredicate HandleInner(Func<Exception, bool> predicate)
{
if (exception is AggregateException aggregateException)
{
Exception matchedInAggregate = aggregateException.Flatten().InnerExceptions.FirstOrDefault(predicate);
if (matchedInAggregate != null) return matchedInAggregate;
//search all inner exceptions wrapped inside the AggregateException recursively
foreach (var innerException in aggregateException.Flatten().InnerExceptions)
{
var matchedInAggregate = HandleInnerNested(predicate, innerException);
if (matchedInAggregate != null)
return matchedInAggregate;
}
}

return HandleInnerNested(predicate, exception);
Expand Down