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 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 Error(status, e.ToString(), publicDetails);
  38. }
  39. public static LuResult<T> Error<T2>(LuResult<T2> other)
  40. {
  41. return new LuResult<T>
  42. {
  43. Status = other.Status,
  44. PublicDetails = other.PublicDetails,
  45. PrivateDetails = other.PrivateDetails,
  46. StackTrace = other.StackTrace,
  47. Exception = other.Exception
  48. };
  49. }
  50. public LuResult<T2> To<T2>()
  51. {
  52. return LuResult<T2>.Error(this);
  53. }
  54. public static implicit operator bool(LuResult<T> res)
  55. {
  56. return res.Success;
  57. }
  58. }
  59. }