-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Artem Leshchev <[email protected]>
- Loading branch information
Showing
8 changed files
with
102 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/Bss.Platform.Api.Middlewares/Bss.Platform.Api.Middlewares.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<PackageId>Luxoft.Bss.Platform.Api.Middlewares</PackageId> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
|
||
namespace Bss.Platform.Api.Middlewares; | ||
|
||
public static class DependencyInjection | ||
{ | ||
public static IApplicationBuilder UsePlatformErrorsMiddleware(this IApplicationBuilder app) => | ||
app.UseMiddleware<ErrorsMiddleware>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System.Net; | ||
using System.Net.Mime; | ||
using System.Text.Json; | ||
|
||
using Bss.Platform.Api.Middlewares.Interfaces; | ||
|
||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Bss.Platform.Api.Middlewares; | ||
|
||
public class ErrorsMiddleware(RequestDelegate next, ILogger<ErrorsMiddleware> logger, IStatusCodeResolver? statusCodeResolver) | ||
{ | ||
public async Task Invoke(HttpContext context) | ||
{ | ||
try | ||
{ | ||
await next(context); | ||
} | ||
catch (Exception e) | ||
{ | ||
logger.LogError(e, "Request failed"); | ||
|
||
await this.HandleExceptionAsync(context, e); | ||
} | ||
} | ||
|
||
private Task HandleExceptionAsync(HttpContext context, Exception exception) | ||
{ | ||
context.Response.ContentType = MediaTypeNames.Application.Json; | ||
context.Response.StatusCode = (int)(statusCodeResolver?.Resolve(exception) ?? HttpStatusCode.InternalServerError); | ||
|
||
return context.Response.WriteAsync(JsonSerializer.Serialize(exception.GetBaseException().Message)); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/Bss.Platform.Api.Middlewares/Interfaces/IStatusCodeResolver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using System.Net; | ||
|
||
namespace Bss.Platform.Api.Middlewares.Interfaces; | ||
|
||
public interface IStatusCodeResolver | ||
{ | ||
HttpStatusCode Resolve(Exception exception); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters