Skip to content

Commit c572c4c

Browse files
authored
Fix CA1062 warnings (#2231)
Fix CA1062 warnings for `IAsyncPolicyPolicyWrapExtensions`.
1 parent 05e86fe commit c572c4c

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

src/Polly/Wrap/IAsyncPolicyPolicyWrapExtensions.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
/// <summary>
44
/// Defines extensions for configuring <see cref="PolicyWrap"/> instances on an <see cref="IAsyncPolicy"/> or <see cref="IAsyncPolicy{TResult}"/>.
55
/// </summary>
6-
#pragma warning disable CA1062 // Validate arguments of public methods
76
public static class IAsyncPolicyPolicyWrapExtensions
87
{
98
/// <summary>
@@ -159,13 +158,20 @@ public partial class Policy
159158
/// <param name="policies">The policies to place in the wrap, outermost (at left) to innermost (at right).</param>
160159
/// <returns>The PolicyWrap.</returns>
161160
/// <exception cref="ArgumentException">The enumerable of policies to form the wrap must contain at least two policies.</exception>
162-
public static AsyncPolicyWrap WrapAsync(params IAsyncPolicy[] policies) =>
163-
policies.Length switch
161+
public static AsyncPolicyWrap WrapAsync(params IAsyncPolicy[] policies)
162+
{
163+
if (policies is null)
164+
{
165+
throw new ArgumentNullException(nameof(policies));
166+
}
167+
168+
return policies.Length switch
164169
{
165170
< MinimumPoliciesRequiredForWrap => throw new ArgumentException("The enumerable of policies to form the wrap must contain at least two policies.", nameof(policies)),
166171
MinimumPoliciesRequiredForWrap => new AsyncPolicyWrap((AsyncPolicy)policies[0], policies[1]),
167172
_ => WrapAsync(policies[0], WrapAsync(policies.Skip(1).ToArray())),
168173
};
174+
}
169175

170176
/// <summary>
171177
/// Creates a <see cref="PolicyWrap" /> of the given policies governing delegates returning values of type <typeparamref name="TResult" />.
@@ -174,11 +180,18 @@ public static AsyncPolicyWrap WrapAsync(params IAsyncPolicy[] policies) =>
174180
/// <typeparam name="TResult">The return type of delegates which may be executed through the policy.</typeparam>
175181
/// <returns>The PolicyWrap.</returns>
176182
/// <exception cref="ArgumentException">The enumerable of policies to form the wrap must contain at least two policies.</exception>
177-
public static AsyncPolicyWrap<TResult> WrapAsync<TResult>(params IAsyncPolicy<TResult>[] policies) =>
178-
policies.Length switch
183+
public static AsyncPolicyWrap<TResult> WrapAsync<TResult>(params IAsyncPolicy<TResult>[] policies)
184+
{
185+
if (policies is null)
186+
{
187+
throw new ArgumentNullException(nameof(policies));
188+
}
189+
190+
return policies.Length switch
179191
{
180192
< MinimumPoliciesRequiredForWrap => throw new ArgumentException("The enumerable of policies to form the wrap must contain at least two policies.", nameof(policies)),
181193
MinimumPoliciesRequiredForWrap => new AsyncPolicyWrap<TResult>((AsyncPolicy<TResult>)policies[0], policies[1]),
182194
_ => WrapAsync(policies[0], WrapAsync(policies.Skip(1).ToArray())),
183195
};
196+
}
184197
}

test/Polly.Specs/Caching/CacheAsyncSpecs.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,18 @@ public void Should_throw_when_on_cache_put_is_null()
302302
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCachePutExpected);
303303
}
304304

305+
[Fact]
306+
public void Should_throw_when_policies_is_null()
307+
{
308+
IAsyncPolicy[] policies = null!;
309+
IAsyncPolicy<int>[] policiesGeneric = null!;
310+
311+
Action action = () => Policy.WrapAsync(policies);
312+
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be("policies");
313+
314+
action = () => Policy.WrapAsync<int>(policiesGeneric);
315+
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be("policies");
316+
}
305317
#endregion
306318

307319
#region Caching behaviours

0 commit comments

Comments
 (0)