| | | 1 | | namespace Elsa.Resilience; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Default implementation of <see cref="ITransientExceptionDetector"/> that delegates to registered detectors. |
| | | 5 | | /// </summary> |
| | 10 | 6 | | public class TransientExceptionDetector(IEnumerable<ITransientExceptionStrategy> detectors) : ITransientExceptionDetecto |
| | | 7 | | { |
| | 10 | 8 | | private readonly IReadOnlyList<ITransientExceptionStrategy> _detectors = detectors.ToList(); |
| | | 9 | | |
| | | 10 | | /// <inheritdoc /> |
| | | 11 | | public bool IsTransient(Exception exception) |
| | | 12 | | { |
| | 28 | 13 | | var detectorsList = _detectors; |
| | | 14 | | |
| | | 15 | | // Handle aggregate exceptions specially to avoid redundant checks |
| | 28 | 16 | | if (exception is AggregateException aggregateException) |
| | | 17 | | { |
| | | 18 | | // Check the aggregate exception itself |
| | 6 | 19 | | if (detectorsList.Any(detector => detector.IsTransient(aggregateException))) |
| | 0 | 20 | | return true; |
| | | 21 | | |
| | | 22 | | // Recursively check each inner exception (this will walk their chains) |
| | 3 | 23 | | if (aggregateException.InnerExceptions.Any(IsTransient)) |
| | 2 | 24 | | return true; |
| | | 25 | | |
| | 1 | 26 | | return false; |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | // Walk the exception chain for non-aggregate exceptions |
| | 25 | 30 | | var currentException = exception; |
| | 33 | 31 | | while (currentException != null) |
| | | 32 | | { |
| | | 33 | | // Check if any detector identifies this exception as transient |
| | 57 | 34 | | if (detectorsList.Any(detector => detector.IsTransient(currentException))) |
| | 20 | 35 | | return true; |
| | | 36 | | |
| | 8 | 37 | | currentException = currentException.InnerException; |
| | | 38 | | } |
| | | 39 | | |
| | 5 | 40 | | return false; |
| | | 41 | | } |
| | | 42 | | } |