POS-29
This commit is contained in:
parent
3e8eed3e25
commit
11f96f9d06
38
Serwer/Serwer.Api/Framework/ExceptionHandlerMiddleware.cs
Normal file
38
Serwer/Serwer.Api/Framework/ExceptionHandlerMiddleware.cs
Normal file
@ -0,0 +1,38 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serwer.Api.Framework
|
||||
{
|
||||
public class ExceptionHandlerMiddleware
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
|
||||
public ExceptionHandlerMiddleware(RequestDelegate next)
|
||||
{
|
||||
_next = next;
|
||||
}
|
||||
|
||||
public async Task Invoke(HttpContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _next(context);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await HandleExceptionAsync(context, ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static Task HandleExceptionAsync(HttpContext context, Exception exception)
|
||||
{
|
||||
var statusCode = HttpStatusCode.BadRequest;
|
||||
context.Response.StatusCode = (int)statusCode;
|
||||
return context.Response.WriteAsync($"Error: {exception.Message}");
|
||||
}
|
||||
}
|
||||
}
|
@ -15,6 +15,7 @@ using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Serwer.Api.Framework;
|
||||
using Serwer.Core.Repositories;
|
||||
using Serwer.Infrastructure.Mappers;
|
||||
using Serwer.Infrastructure.Repositories;
|
||||
@ -88,6 +89,8 @@ namespace Serwer.Api
|
||||
}
|
||||
app.UseRouting();
|
||||
|
||||
app.UseMiddleware(typeof(ExceptionHandlerMiddleware));
|
||||
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user