Outcome<TResult>
Represents the outcome of an operation which could be a result of type TResult or an exception.
using Polly.Utils;
using System;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using System.Threading.Tasks;
namespace Polly
{
[System.Runtime.CompilerServices.NullableContext(2)]
[System.Runtime.CompilerServices.Nullable(0)]
public readonly struct Outcome<TResult>
{
public Exception Exception => ExceptionDispatchInfo?.SourceException;
internal ExceptionDispatchInfo ExceptionDispatchInfo { get; }
public TResult Result { get; }
public bool HasResult => ExceptionDispatchInfo == null;
public bool IsVoidResult => ((object)Result) is VoidResult;
[System.Runtime.CompilerServices.NullableContext(1)]
public Outcome(Exception exception)
{
this = default(Outcome<TResult>);
ExceptionDispatchInfo = ExceptionDispatchInfo.Capture(Guard.NotNull<Exception>(exception, "exception"));
}
[System.Runtime.CompilerServices.NullableContext(1)]
internal Outcome(ExceptionDispatchInfo exceptionDispatchInfo)
{
this = default(Outcome<TResult>);
ExceptionDispatchInfo = Guard.NotNull<ExceptionDispatchInfo>(exceptionDispatchInfo, "exceptionDispatchInfo");
}
public Outcome(TResult result)
{
this = default(Outcome<TResult>);
Result = result;
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
0,
1
})]
internal ValueTask<Outcome<TResult>> AsValueTask()
{
return new ValueTask<Outcome<TResult>>(this);
}
public void EnsureSuccess()
{
ExceptionDispatchInfo?.Throw();
}
public bool TryGetResult(out TResult result)
{
if (HasResult && !IsVoidResult) {
result = Result;
return true;
}
result = default(TResult);
return false;
}
[System.Runtime.CompilerServices.NullableContext(1)]
public override string ToString()
{
object obj;
if (ExceptionDispatchInfo == null) {
TResult result = Result;
obj = result?.ToString();
if (obj == null)
return string.Empty;
} else
obj = Exception.Message;
return (string)obj;
}
[System.Runtime.CompilerServices.NullableContext(1)]
internal TResult GetResultOrRethrow()
{
ExceptionDispatchInfo?.Throw();
return Result;
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})]
internal Outcome<object> AsOutcome()
{
return AsOutcome<object>();
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})]
internal Outcome<T> AsOutcome<T>()
{
if (this.ExceptionDispatchInfo == null)
return new Outcome<T>((T)(object)this.Result);
return new Outcome<T>(this.ExceptionDispatchInfo);
}
}
}