/
vshmidt
/
masstransit
Обзор
Документация
Войти
/
vshmidt
/
masstransit
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
src/MassTransit.Abstractions/Middleware/Configuration/ExceptionSpecification.cs
109 строк
3 KB
Chris Patterson
AggregateException should be recognized by the ExceptionFilter and properly inspected to determine if an exception should be handled or ignored.
11 июн 2025, 20:33
11 июн 2025, 20:33
5dcd785
Код
Авторство
О чём код?
namespace MassTransit.Configuration { using System; using ExceptionFilters; public abstract class ExceptionSpecification : IExceptionConfigurator { readonly CompositeFilter<Exception> _exceptionFilter; protected ExceptionSpecification() { _exceptionFilter = new CompositeFilter<Exception>(); Filter = new CompositeExceptionFilter(_exceptionFilter); } protected IExceptionFilter Filter { get; } public void Handle(params Type[] exceptionTypes) { _exceptionFilter.Includes += exception => Match(exception, exceptionTypes); } public void Handle<T>() where T : Exception { _exceptionFilter.Includes += exception => Match(exception, typeof(T)); } public void Handle<T>(Func<T, bool> filter) where T : Exception { _exceptionFilter.Includes += exception => Match(exception, filter); } public void Ignore(params Type[] exceptionTypes) { _exceptionFilter.Excludes += exception => Match(exception, exceptionTypes); } public void Ignore<T>() where T : Exception { _exceptionFilter.Excludes += exception => Match(exception, typeof(T)); } public void Ignore<T>(Func<T, bool> filter) where T : Exception { _exceptionFilter.Excludes += exception => Match(exception, filter); } static bool Match(Exception exception, params Type[] exceptionTypes) { var baseException = exception.GetBaseException(); if (baseException is AggregateException aggregateException) { foreach (var innerException in aggregateException.InnerExceptions) { var baseInnerException = innerException.GetBaseException(); for (var i = 0; i < exceptionTypes.Length; i++) { if (exceptionTypes[i].IsInstanceOfType(innerException)) return true; if (exceptionTypes[i].IsInstanceOfType(baseInnerException)) return true; } } } for (var i = 0; i < exceptionTypes.Length; i++) { if (exceptionTypes[i].IsInstanceOfType(exception)) return true; if (exceptionTypes[i].IsInstanceOfType(baseException)) return true; } return false; } static bool Match<T>(Exception exception, Func<T, bool> filter) where T : Exception { if (exception is T ofT) return filter(ofT); var baseException = exception.GetBaseException(); if (baseException is AggregateException aggregateException) { foreach (var innerException in aggregateException.InnerExceptions) { var baseInnerException = innerException.GetBaseException(); if (baseInnerException is T innerExceptionOfT && filter(innerExceptionOfT)) return true; } } return baseException is T exceptionOfT && filter(exceptionOfT); } } }