FluentResults icon indicating copy to clipboard operation
FluentResults copied to clipboard

Result created using Result.Fail with empty error list becomes a success

Open sandord opened this issue 1 year ago • 2 comments

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?

sandord avatar Dec 16 '24 20:12 sandord

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();

Feijo avatar Dec 19 '24 09:12 Feijo

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 :)

sandord avatar Dec 19 '24 14:12 sandord

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.

altmann avatar Jun 28 '25 14:06 altmann

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.

sandord avatar Jun 28 '25 14:06 sandord

Implemented in the next version

altmann avatar Jun 28 '25 19:06 altmann