Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

LuExceptionMiddleware.cs 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 ?? e.Result.GetHttpString(),
  35. Version = LuUtilsExtensions.Options.Version
  36. })).ConfigureAwait(false);
  37. }
  38. catch (Exception e)//TODO
  39. {
  40. var response = context.Response;
  41. response.ContentType = "application/json";
  42. response.StatusCode = 500;
  43. await response.WriteAsync(JsonConvert.SerializeObject(new LuApiWrapperDbo<object>
  44. {
  45. code = 500,
  46. data = null,
  47. message = "Internal Error",
  48. Version = LuUtilsExtensions.Options.Version
  49. })).ConfigureAwait(false);
  50. }
  51. }
  52. }
  53. }