Result created using Result.Fail with empty error list becomes a success
Perhaps rather naively, I'd expect the following code to either fail at runtime or the assertion to succeed.
var result = Result.Fail(new List<Error>())
result.IsFailed.Should().BeTrue();
But result.IsFailed turns out to be false, which seems a bit weird to me for an object that was created with Result.Fail().
Is it supposed to work like this?
It's because the list of errors is empty, which is probably assumed to not have any errors.
The examples below work fine:
var result = Result.Fail(new List<Error> { new Error("") });
result.IsFailed.Should().BeTrue();
var result = Result.Fail(new List<string> { "" });
result.IsFailed.Should().BeTrue();
Sure, I understand that. But "a failure without errors is not a failure" sounds illogical to me. Logic dictates that a failure with no errors cannot exist because it is not a failure at all, since there are no errors in it :)
I think I would throw an exception if you call Result.Fail(..) with an empty list. What do you think? I think its a failure of the consumer that you call the method Fail() but the list of errors is empty.
I think I would throw an exception if you call Result.Fail(..) with an empty list. What do you think? I think its a failure of the consumer that you call the method Fail() but the list of errors is empty.
That sounds totally reasonable and I think throwing an exception is the way to go.
Implemented in the next version