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

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