У меня есть общий класс, который перехватывает исключения T:
public abstract class ErrorHandlingOperationInterceptor<T> : OperationInterceptor where T : ApiException { private readonly Func<OperationResult> _resultFactory; protected ErrorHandlingOperationInterceptor(Func<OperationResult> resultFactory) { _resultFactory = resultFactory; } public override Func<IEnumerable<OutputMember>> RewriteOperation(Func<IEnumerable<OutputMember>> operationBuilder) { return () => { try { return operationBuilder(); } catch (T ex) { var operationResult = _resultFactory(); operationResult.ResponseResource = new ApiErrorResource { Exception = ex }; return operationResult.AsOutput(); } }; } }
с подклассами для определенных исключений, например
public class BadRequestOperationInterceptor : ErrorHandlingOperationInterceptor<BadRequestException> { public BadRequestOperationInterceptor() : base(() => new OperationResult.BadRequest()) { } }
Кажется, все работает отлично. Но каким-то образом в журналах (один раз, а не каждый раз) есть InvalidCastException:
System.InvalidCastException: Unable to cast object of type 'ErrorHandling.Exceptions.ApiException' to type 'ErrorHandling.Exceptions.UnexpectedInternalServerErrorException'. at OperationModel.Interceptors.ErrorHandlingOperationInterceptor`1.c__DisplayClass2.b__1() in c:\BuildAgent\work\da77ba20595a9d4\src\OperationModel\Interceptors\ErrorHandlingOperationInterceptor.cs:line 28
Строка 28 - уловка.
Что мне не хватает? Я сделал что-то действительно глупое?