Skip to content

Commit

Permalink
Fix DI in ErrorsMiddlewarte (#18)
Browse files Browse the repository at this point in the history
Co-authored-by: Artem Leshchev <[email protected]>
  • Loading branch information
aleshchev and Artem Leshchev authored Jul 8, 2024
1 parent 3d498c1 commit 4730032
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
14 changes: 6 additions & 8 deletions src/Bss.Platform.Api.Middlewares/ErrorsMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
using Bss.Platform.Api.Middlewares.Interfaces;

using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Bss.Platform.Api.Middlewares;

public class ErrorsMiddleware(RequestDelegate next, ILogger<ErrorsMiddleware> logger, IServiceProvider serviceProvider)
public class ErrorsMiddleware(RequestDelegate next)
{
public async Task Invoke(HttpContext context)
public async Task InvokeAsync(HttpContext context, ILogger<ErrorsMiddleware> logger, IStatusCodeResolver? statusCodeResolver)
{
try
{
Expand All @@ -21,16 +20,15 @@ public async Task Invoke(HttpContext context)
{
logger.LogError(e, "Request failed");

await this.HandleExceptionAsync(context, e);
await HandleExceptionAsync(context, e, statusCodeResolver);
}
}

private Task HandleExceptionAsync(HttpContext context, Exception exception)
private static async Task HandleExceptionAsync(HttpContext context, Exception exception, IStatusCodeResolver? statusCodeResolver)
{
context.Response.ContentType = MediaTypeNames.Text.Plain;
context.Response.StatusCode =
(int)(serviceProvider.GetService<IStatusCodeResolver>()?.Resolve(exception) ?? HttpStatusCode.InternalServerError);
context.Response.StatusCode = (int)(statusCodeResolver?.Resolve(exception) ?? HttpStatusCode.InternalServerError);

return context.Response.WriteAsync(exception.GetBaseException().Message);
await context.Response.WriteAsync(exception.GetBaseException().Message, context.RequestAborted);
}
}
6 changes: 3 additions & 3 deletions src/__SolutionItems/CommonAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
[assembly: AssemblyCompany("Luxoft")]
[assembly: AssemblyCopyright("Copyright © Luxoft 2024")]

[assembly: AssemblyVersion("1.5.3.0")]
[assembly: AssemblyFileVersion("1.5.3.0")]
[assembly: AssemblyInformationalVersion("1.5.3.0")]
[assembly: AssemblyVersion("1.5.4.0")]
[assembly: AssemblyFileVersion("1.5.4.0")]
[assembly: AssemblyInformationalVersion("1.5.4.0")]

#if DEBUG
[assembly: AssemblyConfiguration("Debug")]
Expand Down

0 comments on commit 4730032

Please sign in to comment.