You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LuExceptionMiddleware.cs 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using System.Threading.Tasks;
  3. using Luticate2.Utils.Controllers;
  4. using Luticate2.Utils.Dbo.Basic;
  5. using Microsoft.AspNetCore.Http;
  6. using Microsoft.AspNetCore.Mvc.Infrastructure;
  7. using Newtonsoft.Json;
  8. namespace Luticate2.Utils.Middlewares
  9. {
  10. public class LuExceptionMiddleware
  11. {
  12. private readonly RequestDelegate _next;
  13. public LuExceptionMiddleware(RequestDelegate next,
  14. IActionDescriptorCollectionProvider actionDescriptorCollectionProvider)
  15. {
  16. _next = next;
  17. }
  18. public async Task Invoke(HttpContext context)
  19. {
  20. try
  21. {
  22. await _next.Invoke(context);
  23. }
  24. catch (LuResultException e)
  25. {
  26. var response = context.Response;
  27. response.ContentType = "application/json";
  28. response.StatusCode = e.Result.GetHttpCode();
  29. await response.WriteAsync(JsonConvert.SerializeObject(new LuApiWrapperDbo<object>
  30. {
  31. Code = e.Result.GetHttpCode(),
  32. Data = null,
  33. Message = e.Result.PublicDetails
  34. })).ConfigureAwait(false);
  35. }
  36. catch (Exception e)//TODO
  37. {
  38. var response = context.Response;
  39. response.ContentType = "application/json";
  40. response.StatusCode = 500;
  41. await response.WriteAsync(JsonConvert.SerializeObject(new LuApiWrapperDbo<object>
  42. {
  43. Code = 500,
  44. Data = null,
  45. Message = "Internal Error"
  46. })).ConfigureAwait(false);
  47. }
  48. }
  49. }
  50. }