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

Fix warning CA1062#FallbackPolicy #2232

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/Polly/Fallback/FallbackPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ namespace Polly.Fallback;
/// <summary>
/// A fallback policy that can be applied to delegates.
/// </summary>
#pragma warning disable CA1062 // Validate arguments of public methods
public class FallbackPolicy : Policy, IFallbackPolicy
{
private readonly Action<Exception, Context> _onFallback;
Expand Down Expand Up @@ -69,13 +68,20 @@ internal FallbackPolicy(

/// <inheritdoc/>
[DebuggerStepThrough]
protected override TResult Implementation(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken) =>
FallbackEngine.Implementation(
protected override TResult Implementation(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken)
{
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}

return FallbackEngine.Implementation(
action,
context,
ExceptionPredicates,
ResultPredicates,
_onFallback,
_fallbackAction,
cancellationToken);
}
}
27 changes: 27 additions & 0 deletions test/Polly.Specs/Fallback/FallbackTResultSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,33 @@ public class FallbackTResultSpecs
{
#region Configuration guard condition tests

[Fact]
public void Should_throw_when_action_is_null()
{
var flags = BindingFlags.NonPublic | BindingFlags.Instance;
Func<Context, CancellationToken, EmptyStruct> action = null!;
PolicyBuilder<EmptyStruct> policyBuilder = new PolicyBuilder<EmptyStruct>(exception => exception);
Action<DelegateResult<EmptyStruct>, Context> onFallback = (_, _) => { };
Func<DelegateResult<EmptyStruct>, Context, CancellationToken, EmptyStruct> fallbackAction = (_, _, _) => EmptyStruct.Instance;

var instance = Activator.CreateInstance(
typeof(FallbackPolicy<EmptyStruct>),
flags,
null,
[policyBuilder, onFallback, fallbackAction],
null)!;
var instanceType = instance.GetType();
var methods = instanceType.GetMethods(flags);
var methodInfo = methods.First(method => method is { Name: "Implementation", ReturnType.Name: "EmptyStruct" });

var func = () => methodInfo.Invoke(instance, [action, new Context(), CancellationToken.None]);

var exceptionAssertions = func.Should().Throw<TargetInvocationException>();
exceptionAssertions.And.Message.Should().Be("Exception has been thrown by the target of an invocation.");
exceptionAssertions.And.InnerException.Should().BeOfType<ArgumentNullException>()
.Which.ParamName.Should().Be("action");
}

[Fact]
public void Should_throw_when_fallback_action_is_null()
{
Expand Down