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;
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; }
internal bool HasResult => ExceptionDispatchInfo == null;
internal bool IsVoidResult => ((object)Result) is VoidResult;
[System.Runtime.CompilerServices.NullableContext(1)]
internal Outcome(Exception exception)
{
this = default(Outcome<TResult>);
ExceptionDispatchInfo = ExceptionDispatchInfo.Capture(Guard.NotNull<Exception>(exception, "exception"));
}
internal Outcome(TResult result)
{
this = default(Outcome<TResult>);
Result = result;
}
[System.Runtime.CompilerServices.NullableContext(1)]
internal Outcome(ExceptionDispatchInfo exceptionDispatchInfo)
{
this = default(Outcome<TResult>);
ExceptionDispatchInfo = Guard.NotNull<ExceptionDispatchInfo>(exceptionDispatchInfo, "exceptionDispatchInfo");
}
public void ThrowIfException()
{
ExceptionDispatchInfo?.Throw();
}
internal 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;
}
}
}