-
-
Notifications
You must be signed in to change notification settings - Fork 126
Description
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, Func
2 predicate, IEnumerable1& result) at FluentResults.ResultHelper.HasError[TError](List
1 errors, Func2 predicate, IEnumerable
1& result)
at FluentResults.ResultBase.HasError[TError](Func2 predicate, IEnumerable
1& 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.