Files
IntegrySmartWarehouse/Integry Smart Warehouse/Core/WebHost/Controller/ResponseBaseExceptionWrapper.cs
2023-05-09 10:53:29 +02:00

27 lines
762 B
C#

using System.Text.Json;
using Integry_Smart_Warehouse.Core.WebHost.Dto;
using Microsoft.AspNetCore.Diagnostics;
namespace Integry_Smart_Warehouse.Core.WebHost.Controller;
public class ResponseBaseExceptionWrapper
{
public async Task Invoke(HttpContext context)
{
var contextFeature = context.Features.Get<IExceptionHandlerFeature>();
if (contextFeature is { Error: { } })
{
context.Response.StatusCode = StatusCodes.Status200OK;
context.Response.ContentType = "application/json";
await context.Response.WriteAsync(JsonSerializer.Serialize(new ResponseBaseDto()
{
Esito = -1,
ErrorMessage = contextFeature.Error.Message
}, new JsonSerializerOptions()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
}));
}
}
}