123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- using System;
-
- namespace Luticate2.Auth.Dbo.Result
- {
- public class LuResult<T>
- {
- public int Status { get; set; }
-
- public bool Success => Status == LuStatus.Success.ToInt();
-
- public string PublicDetails { get; set; }
-
- public string PrivateDetails { get; set; }
-
- public string StackTrace { get; set; }
-
- public Exception Exception { get; set; }
-
- public T Data { get; set; }
-
- protected LuResult()
- {
- }
-
- public static LuResult<T> Ok(T data)
- {
- return new LuResult<T>
- {
- StackTrace = new Exception().StackTrace,
- Data = data,
- Status = LuStatus.Success.ToInt()
- };
- }
-
- public static LuResult<T> Error(int status, string privateDetails, string publicDetails = null)
- {
- return new LuResult<T>
- {
- PublicDetails = publicDetails == "" ? privateDetails : publicDetails,
- PrivateDetails = privateDetails,
- StackTrace = new Exception().StackTrace,
- Status = status
- };
- }
-
- public static LuResult<T> Error(int status, Exception e, string publicDetails = null)
- {
- return new LuResult<T>
- {
- PublicDetails = publicDetails,
- PrivateDetails = e.ToString(),
- StackTrace = new Exception().StackTrace,
- Exception = e,
- Status = status
- };
- }
-
- public static LuResult<T> Error<T2>(LuResult<T2> other)
- {
- return new LuResult<T>
- {
- Status = other.Status,
- PublicDetails = other.PublicDetails,
- PrivateDetails = other.PrivateDetails,
- StackTrace = other.StackTrace,
- Exception = other.Exception
- };
- }
-
- public LuResult<T2> To<T2>()
- {
- return LuResult<T2>.Error(this);
- }
-
- public LuResult<T2> To<T2>(Func<T, T2> convert)
- {
- return new LuResult<T2>
- {
- Data = Success ? convert(Data) : default(T2),
- Status = Status,
- PublicDetails = PublicDetails,
- PrivateDetails = PrivateDetails,
- StackTrace = StackTrace,
- Exception = Exception
- };
- }
-
- public static implicit operator bool(LuResult<T> res)
- {
- return res.Success;
- }
- }
- }
|