1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- using System;
- using System.Buffers;
- using System.Threading.Tasks;
- using Luticate2.Utils.Controllers;
- using Luticate2.Utils.Dbo.Basic;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc.Formatters;
- using Microsoft.AspNetCore.Mvc.Infrastructure;
- using Newtonsoft.Json;
-
- namespace Luticate2.Utils.Middlewares
- {
- public class LuExceptionMiddleware
- {
- private readonly RequestDelegate _next;
-
- public LuExceptionMiddleware(RequestDelegate next)
- {
- _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<object>
- {
- code = e.Result.GetHttpCode(),
- data = null,
- message = e.Result.PublicDetails ?? e.Result.GetHttpString(),
- Version = LuUtilsExtensions.Options.Version
- })).ConfigureAwait(false);
- }
- catch (Exception e)//TODO
- {
- var response = context.Response;
- response.ContentType = "application/json";
- response.StatusCode = 500;
- await response.WriteAsync(JsonConvert.SerializeObject(new LuApiWrapperDbo<object>
- {
- code = 500,
- data = null,
- message = "Internal Error",
- Version = LuUtilsExtensions.Options.Version
- })).ConfigureAwait(false);
- }
- }
- }
- }
|