2019-11-03 02:49:39 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace Wabbajack
|
|
|
|
|
{
|
|
|
|
|
public struct ErrorResponse : IErrorResponse
|
|
|
|
|
{
|
|
|
|
|
public readonly static ErrorResponse Success = Succeed();
|
|
|
|
|
public readonly static ErrorResponse Failure = new ErrorResponse();
|
|
|
|
|
|
2020-01-14 04:57:12 +00:00
|
|
|
|
public bool Succeeded { get; }
|
2020-04-03 23:23:13 +00:00
|
|
|
|
public Exception? Exception { get; }
|
2019-11-03 02:49:39 +00:00
|
|
|
|
private readonly string _reason;
|
|
|
|
|
|
|
|
|
|
public bool Failed => !Succeeded;
|
|
|
|
|
public string Reason
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2019-11-21 15:04:48 +00:00
|
|
|
|
if (Exception != null)
|
2019-11-03 02:49:39 +00:00
|
|
|
|
{
|
2019-12-03 05:35:51 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(_reason))
|
|
|
|
|
{
|
|
|
|
|
return Exception.ToString();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return $"{_reason}: {Exception.Message}";
|
|
|
|
|
}
|
2019-11-03 02:49:39 +00:00
|
|
|
|
}
|
|
|
|
|
return _reason;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-21 15:04:48 +00:00
|
|
|
|
bool IErrorResponse.Succeeded => Succeeded;
|
2020-04-03 23:23:13 +00:00
|
|
|
|
Exception? IErrorResponse.Exception => Exception;
|
2019-11-03 02:49:39 +00:00
|
|
|
|
|
|
|
|
|
private ErrorResponse(
|
|
|
|
|
bool succeeded,
|
2020-04-03 23:23:13 +00:00
|
|
|
|
string? reason = null,
|
|
|
|
|
Exception? ex = null)
|
2019-11-03 02:49:39 +00:00
|
|
|
|
{
|
2019-11-21 15:04:48 +00:00
|
|
|
|
Succeeded = succeeded;
|
2020-04-03 23:23:13 +00:00
|
|
|
|
_reason = reason ?? string.Empty;
|
2019-11-21 15:04:48 +00:00
|
|
|
|
Exception = ex;
|
2019-11-03 02:49:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return $"({(Succeeded ? "Success" : "Fail")}, {Reason})";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region Factories
|
|
|
|
|
public static ErrorResponse Succeed()
|
|
|
|
|
{
|
|
|
|
|
return new ErrorResponse(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ErrorResponse Succeed(string reason)
|
|
|
|
|
{
|
|
|
|
|
return new ErrorResponse(true, reason);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-03 23:23:13 +00:00
|
|
|
|
public static ErrorResponse Fail(string reason, Exception? ex = null)
|
2019-11-03 02:49:39 +00:00
|
|
|
|
{
|
2019-12-03 05:35:51 +00:00
|
|
|
|
return new ErrorResponse(false, reason: reason, ex: ex);
|
2019-11-03 02:49:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ErrorResponse Fail(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
return new ErrorResponse(false, ex: ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ErrorResponse Fail()
|
|
|
|
|
{
|
|
|
|
|
return new ErrorResponse(false);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-03 23:23:13 +00:00
|
|
|
|
public static ErrorResponse Create(bool successful, string? reason = null)
|
2019-11-03 02:49:39 +00:00
|
|
|
|
{
|
|
|
|
|
return new ErrorResponse(successful, reason);
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
2019-11-10 01:08:41 +00:00
|
|
|
|
|
|
|
|
|
public static ErrorResponse Convert(IErrorResponse err, bool nullIsSuccess = true)
|
|
|
|
|
{
|
2019-11-21 15:04:48 +00:00
|
|
|
|
if (err == null) return Create(nullIsSuccess);
|
2019-11-10 01:08:41 +00:00
|
|
|
|
return new ErrorResponse(err.Succeeded, err.Reason, err.Exception);
|
|
|
|
|
}
|
2019-11-03 02:49:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public interface IErrorResponse
|
|
|
|
|
{
|
|
|
|
|
bool Succeeded { get; }
|
2020-04-03 23:23:13 +00:00
|
|
|
|
Exception? Exception { get; }
|
2019-11-03 02:49:39 +00:00
|
|
|
|
string Reason { get; }
|
|
|
|
|
}
|
|
|
|
|
}
|