SES-91 Added error middleware
This commit is contained in:
parent
6131bbe706
commit
9efefd96fe
@ -0,0 +1,26 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace SessionCompanion.Extensions.ApiErrors
|
||||||
|
{
|
||||||
|
public class ErrorDetails
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Kod błędu
|
||||||
|
/// </summary>
|
||||||
|
public int StatusCode { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Wiadomość błędu do pokazania
|
||||||
|
/// </summary>
|
||||||
|
public string Message { get; set; }
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return JsonSerializer.Serialize(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -4,4 +4,8 @@
|
|||||||
<TargetFramework>net5.0</TargetFramework>
|
<TargetFramework>net5.0</TargetFramework>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
@ -4,9 +4,6 @@ Microsoft Visual Studio Solution File, Format Version 12.00
|
|||||||
VisualStudioVersion = 16.0.30717.126
|
VisualStudioVersion = 16.0.30717.126
|
||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SessionCompanion", "SessionCompanion\SessionCompanion.csproj", "{C646135F-16CE-4B16-B041-252D343D4E01}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SessionCompanion", "SessionCompanion\SessionCompanion.csproj", "{C646135F-16CE-4B16-B041-252D343D4E01}"
|
||||||
ProjectSection(ProjectDependencies) = postProject
|
|
||||||
{0CBA48BB-7C1F-42FB-9008-C75A7681ED09} = {0CBA48BB-7C1F-42FB-9008-C75A7681ED09}
|
|
||||||
EndProjectSection
|
|
||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SessionCompanion.Database", "SessionCompanion.Database\SessionCompanion.Database.csproj", "{CA05189B-A4AB-4946-80DC-EFA075A10F09}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SessionCompanion.Database", "SessionCompanion.Database\SessionCompanion.Database.csproj", "{CA05189B-A4AB-4946-80DC-EFA075A10F09}"
|
||||||
EndProject
|
EndProject
|
||||||
@ -14,7 +11,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SessionCompanion.ViewModels
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SessionCompanion.Services", "SessionCompanion.Services\SessionCompanion.Services.csproj", "{C0A172ED-0F4C-4E78-8B64-28E2A756F62F}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SessionCompanion.Services", "SessionCompanion.Services\SessionCompanion.Services.csproj", "{C0A172ED-0F4C-4E78-8B64-28E2A756F62F}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SessionCompanion.ApiExtensions", "SessionCompanion.ApiReturn\SessionCompanion.ApiExtensions.csproj", "{0CBA48BB-7C1F-42FB-9008-C75A7681ED09}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SessionCompanion.Extensions", "SessionCompanion.Extensions\SessionCompanion.Extensions.csproj", "{1EE35EB3-C703-407C-B390-5605A0A46884}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
@ -38,10 +35,10 @@ Global
|
|||||||
{C0A172ED-0F4C-4E78-8B64-28E2A756F62F}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{C0A172ED-0F4C-4E78-8B64-28E2A756F62F}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{C0A172ED-0F4C-4E78-8B64-28E2A756F62F}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{C0A172ED-0F4C-4E78-8B64-28E2A756F62F}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{C0A172ED-0F4C-4E78-8B64-28E2A756F62F}.Release|Any CPU.Build.0 = Release|Any CPU
|
{C0A172ED-0F4C-4E78-8B64-28E2A756F62F}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
{0CBA48BB-7C1F-42FB-9008-C75A7681ED09}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
{1EE35EB3-C703-407C-B390-5605A0A46884}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
{0CBA48BB-7C1F-42FB-9008-C75A7681ED09}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{1EE35EB3-C703-407C-B390-5605A0A46884}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{0CBA48BB-7C1F-42FB-9008-C75A7681ED09}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{1EE35EB3-C703-407C-B390-5605A0A46884}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{0CBA48BB-7C1F-42FB-9008-C75A7681ED09}.Release|Any CPU.Build.0 = Release|Any CPU
|
{1EE35EB3-C703-407C-B390-5605A0A46884}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
@ -0,0 +1,43 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace SessionCompanion.Configurations
|
||||||
|
{
|
||||||
|
using System.Net;
|
||||||
|
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
|
||||||
|
using SessionCompanion.Extensions.ApiErrors;
|
||||||
|
|
||||||
|
public class ExceptionMiddleware
|
||||||
|
{
|
||||||
|
private readonly RequestDelegate _next;
|
||||||
|
public ExceptionMiddleware(RequestDelegate next)
|
||||||
|
{
|
||||||
|
_next = next;
|
||||||
|
}
|
||||||
|
public async Task InvokeAsync(HttpContext httpContext)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await _next(httpContext);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
await HandleExceptionAsync(httpContext, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private Task HandleExceptionAsync(HttpContext context, Exception exception)
|
||||||
|
{
|
||||||
|
context.Response.ContentType = "application/json";
|
||||||
|
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
|
||||||
|
return context.Response.WriteAsync(new ErrorDetails()
|
||||||
|
{
|
||||||
|
StatusCode = context.Response.StatusCode,
|
||||||
|
Message = exception.Message
|
||||||
|
}.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using System.Net;
|
||||||
|
|
||||||
|
namespace SessionCompanion.Extensions.ApiErrors
|
||||||
|
{
|
||||||
|
using Microsoft.AspNetCore.Diagnostics;
|
||||||
|
|
||||||
|
using SessionCompanion.Configurations;
|
||||||
|
|
||||||
|
public static class ExceptionMiddlewareExtensions
|
||||||
|
{
|
||||||
|
public static void ConfigureCustomExceptionMiddleware(this IApplicationBuilder app)
|
||||||
|
{
|
||||||
|
app.UseMiddleware<ExceptionMiddleware>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -41,8 +41,8 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\SessionCompanion.ApiReturn\SessionCompanion.ApiExtensions.csproj" />
|
|
||||||
<ProjectReference Include="..\SessionCompanion.Database\SessionCompanion.Database.csproj" />
|
<ProjectReference Include="..\SessionCompanion.Database\SessionCompanion.Database.csproj" />
|
||||||
|
<ProjectReference Include="..\SessionCompanion.Extensions\SessionCompanion.Extensions.csproj" />
|
||||||
<ProjectReference Include="..\SessionCompanion.Services\SessionCompanion.Services.csproj" />
|
<ProjectReference Include="..\SessionCompanion.Services\SessionCompanion.Services.csproj" />
|
||||||
<ProjectReference Include="..\SessionCompanion.ViewModels\SessionCompanion.ViewModels.csproj" />
|
<ProjectReference Include="..\SessionCompanion.ViewModels\SessionCompanion.ViewModels.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
@ -21,6 +21,8 @@ namespace SessionCompanion
|
|||||||
|
|
||||||
using Microsoft.OpenApi.Models;
|
using Microsoft.OpenApi.Models;
|
||||||
|
|
||||||
|
using SessionCompanion.Extensions.ApiErrors;
|
||||||
|
|
||||||
public class Startup
|
public class Startup
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -83,6 +85,8 @@ namespace SessionCompanion
|
|||||||
app.UseHsts();
|
app.UseHsts();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
app.ConfigureCustomExceptionMiddleware();
|
||||||
|
|
||||||
app.UseHttpsRedirection();
|
app.UseHttpsRedirection();
|
||||||
app.UseStaticFiles();
|
app.UseStaticFiles();
|
||||||
if (!env.IsDevelopment())
|
if (!env.IsDevelopment())
|
||||||
|
Loading…
Reference in New Issue
Block a user