Skip to content

Commit

Permalink
Fix backward compability for multiple targets
Browse files Browse the repository at this point in the history
  • Loading branch information
tebaikin committed Nov 2, 2023
1 parent 5e11fbc commit f537335
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -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

Expand All @@ -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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ public class UnhandledExceptionSettings
/// <summary>
/// List of exceptions to be ignored
/// </summary>
public List<Type> ExceptionsToIgnore = new() {
public List<Type> ExceptionsToIgnore = new()
{
typeof(TaskCanceledException),
typeof(OperationCanceledException),
typeof(ConnectionResetException)};
typeof(ConnectionResetException)
};
}
}

0 comments on commit f537335

Please sign in to comment.