diff --git a/Vostok.Applications.AspNetCore.Tests/Controllers/ExceptionController.cs b/Vostok.Applications.AspNetCore.Tests/Controllers/ExceptionController.cs index d1ade67..341b757 100644 --- a/Vostok.Applications.AspNetCore.Tests/Controllers/ExceptionController.cs +++ b/Vostok.Applications.AspNetCore.Tests/Controllers/ExceptionController.cs @@ -1,7 +1,9 @@ using System; +using Microsoft.AspNetCore.Mvc; +#if NET5_0_OR_GREATER using System.Threading; using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; +#endif namespace Vostok.Applications.AspNetCore.Tests.Controllers { @@ -11,17 +13,14 @@ public class ExceptionController : ControllerBase { [HttpGet] public void Throw() => throw new NotImplementedException(); - } - - [ApiController] - [Route("canceled-bad-http-exception")] - public class BadHttpExceptionController : ControllerBase - { - [HttpGet] - public void Throw() + +#if NET5_0_OR_GREATER + [HttpGet("/canceled-bad-http-exception")] + public void ThrowBadRequestException() { Request.HttpContext.RequestAborted = new CancellationToken(true); throw new BadHttpRequestException(""); } +#endif } } \ No newline at end of file diff --git a/Vostok.Applications.AspNetCore.Tests/MiddlewareTests/UnhandledExceptionMiddlewareTests.cs b/Vostok.Applications.AspNetCore.Tests/MiddlewareTests/UnhandledExceptionMiddlewareTests.cs index d5f7a33..781edbc 100644 --- a/Vostok.Applications.AspNetCore.Tests/MiddlewareTests/UnhandledExceptionMiddlewareTests.cs +++ b/Vostok.Applications.AspNetCore.Tests/MiddlewareTests/UnhandledExceptionMiddlewareTests.cs @@ -1,11 +1,14 @@ using System.Threading.Tasks; using FluentAssertions; -using Microsoft.AspNetCore.Http; using NUnit.Framework; using Vostok.Applications.AspNetCore.Builders; +using Vostok.Applications.AspNetCore.Configuration; using Vostok.Applications.AspNetCore.Tests.Extensions; using Vostok.Applications.AspNetCore.Tests.TestHelpers; using Vostok.Hosting.Abstractions; +#if NET5_0_OR_GREATER +using Microsoft.AspNetCore.Http; +#endif namespace Vostok.Applications.AspNetCore.Tests.MiddlewareTests { @@ -41,21 +44,13 @@ public async Task Invoke_ShouldNotCatch_IgnoredUnhandledExceptions() protected override void SetupGlobal(IVostokAspNetCoreApplicationBuilder builder, IVostokHostingEnvironment environment) { - builder.SetupUnhandledExceptions(s => - { - s.ErrorResponseCode = ResponseCode; - s.ExceptionsToIgnore.Add(typeof(BadHttpRequestException)); - }); + builder.SetupUnhandledExceptions(SetupUnhandledExceptions); } #if NET6_0_OR_GREATER protected override void SetupGlobal(IVostokAspNetCoreWebApplicationBuilder builder, IVostokHostingEnvironment environment) { - builder.SetupUnhandledExceptions(s => - { - s.ErrorResponseCode = ResponseCode; - s.ExceptionsToIgnore.Add(typeof(BadHttpRequestException)); - }); + builder.SetupUnhandledExceptions(SetupUnhandledExceptions); } #endif @@ -65,5 +60,13 @@ protected override void SetupGlobal(Microsoft.AspNetCore.Builder.WebApplicationB middlewaresConfigurator.ConfigureUnhandledExceptions(s => s.ErrorResponseCode = ResponseCode); } #endif + + private static void SetupUnhandledExceptions(UnhandledExceptionSettings settings) + { + settings.ErrorResponseCode = ResponseCode; +#if NET5_0_OR_GREATER + settings.ExceptionsToIgnore.Add(typeof(BadHttpRequestException)); +#endif + } } } \ No newline at end of file diff --git a/Vostok.Applications.AspNetCore/Configuration/UnhandledExceptionSettings.cs b/Vostok.Applications.AspNetCore/Configuration/UnhandledExceptionSettings.cs index b351f0f..92cd2bd 100644 --- a/Vostok.Applications.AspNetCore/Configuration/UnhandledExceptionSettings.cs +++ b/Vostok.Applications.AspNetCore/Configuration/UnhandledExceptionSettings.cs @@ -21,9 +21,11 @@ public class UnhandledExceptionSettings /// /// List of exceptions to be ignored /// - public List ExceptionsToIgnore = new() { + public List ExceptionsToIgnore = new() + { typeof(TaskCanceledException), typeof(OperationCanceledException), - typeof(ConnectionResetException)}; + typeof(ConnectionResetException) + }; } } \ No newline at end of file