using System; using System.Threading.Tasks; using Luticate2.Utils.Controllers; using Luticate2.Utils.Dbo.Basic; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.Infrastructure; using Newtonsoft.Json; namespace Luticate2.Utils.Middlewares { public class LuExceptionMiddleware { private readonly RequestDelegate _next; public LuExceptionMiddleware(RequestDelegate next, IActionDescriptorCollectionProvider actionDescriptorCollectionProvider) { _next = next; } public async Task Invoke(HttpContext context) { try { await _next.Invoke(context); } catch (LuResultException e) { var response = context.Response; response.ContentType = "application/json"; response.StatusCode = e.Result.GetHttpCode(); await response.WriteAsync(JsonConvert.SerializeObject(new LuApiWrapperDbo { Code = e.Result.GetHttpCode(), Data = null, Message = e.Result.PublicDetails })).ConfigureAwait(false); } catch (Exception e)//TODO { var response = context.Response; response.ContentType = "application/json"; response.StatusCode = 500; await response.WriteAsync(JsonConvert.SerializeObject(new LuApiWrapperDbo { Code = 500, Data = null, Message = "Internal Error" })).ConfigureAwait(false); } } } }