Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Errors middleware #9

Merged
merged 3 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@

This repository offers a wide collection of .NET packages for use in microservices architecture.

# Sections

- [RabbitMQ Consumer](#Consumer)
- [Logging](#Logging)
- [API Middlewares](#Middlewares)
- [API Documentation](#Documentation)
- [Kubernetes Insights](#Insights)
- [Kubernetes Health Checks](#Health-Checks)
- [NHibernate](#NHibernate)

## RabbitMQ

### Consumer
Expand Down Expand Up @@ -85,7 +95,26 @@ Finally, register sinks in appsettings.json file
}
}
```
## API Documentation
## API

### Middlewares
To use platform API middlewares, first install the [NuGet package](https://www.nuget.org/packages/Luxoft.Bss.Platform.Api.Middlewares):
```shell
dotnet add package Luxoft.Bss.Platform.Api.Middlewares
```

#### Errors
To log exceptions you need to use errors middleware.
```C#
app.UsePlatformErrorsMiddleware(); // This middleware should be the first
```
> [!IMPORTANT]
> If you need to change response status code, then you should register status code resolver.
```C#
services.AddSingleton<IStatusCodeResolver, YourStatusCodeResolver>();
```

### Documentation
To use platform API documentation, first install the [NuGet package](https://www.nuget.org/packages/Luxoft.Bss.Platform.Api.Documentation):
```shell
dotnet add package Luxoft.Bss.Platform.Api.Documentation
Expand Down
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>
9 changes: 9 additions & 0 deletions src/Bss.Platform.Api.Middlewares/DependencyInjection.cs
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>();
}
35 changes: 35 additions & 0 deletions src/Bss.Platform.Api.Middlewares/ErrorsMiddleware.cs
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));
}
}
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);
}
7 changes: 7 additions & 0 deletions src/Bss.Platform.sln
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Kubernetes", "Kubernetes",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bss.Platform.Kubernetes", "Bss.Platform.Kubernetes\Bss.Platform.Kubernetes.csproj", "{1BD00077-8005-42E9-AEFE-62A21E057858}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bss.Platform.Api.Middlewares", "Bss.Platform.Api.Middlewares\Bss.Platform.Api.Middlewares.csproj", "{285EBB7B-6B2A-453E-98F6-30AC38317A76}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -39,6 +41,7 @@ Global
{2417B26C-C12D-4E63-8996-395DD78F81A1} = {D07584C3-F6D5-472C-8515-7D38F4C54F11}
{0005DAE1-3F66-4A98-A11B-DEFDF2BD7EA3} = {8F22FE4E-8FD4-4309-8B47-C33AA6E52052}
{1BD00077-8005-42E9-AEFE-62A21E057858} = {51F57F48-6CDC-43E2-90DA-B73C12EA2185}
{285EBB7B-6B2A-453E-98F6-30AC38317A76} = {8F22FE4E-8FD4-4309-8B47-C33AA6E52052}
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{860BFBD8-26EA-44F9-980E-21B828FC8F72}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -69,5 +72,9 @@ Global
{1BD00077-8005-42E9-AEFE-62A21E057858}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1BD00077-8005-42E9-AEFE-62A21E057858}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1BD00077-8005-42E9-AEFE-62A21E057858}.Release|Any CPU.Build.0 = Release|Any CPU
{285EBB7B-6B2A-453E-98F6-30AC38317A76}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{285EBB7B-6B2A-453E-98F6-30AC38317A76}.Debug|Any CPU.Build.0 = Debug|Any CPU
{285EBB7B-6B2A-453E-98F6-30AC38317A76}.Release|Any CPU.ActiveCfg = Release|Any CPU
{285EBB7B-6B2A-453E-98F6-30AC38317A76}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
1 change: 1 addition & 0 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<PackageVersion Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0" />
<PackageVersion Include="Microsoft.ApplicationInsights.Kubernetes" Version="6.1.2" />
<PackageVersion Include="Microsoft.AspNetCore.Diagnostics.HealthChecks" Version="2.2.0" />
<PackageVersion Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageVersion Include="Microsoft.Data.SqlClient" Version="5.2.0" />
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
Expand Down
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.2.0.0")]
[assembly: AssemblyFileVersion("1.2.0.0")]
[assembly: AssemblyInformationalVersion("1.2.0.0")]
[assembly: AssemblyVersion("1.3.0.0")]
[assembly: AssemblyFileVersion("1.3.0.0")]
[assembly: AssemblyInformationalVersion("1.3.0.0")]

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