FluentResults
FluentResults copied to clipboard
Value throws an InvalidOperationException after any call of .WithError()
Here are the steps to reproduce the behaviour.
public static void Test()
{
Result<Customer> result = Result.Ok(new Customer() { Name = "Name", Email = "[email protected]"});
var valueBefore = result.Value; // This works. The Value is not null
result.WithError(new Error("Some Error"));
var valueAfter = result.Value; // This will throw a exception: System.InvalidOperationException: 'Result is in status failed. Value is not set. Having: Error with Message='Some Error''
}
This is intended behavior. By design, you're not supposed to access the value of a failed result.
Thank you for your question. @Kilazur already answered it - it makes no sense to query the Value property if your Result is failed. Therefore I implemented this safety net and throw in this case an exception. Details see in the docs