Skip to content

Commit

Permalink
Fix CA1062 warnings (#2231)
Browse files Browse the repository at this point in the history
Fix CA1062 warnings for `IAsyncPolicyPolicyWrapExtensions`.
  • Loading branch information
Zombach authored Jul 23, 2024
1 parent 05e86fe commit c572c4c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/Polly/Wrap/IAsyncPolicyPolicyWrapExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
/// <summary>
/// Defines extensions for configuring <see cref="PolicyWrap"/> instances on an <see cref="IAsyncPolicy"/> or <see cref="IAsyncPolicy{TResult}"/>.
/// </summary>
#pragma warning disable CA1062 // Validate arguments of public methods
public static class IAsyncPolicyPolicyWrapExtensions
{
/// <summary>
Expand Down Expand Up @@ -159,13 +158,20 @@ public partial class Policy
/// <param name="policies">The policies to place in the wrap, outermost (at left) to innermost (at right).</param>
/// <returns>The PolicyWrap.</returns>
/// <exception cref="ArgumentException">The enumerable of policies to form the wrap must contain at least two policies.</exception>
public static AsyncPolicyWrap WrapAsync(params IAsyncPolicy[] policies) =>
policies.Length switch
public static AsyncPolicyWrap WrapAsync(params IAsyncPolicy[] policies)
{
if (policies is null)
{
throw new ArgumentNullException(nameof(policies));
}

return policies.Length switch
{
< MinimumPoliciesRequiredForWrap => throw new ArgumentException("The enumerable of policies to form the wrap must contain at least two policies.", nameof(policies)),
MinimumPoliciesRequiredForWrap => new AsyncPolicyWrap((AsyncPolicy)policies[0], policies[1]),
_ => WrapAsync(policies[0], WrapAsync(policies.Skip(1).ToArray())),
};
}

/// <summary>
/// Creates a <see cref="PolicyWrap" /> of the given policies governing delegates returning values of type <typeparamref name="TResult" />.
Expand All @@ -174,11 +180,18 @@ public static AsyncPolicyWrap WrapAsync(params IAsyncPolicy[] policies) =>
/// <typeparam name="TResult">The return type of delegates which may be executed through the policy.</typeparam>
/// <returns>The PolicyWrap.</returns>
/// <exception cref="ArgumentException">The enumerable of policies to form the wrap must contain at least two policies.</exception>
public static AsyncPolicyWrap<TResult> WrapAsync<TResult>(params IAsyncPolicy<TResult>[] policies) =>
policies.Length switch
public static AsyncPolicyWrap<TResult> WrapAsync<TResult>(params IAsyncPolicy<TResult>[] policies)
{
if (policies is null)
{
throw new ArgumentNullException(nameof(policies));
}

return policies.Length switch
{
< MinimumPoliciesRequiredForWrap => throw new ArgumentException("The enumerable of policies to form the wrap must contain at least two policies.", nameof(policies)),
MinimumPoliciesRequiredForWrap => new AsyncPolicyWrap<TResult>((AsyncPolicy<TResult>)policies[0], policies[1]),
_ => WrapAsync(policies[0], WrapAsync(policies.Skip(1).ToArray())),
};
}
}
12 changes: 12 additions & 0 deletions test/Polly.Specs/Caching/CacheAsyncSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,18 @@ public void Should_throw_when_on_cache_put_is_null()
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCachePutExpected);
}

[Fact]
public void Should_throw_when_policies_is_null()
{
IAsyncPolicy[] policies = null!;
IAsyncPolicy<int>[] policiesGeneric = null!;

Action action = () => Policy.WrapAsync(policies);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be("policies");

action = () => Policy.WrapAsync<int>(policiesGeneric);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be("policies");
}
#endregion

#region Caching behaviours
Expand Down

0 comments on commit c572c4c

Please sign in to comment.