develop #10
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.Extensions.Logging;
|
||||||
using Microsoft.IdentityModel.Tokens;
|
using Microsoft.IdentityModel.Tokens;
|
||||||
using Microsoft.OpenApi.Models;
|
using Microsoft.OpenApi.Models;
|
||||||
|
using Serwer.Api.Framework;
|
||||||
using Serwer.Core.Repositories;
|
using Serwer.Core.Repositories;
|
||||||
using Serwer.Infrastructure.Mappers;
|
using Serwer.Infrastructure.Mappers;
|
||||||
using Serwer.Infrastructure.Repositories;
|
using Serwer.Infrastructure.Repositories;
|
||||||
@ -88,6 +89,8 @@ namespace Serwer.Api
|
|||||||
}
|
}
|
||||||
app.UseRouting();
|
app.UseRouting();
|
||||||
|
|
||||||
|
app.UseMiddleware(typeof(ExceptionHandlerMiddleware));
|
||||||
|
|
||||||
app.UseAuthentication();
|
app.UseAuthentication();
|
||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user