FluentResults icon indicating copy to clipboard operation
FluentResults copied to clipboard

Value throws an InvalidOperationException after any call of .WithError()

Open Ewerton opened this issue 1 year ago • 1 comments

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''
}

Ewerton avatar Jan 26 '24 20:01 Ewerton

This is intended behavior. By design, you're not supposed to access the value of a failed result.

Kilazur avatar Jan 29 '24 09:01 Kilazur

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

altmann avatar Jun 19 '24 20:06 altmann