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.

LuResult.cs 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. namespace Luticate2.Utils.Dbo
  3. {
  4. public class LuResult<T>
  5. {
  6. public LuStatus Status { get; set; }
  7. public bool Success => Status == LuStatus.Success;
  8. public string PublicDetails { get; set; }
  9. public string PrivateDetails { get; set; }
  10. public string StackTrace { get; set; }
  11. public Exception Exception { get; set; }
  12. public T Data { get; set; }
  13. protected LuResult()
  14. {
  15. }
  16. public static LuResult<T> Ok(T data)
  17. {
  18. return new LuResult<T>
  19. {
  20. StackTrace = new Exception().StackTrace,
  21. Data = data,
  22. Status = LuStatus.Success
  23. };
  24. }
  25. public static LuResult<T> Error(LuStatus status, string privateDetails, string publicDetails = null)
  26. {
  27. return new LuResult<T>
  28. {
  29. PublicDetails = publicDetails == "" ? privateDetails : publicDetails,
  30. PrivateDetails = privateDetails,
  31. StackTrace = new Exception().StackTrace,
  32. Status = status
  33. };
  34. }
  35. public static LuResult<T> Error(LuStatus status, Exception e, string publicDetails = null)
  36. {
  37. return new LuResult<T>
  38. {
  39. PublicDetails = publicDetails,
  40. PrivateDetails = e.ToString(),
  41. StackTrace = new Exception().StackTrace,
  42. Exception = e,
  43. Status = status
  44. };
  45. }
  46. public static LuResult<T> Error<T2>(LuResult<T2> other)
  47. {
  48. return new LuResult<T>
  49. {
  50. Status = other.Status,
  51. PublicDetails = other.PublicDetails,
  52. PrivateDetails = other.PrivateDetails,
  53. StackTrace = other.StackTrace,
  54. Exception = other.Exception
  55. };
  56. }
  57. public LuResult<T2> To<T2>()
  58. {
  59. return LuResult<T2>.Error(this);
  60. }
  61. public static implicit operator bool(LuResult<T> res)
  62. {
  63. return res.Success;
  64. }
  65. }
  66. }