From f054ef7fb02bc149d52469028d34392c70598f78 Mon Sep 17 00:00:00 2001 From: ZLoo Date: Tue, 23 Jul 2024 10:01:28 +0300 Subject: [PATCH] Fix CA1062 warnings (#2230) Fix CA1062 warnings for `ISyncPolicyPolicyWrapExtensions`. --- .../Wrap/ISyncPolicyPolicyWrapExtensions.cs | 29 ++++++++++++++----- test/Polly.Specs/Caching/CacheSpecs.cs | 13 +++++++++ 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/Polly/Wrap/ISyncPolicyPolicyWrapExtensions.cs b/src/Polly/Wrap/ISyncPolicyPolicyWrapExtensions.cs index be1e25ebeab..a88c518b19e 100644 --- a/src/Polly/Wrap/ISyncPolicyPolicyWrapExtensions.cs +++ b/src/Polly/Wrap/ISyncPolicyPolicyWrapExtensions.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 ISyncPolicyPolicyWrapExtensions { /// @@ -157,13 +156,21 @@ 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 PolicyWrap Wrap(params ISyncPolicy[] policies) => - policies.Length switch + public static PolicyWrap Wrap(params ISyncPolicy[] policies) + { + if (policies is null) { - < MinimumPoliciesRequiredForWrap => throw new ArgumentException("The enumerable of policies to form the wrap must contain at least two policies.", nameof(policies)), + 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 PolicyWrap((Policy)policies[0], policies[1]), _ => Wrap(policies[0], Wrap(policies.Skip(1).ToArray())), }; + } /// /// Creates a of the given policies governing delegates returning values of type . @@ -172,11 +179,19 @@ public static PolicyWrap Wrap(params ISyncPolicy[] 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 PolicyWrap Wrap(params ISyncPolicy[] policies) => - policies.Length switch + public static PolicyWrap Wrap(params ISyncPolicy[] policies) + { + if (policies is null) { - < MinimumPoliciesRequiredForWrap => throw new ArgumentException("The enumerable of policies to form the wrap must contain at least two policies.", nameof(policies)), + 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 PolicyWrap((Policy)policies[0], policies[1]), _ => Wrap(policies[0], Wrap(policies.Skip(1).ToArray())), }; + } } diff --git a/test/Polly.Specs/Caching/CacheSpecs.cs b/test/Polly.Specs/Caching/CacheSpecs.cs index e1e06fe6cf4..87d763508d8 100644 --- a/test/Polly.Specs/Caching/CacheSpecs.cs +++ b/test/Polly.Specs/Caching/CacheSpecs.cs @@ -31,6 +31,19 @@ public void Should_throw_when_cache_key_strategy_is_null() action.Should().Throw().And.ParamName.Should().Be("cacheKeyStrategy"); } + [Fact] + public void Should_throw_when_policies_is_null() + { + ISyncPolicy[] policies = null!; + ISyncPolicy[] policiesGeneric = null!; + + Action action = () => Policy.Wrap(policies); + action.Should().Throw().And.ParamName.Should().Be("policies"); + + action = () => Policy.Wrap(policiesGeneric); + action.Should().Throw().And.ParamName.Should().Be("policies"); + } + #endregion #region Caching behaviours