Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HasError<T> can throw, when I would expect false #204

Open
A9G-Data-Droid opened this issue Feb 16, 2024 · 0 comments
Open

HasError<T> can throw, when I would expect false #204

A9G-Data-Droid opened this issue Feb 16, 2024 · 0 comments

Comments

@A9G-Data-Droid
Copy link

ISSUE:
When checking for the existence of a custom IError I sometimes get an exception that does not clearly point to the problem:

System.ArgumentNullException: Value cannot be null. (Parameter 'source')
at System.Linq.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)
at System.Linq.Enumerable.OfType[TResult](IEnumerable source)
at FluentResults.ResultHelper.HasError[TError](List1 errors, Func2 predicate, IEnumerable1& result) at FluentResults.ResultHelper.HasError[TError](List1 errors, Func2 predicate, IEnumerable1& result)
at FluentResults.ResultBase.HasError[TError](Func2 predicate, IEnumerable1& result)
at FluentResults.ResultBase.HasError[TError](IEnumerable`1& result)
at FluentResults.ResultBase.HasErrorTError

EXPECTED BEHAVIOR:
I would expect HasError<T> to return false instead of throwing.

CAUSE:
In my custom error I did not initialize Reasons in some cases.

SOLUTION:
I would recommend turning on <nullable>enable</nullable> to help flag null problems across the code base.

The problem seems to be in [public static bool HasError(](

var foundErrors = errors.OfType<TError>().Where(predicate).ToList();
)

where predicate is null when looking at a null set of Reasons.

Adding

if(predicate is null) return false

is an example fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant