Skip to content

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

@A9G-Data-Droid

Description

@A9G-Data-Droid

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions