選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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 Newtonsoft.Json;
  7. namespace Luticate2.Utils.Middlewares
  8. {
  9. public class LuExceptionMiddleware
  10. {
  11. private readonly RequestDelegate _next;
  12. public LuExceptionMiddleware(RequestDelegate next)
  13. {
  14. _next = next;
  15. }
  16. public async Task Invoke(HttpContext context)
  17. {
  18. try
  19. {
  20. await _next.Invoke(context);
  21. }
  22. catch (LuResultException e)
  23. {
  24. var response = context.Response;
  25. response.ContentType = "application/json";
  26. response.StatusCode = e.Result.GetHttpCode();
  27. await response.WriteAsync(JsonConvert.SerializeObject(new LuApiWrapperDbo<object>
  28. {
  29. code = e.Result.GetHttpCode(),
  30. data = null,
  31. message = e.Result.PublicDetails ?? e.Result.GetHttpString(),
  32. Version = LuUtilsExtensions.Options.Version
  33. })).ConfigureAwait(false);
  34. }
  35. catch (Exception e)//TODO
  36. {
  37. var response = context.Response;
  38. response.ContentType = "application/json";
  39. response.StatusCode = 500;
  40. await response.WriteAsync(JsonConvert.SerializeObject(new LuApiWrapperDbo<object>
  41. {
  42. code = 500,
  43. data = null,
  44. message = "Internal Error",
  45. Version = LuUtilsExtensions.Options.Version
  46. })).ConfigureAwait(false);
  47. }
  48. }
  49. }
  50. }