123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- using System;
- using System.Diagnostics;
-
- namespace iiie.Logs.DBO
- {
- /// <summary>
- /// Represent the result of an operation, with details in case of failure and data in case of success
- /// </summary>
- /// <typeparam name="T">The data type</typeparam>
- public class OpResult<T>
- {
- /// <summary>
- /// Status of the operation
- /// </summary>
- public ResultStatus Status { get; set; }
-
- /// <summary>
- /// True if Status is Success, false otherwise
- /// </summary>
- public bool Success
- {
- get
- {
- return Status == ResultStatus.Success;
- }
- }
-
- /// <summary>
- /// Details on the error, or null if error details must not be shown to the user
- /// Only relevent when Success is false
- /// </summary>
- public string PublicDetails { get; set; }
-
- /// <summary>
- /// Technical details on the error
- /// Only relevent when Success is false
- /// </summary>
- public string PrivateDetails { get; set; }
-
- /// <summary>
- /// Original error stack trace
- /// </summary>
- public StackFrame[] StackFrames { get; set; }
-
- /// <summary>
- /// Caught exception, if any
- /// </summary>
- public Exception Exception { get; set; }
-
- /// <summary>
- /// The data returned by the call
- /// Only relevent if Success is true
- /// </summary>
- public T Data { get; set; }
-
- /// <summary>
- /// Create and returns a new OpResult with
- /// {Public,Private}Details = null, Data = data, Status = true
- /// Used for successfull operations
- /// </summary>
- public static OpResult<T> Ok(T data)
- {
- return new OpResult<T>
- {
- PublicDetails = null,
- PrivateDetails = null,
- StackFrames = new StackTrace().GetFrames(),
- Data = data,
- Status = ResultStatus.Success
- };
- }
-
- /// <summary>
- /// Create and returns a new OpResult with
- /// PrivateDetails = privateDetails, Data = null, Status = status
- /// If publicDetails == "", privateDetails will be used
- /// Used for all errors (invalid input, ...)
- /// </summary>
- public static OpResult<T> Error(ResultStatus status, string privateDetails, string publicDetails = null)
- {
- return new OpResult<T>
- {
- PublicDetails = publicDetails == "" ? privateDetails : publicDetails,
- PrivateDetails = privateDetails,
- StackFrames = new StackTrace().GetFrames(),
- Data = default(T),
- Status = status
- };
- }
-
- /// <summary>
- /// Create and returns a new OpResult with
- /// PrivateDetails = e to string, Data = null, Status = status
- /// If publicDetails == "", privateDetails will be used
- /// Used for all errors (invalid input, ...)
- /// </summary>
- public static OpResult<T> Error(ResultStatus status, Exception e, string userDetails = null)
- {
- string message = "";
- Exception inner = e;
- while (inner != null)
- {
- message += inner.GetType().Name + ": " + inner.Message + (inner.InnerException != null ? "\n" : "");
- inner = inner.InnerException;
- }
- var res = Error(status, message, userDetails);
- res.Exception = e;
- return res;
- }
-
- /// <summary>
- /// Translate an error to another type
- /// </summary>
- /// <typeparam name="T2">The original type</typeparam>
- /// <param name="other">The original error</param>
- /// <returns>The original error with the type T</returns>
- public static OpResult<T> Error<T2>(OpResult<T2> other)
- {
- return new OpResult<T>
- {
- PublicDetails = other.PublicDetails,
- PrivateDetails = other.PrivateDetails,
- StackFrames = other.StackFrames,
- Data = default(T),
- Status = other.Status
- };
- }
-
- /// <summary>
- /// Translate an error to another type
- /// </summary>
- /// <typeparam name="T2">The original type</typeparam>
- /// <returns>The original error with the type T</returns>
- public OpResult<T2> To<T2>()
- {
- return OpResult<T2>.Error(this);
- }
-
- /// <summary>
- /// Implicit converion to bool
- /// </summary>
- /// <param name="res">The result to be converted</param>
- /// <returns>True if Success, false otherwise</returns>
- public static implicit operator bool(OpResult<T> res)
- {
- return res.Success;
- }
-
- protected OpResult()
- {
- }
- }
- }
|