From c572c4c1ae22bc7e955fabaa60722c531e4317a0 Mon Sep 17 00:00:00 2001 From: ZLoo Date: Tue, 23 Jul 2024 10:35:39 +0300 Subject: [PATCH] Fix CA1062 warnings (#2231) Fix CA1062 warnings for `IAsyncPolicyPolicyWrapExtensions`. --- .../Wrap/IAsyncPolicyPolicyWrapExtensions.cs | 23 +++++++++++++++---- test/Polly.Specs/Caching/CacheAsyncSpecs.cs | 12 ++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/Polly/Wrap/IAsyncPolicyPolicyWrapExtensions.cs b/src/Polly/Wrap/IAsyncPolicyPolicyWrapExtensions.cs index 74a113e9cde..8700a14b6aa 100644 --- a/src/Polly/Wrap/IAsyncPolicyPolicyWrapExtensions.cs +++ b/src/Polly/Wrap/IAsyncPolicyPolicyWrapExtensions.cs @@ -3,7 +3,6 @@ /// /// Defines extensions for configuring instances on an or . /// -#pragma warning disable CA1062 // Validate arguments of public methods public static class IAsyncPolicyPolicyWrapExtensions { /// @@ -159,13 +158,20 @@ public partial class Policy /// The policies to place in the wrap, outermost (at left) to innermost (at right). /// The PolicyWrap. /// The enumerable of policies to form the wrap must contain at least two policies. - 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())), }; + } /// /// Creates a of the given policies governing delegates returning values of type . @@ -174,11 +180,18 @@ public static AsyncPolicyWrap WrapAsync(params IAsyncPolicy[] policies) => /// The return type of delegates which may be executed through the policy. /// The PolicyWrap. /// The enumerable of policies to form the wrap must contain at least two policies. - 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())), }; + } } diff --git a/test/Polly.Specs/Caching/CacheAsyncSpecs.cs b/test/Polly.Specs/Caching/CacheAsyncSpecs.cs index 3e9163bfeb7..27cf84dc8e5 100644 --- a/test/Polly.Specs/Caching/CacheAsyncSpecs.cs +++ b/test/Polly.Specs/Caching/CacheAsyncSpecs.cs @@ -302,6 +302,18 @@ public void Should_throw_when_on_cache_put_is_null() action.Should().Throw().And.ParamName.Should().Be(OnCachePutExpected); } + [Fact] + public void Should_throw_when_policies_is_null() + { + IAsyncPolicy[] policies = null!; + IAsyncPolicy[] policiesGeneric = null!; + + Action action = () => Policy.WrapAsync(policies); + action.Should().Throw().And.ParamName.Should().Be("policies"); + + action = () => Policy.WrapAsync(policiesGeneric); + action.Should().Throw().And.ParamName.Should().Be("policies"); + } #endregion #region Caching behaviours