diff --git a/src/Polly/AsyncPolicy.ContextAndKeys.cs b/src/Polly/AsyncPolicy.ContextAndKeys.cs index ec0e813c14..7ba44c96a7 100644 --- a/src/Polly/AsyncPolicy.ContextAndKeys.cs +++ b/src/Polly/AsyncPolicy.ContextAndKeys.cs @@ -10,7 +10,9 @@ public abstract partial class AsyncPolicy public AsyncPolicy WithPolicyKey(string policyKey) { if (policyKeyInternal != null) + { throw PolicyKeyMustBeImmutableException(nameof(policyKey)); + } policyKeyInternal = policyKey; return this; @@ -24,7 +26,9 @@ public AsyncPolicy WithPolicyKey(string policyKey) IAsyncPolicy IAsyncPolicy.WithPolicyKey(string policyKey) { if (policyKeyInternal != null) + { throw PolicyKeyMustBeImmutableException(nameof(policyKey)); + } policyKeyInternal = policyKey; return this; @@ -41,7 +45,9 @@ public abstract partial class AsyncPolicy public AsyncPolicy WithPolicyKey(string policyKey) { if (policyKeyInternal != null) + { throw PolicyKeyMustBeImmutableException(nameof(policyKey)); + } policyKeyInternal = policyKey; return this; @@ -55,7 +61,9 @@ public AsyncPolicy WithPolicyKey(string policyKey) IAsyncPolicy IAsyncPolicy.WithPolicyKey(string policyKey) { if (policyKeyInternal != null) + { throw PolicyKeyMustBeImmutableException(nameof(policyKey)); + } policyKeyInternal = policyKey; return this; diff --git a/src/Polly/AsyncPolicy.ExecuteOverloads.cs b/src/Polly/AsyncPolicy.ExecuteOverloads.cs index 661cfadf64..af804d5d39 100644 --- a/src/Polly/AsyncPolicy.ExecuteOverloads.cs +++ b/src/Polly/AsyncPolicy.ExecuteOverloads.cs @@ -94,7 +94,9 @@ public abstract partial class AsyncPolicy : PolicyBase, IAsyncPolicy public async Task ExecuteAsync(Func action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) { if (context == null) + { throw new ArgumentNullException(nameof(context)); + } SetPolicyContext(context, out string priorPolicyWrapKey, out string priorPolicyKey); @@ -218,7 +220,9 @@ public async Task ExecuteAsync(Func action, Co public async Task ExecuteAsync(Func> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) { if (context == null) + { throw new ArgumentNullException(nameof(context)); + } SetPolicyContext(context, out string priorPolicyWrapKey, out string priorPolicyKey); @@ -335,7 +339,9 @@ public async Task ExecuteAsync(Func ExecuteAndCaptureAsync(Func action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) { if (context == null) + { throw new ArgumentNullException(nameof(context)); + } try { @@ -460,7 +466,9 @@ public async Task ExecuteAndCaptureAsync(Func> ExecuteAndCaptureAsync(Func> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) { if (context == null) + { throw new ArgumentNullException(nameof(context)); + } try { diff --git a/src/Polly/AsyncPolicy.TResult.ExecuteOverloads.cs b/src/Polly/AsyncPolicy.TResult.ExecuteOverloads.cs index 74a178f394..8e704d5b2e 100644 --- a/src/Polly/AsyncPolicy.TResult.ExecuteOverloads.cs +++ b/src/Polly/AsyncPolicy.TResult.ExecuteOverloads.cs @@ -103,7 +103,9 @@ public abstract partial class AsyncPolicy : IAsyncPolicy public async Task ExecuteAsync(Func> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) { if (context == null) + { throw new ArgumentNullException(nameof(context)); + } SetPolicyContext(context, out string priorPolicyWrapKey, out string priorPolicyKey); @@ -222,7 +224,9 @@ public async Task ExecuteAsync(Func> ExecuteAndCaptureAsync(Func> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) { if (context == null) + { throw new ArgumentNullException(nameof(context)); + } try { diff --git a/src/Polly/Bulkhead/AsyncBulkheadSyntax.cs b/src/Polly/Bulkhead/AsyncBulkheadSyntax.cs index 80cfc1459a..f31052e169 100644 --- a/src/Polly/Bulkhead/AsyncBulkheadSyntax.cs +++ b/src/Polly/Bulkhead/AsyncBulkheadSyntax.cs @@ -60,11 +60,19 @@ public static AsyncBulkheadPolicy BulkheadAsync(int maxParallelization, int maxQ Func onBulkheadRejectedAsync) { if (maxParallelization <= 0) + { throw new ArgumentOutOfRangeException(nameof(maxParallelization), "Value must be greater than zero."); + } + if (maxQueuingActions < 0) + { throw new ArgumentOutOfRangeException(nameof(maxQueuingActions), "Value must be greater than or equal to zero."); + } + if (onBulkheadRejectedAsync == null) + { throw new ArgumentNullException(nameof(onBulkheadRejectedAsync)); + } return new AsyncBulkheadPolicy( maxParallelization, diff --git a/src/Polly/Bulkhead/AsyncBulkheadTResultSyntax.cs b/src/Polly/Bulkhead/AsyncBulkheadTResultSyntax.cs index ee3f43ccfb..a174fda319 100644 --- a/src/Polly/Bulkhead/AsyncBulkheadTResultSyntax.cs +++ b/src/Polly/Bulkhead/AsyncBulkheadTResultSyntax.cs @@ -60,11 +60,19 @@ public static AsyncBulkheadPolicy BulkheadAsync(int maxParalle public static AsyncBulkheadPolicy BulkheadAsync(int maxParallelization, int maxQueuingActions, Func onBulkheadRejectedAsync) { if (maxParallelization <= 0) + { throw new ArgumentOutOfRangeException(nameof(maxParallelization), "Value must be greater than zero."); + } + if (maxQueuingActions < 0) + { throw new ArgumentOutOfRangeException(nameof(maxQueuingActions), "Value must be greater than or equal to zero."); + } + if (onBulkheadRejectedAsync == null) + { throw new ArgumentNullException(nameof(onBulkheadRejectedAsync)); + } return new AsyncBulkheadPolicy( maxParallelization, diff --git a/src/Polly/Bulkhead/BulkheadSyntax.cs b/src/Polly/Bulkhead/BulkheadSyntax.cs index c841d9d8ba..7b1b751547 100644 --- a/src/Polly/Bulkhead/BulkheadSyntax.cs +++ b/src/Polly/Bulkhead/BulkheadSyntax.cs @@ -57,11 +57,19 @@ public static BulkheadPolicy Bulkhead(int maxParallelization, int maxQueuingActi public static BulkheadPolicy Bulkhead(int maxParallelization, int maxQueuingActions, Action onBulkheadRejected) { if (maxParallelization <= 0) + { throw new ArgumentOutOfRangeException(nameof(maxParallelization), "Value must be greater than zero."); + } + if (maxQueuingActions < 0) + { throw new ArgumentOutOfRangeException(nameof(maxQueuingActions), "Value must be greater than or equal to zero."); + } + if (onBulkheadRejected == null) + { throw new ArgumentNullException(nameof(onBulkheadRejected)); + } return new BulkheadPolicy( maxParallelization, diff --git a/src/Polly/Bulkhead/BulkheadTResultSyntax.cs b/src/Polly/Bulkhead/BulkheadTResultSyntax.cs index 19f7efe267..c3661c1562 100644 --- a/src/Polly/Bulkhead/BulkheadTResultSyntax.cs +++ b/src/Polly/Bulkhead/BulkheadTResultSyntax.cs @@ -61,11 +61,19 @@ public static BulkheadPolicy Bulkhead(int maxParallelization, public static BulkheadPolicy Bulkhead(int maxParallelization, int maxQueuingActions, Action onBulkheadRejected) { if (maxParallelization <= 0) + { throw new ArgumentOutOfRangeException(nameof(maxParallelization), "Value must be greater than zero."); + } + if (maxQueuingActions < 0) + { throw new ArgumentOutOfRangeException(nameof(maxQueuingActions), "Value must be greater than or equal to zero."); + } + if (onBulkheadRejected == null) + { throw new ArgumentNullException(nameof(onBulkheadRejected)); + } return new BulkheadPolicy( maxParallelization, diff --git a/src/Polly/Caching/AsyncCacheSyntax.cs b/src/Polly/Caching/AsyncCacheSyntax.cs index 9fa5db4510..9da9b5a27c 100644 --- a/src/Polly/Caching/AsyncCacheSyntax.cs +++ b/src/Polly/Caching/AsyncCacheSyntax.cs @@ -65,11 +65,19 @@ public partial class Policy public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action? onCacheError = null) { if (cacheProvider == null) + { throw new ArgumentNullException(nameof(cacheProvider)); + } + if (ttlStrategy == null) + { throw new ArgumentNullException(nameof(ttlStrategy)); + } + if (cacheKeyStrategy == null) + { throw new ArgumentNullException(nameof(cacheKeyStrategy)); + } Action emptyDelegate = (_, _) => { }; @@ -109,11 +117,19 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, ITt public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Func cacheKeyStrategy, Action? onCacheError = null) { if (cacheProvider == null) + { throw new ArgumentNullException(nameof(cacheProvider)); + } + if (ttlStrategy == null) + { throw new ArgumentNullException(nameof(ttlStrategy)); + } + if (cacheKeyStrategy == null) + { throw new ArgumentNullException(nameof(cacheKeyStrategy)); + } Action emptyDelegate = (_, _) => { }; @@ -303,18 +319,34 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, ITt Action? onCachePutError) { if (cacheProvider == null) + { throw new ArgumentNullException(nameof(cacheProvider)); + } + if (ttlStrategy == null) + { throw new ArgumentNullException(nameof(ttlStrategy)); + } + if (cacheKeyStrategy == null) + { throw new ArgumentNullException(nameof(cacheKeyStrategy)); + } if (onCacheGet == null) + { throw new ArgumentNullException(nameof(onCacheGet)); + } + if (onCacheMiss == null) + { throw new ArgumentNullException(nameof(onCacheMiss)); + } + if (onCachePut == null) + { throw new ArgumentNullException(nameof(onCachePut)); + } return new AsyncCachePolicy(cacheProvider, ttlStrategy, cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } diff --git a/src/Polly/Caching/AsyncCacheTResultSyntax.cs b/src/Polly/Caching/AsyncCacheTResultSyntax.cs index 3a4daa6d41..966e69b05a 100644 --- a/src/Polly/Caching/AsyncCacheTResultSyntax.cs +++ b/src/Polly/Caching/AsyncCacheTResultSyntax.cs @@ -18,7 +18,9 @@ public partial class Policy public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, TimeSpan ttl, Action? onCacheError = null) { if (cacheProvider == null) + { throw new ArgumentNullException(nameof(cacheProvider)); + } return CacheAsync(cacheProvider.AsyncFor(), new RelativeTtl(ttl), DefaultCacheKeyStrategy.Instance.GetCacheKey, onCacheError); } @@ -39,7 +41,9 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Action? onCacheError = null) { if (cacheProvider == null) + { throw new ArgumentNullException(nameof(cacheProvider)); + } return CacheAsync(cacheProvider.AsyncFor(), ttlStrategy, DefaultCacheKeyStrategy.Instance.GetCacheKey, onCacheError); } @@ -61,7 +65,9 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, TimeSpan ttl, ICacheKeyStrategy cacheKeyStrategy, Action? onCacheError = null) { if (cacheProvider == null) + { throw new ArgumentNullException(nameof(cacheProvider)); + } return CacheAsync(cacheProvider.AsyncFor(), new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheError); } @@ -84,7 +90,9 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action? onCacheError = null) { if (cacheProvider == null) + { throw new ArgumentNullException(nameof(cacheProvider)); + } return CacheAsync(cacheProvider.AsyncFor(), ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheError); } @@ -106,7 +114,9 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, TimeSpan ttl, Func cacheKeyStrategy, Action? onCacheError = null) { if (cacheProvider == null) + { throw new ArgumentNullException(nameof(cacheProvider)); + } return CacheAsync(cacheProvider.AsyncFor(), new RelativeTtl(ttl), cacheKeyStrategy, onCacheError); } @@ -129,7 +139,9 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Func cacheKeyStrategy, Action? onCacheError = null) { if (cacheProvider == null) + { throw new ArgumentNullException(nameof(cacheProvider)); + } return CacheAsync(cacheProvider.AsyncFor(), ttlStrategy, cacheKeyStrategy, onCacheError); } @@ -163,7 +175,9 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider Action? onCachePutError) { if (cacheProvider == null) + { throw new ArgumentNullException(nameof(cacheProvider)); + } return CacheAsync(cacheProvider.AsyncFor(), new RelativeTtl(ttl), DefaultCacheKeyStrategy.Instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } @@ -198,7 +212,9 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider Action? onCachePutError) { if (cacheProvider == null) + { throw new ArgumentNullException(nameof(cacheProvider)); + } return CacheAsync(cacheProvider.AsyncFor(), ttlStrategy, DefaultCacheKeyStrategy.Instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } @@ -235,7 +251,9 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider Action? onCachePutError) { if (cacheProvider == null) + { throw new ArgumentNullException(nameof(cacheProvider)); + } return CacheAsync(cacheProvider.AsyncFor(), new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } @@ -273,7 +291,9 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider Action? onCachePutError) { if (cacheProvider == null) + { throw new ArgumentNullException(nameof(cacheProvider)); + } return CacheAsync(cacheProvider.AsyncFor(), ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } @@ -310,7 +330,9 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider Action? onCachePutError) { if (cacheProvider == null) + { throw new ArgumentNullException(nameof(cacheProvider)); + } return CacheAsync(cacheProvider.AsyncFor(), new RelativeTtl(ttl), cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } @@ -348,7 +370,9 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider Action? onCachePutError) { if (cacheProvider == null) + { throw new ArgumentNullException(nameof(cacheProvider)); + } return CacheAsync(cacheProvider.AsyncFor(), ttlStrategy, cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } @@ -810,18 +834,34 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider< Action? onCachePutError) { if (cacheProvider == null) + { throw new ArgumentNullException(nameof(cacheProvider)); + } + if (ttlStrategy == null) + { throw new ArgumentNullException(nameof(ttlStrategy)); + } + if (cacheKeyStrategy == null) + { throw new ArgumentNullException(nameof(cacheKeyStrategy)); + } if (onCacheGet == null) + { throw new ArgumentNullException(nameof(onCacheGet)); + } + if (onCacheMiss == null) + { throw new ArgumentNullException(nameof(onCacheMiss)); + } + if (onCachePut == null) + { throw new ArgumentNullException(nameof(onCachePut)); + } return new AsyncCachePolicy(cacheProvider, ttlStrategy, cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } diff --git a/src/Polly/Caching/CacheSyntax.cs b/src/Polly/Caching/CacheSyntax.cs index 350dd3b111..52fddb54f1 100644 --- a/src/Polly/Caching/CacheSyntax.cs +++ b/src/Polly/Caching/CacheSyntax.cs @@ -65,11 +65,19 @@ public partial class Policy public static CachePolicy Cache(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action? onCacheError = null) { if (cacheProvider == null) + { throw new ArgumentNullException(nameof(cacheProvider)); + } + if (ttlStrategy == null) + { throw new ArgumentNullException(nameof(ttlStrategy)); + } + if (cacheKeyStrategy == null) + { throw new ArgumentNullException(nameof(cacheKeyStrategy)); + } Action emptyDelegate = (_, _) => { }; @@ -109,11 +117,19 @@ public static CachePolicy Cache(ISyncCacheProvider cacheProvider, ITtlStrategy t public static CachePolicy Cache(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Func cacheKeyStrategy, Action? onCacheError = null) { if (cacheProvider == null) + { throw new ArgumentNullException(nameof(cacheProvider)); + } + if (ttlStrategy == null) + { throw new ArgumentNullException(nameof(ttlStrategy)); + } + if (cacheKeyStrategy == null) + { throw new ArgumentNullException(nameof(cacheKeyStrategy)); + } Action emptyDelegate = (_, _) => { }; @@ -303,18 +319,34 @@ public static CachePolicy Cache(ISyncCacheProvider cacheProvider, ITtlStrategy t Action? onCachePutError) { if (cacheProvider == null) + { throw new ArgumentNullException(nameof(cacheProvider)); + } + if (ttlStrategy == null) + { throw new ArgumentNullException(nameof(ttlStrategy)); + } + if (cacheKeyStrategy == null) + { throw new ArgumentNullException(nameof(cacheKeyStrategy)); + } if (onCacheGet == null) + { throw new ArgumentNullException(nameof(onCacheGet)); + } + if (onCacheMiss == null) + { throw new ArgumentNullException(nameof(onCacheMiss)); + } + if (onCachePut == null) + { throw new ArgumentNullException(nameof(onCachePut)); + } return new CachePolicy(cacheProvider, ttlStrategy, cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } diff --git a/src/Polly/Caching/CacheTResultSyntax.cs b/src/Polly/Caching/CacheTResultSyntax.cs index 4f7c671226..cdee22416e 100644 --- a/src/Polly/Caching/CacheTResultSyntax.cs +++ b/src/Polly/Caching/CacheTResultSyntax.cs @@ -18,7 +18,9 @@ public partial class Policy public static CachePolicy Cache(ISyncCacheProvider cacheProvider, TimeSpan ttl, Action? onCacheError = null) { if (cacheProvider == null) + { throw new ArgumentNullException(nameof(cacheProvider)); + } return Cache(cacheProvider.For(), new RelativeTtl(ttl), DefaultCacheKeyStrategy.Instance.GetCacheKey, onCacheError); } @@ -39,7 +41,9 @@ public static CachePolicy Cache(ISyncCacheProvider cacheProvid public static CachePolicy Cache(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Action? onCacheError = null) { if (cacheProvider == null) + { throw new ArgumentNullException(nameof(cacheProvider)); + } return Cache(cacheProvider.For(), ttlStrategy, DefaultCacheKeyStrategy.Instance.GetCacheKey, onCacheError); } @@ -61,7 +65,9 @@ public static CachePolicy Cache(ISyncCacheProvider cacheProvid public static CachePolicy Cache(ISyncCacheProvider cacheProvider, TimeSpan ttl, ICacheKeyStrategy cacheKeyStrategy, Action? onCacheError = null) { if (cacheProvider == null) + { throw new ArgumentNullException(nameof(cacheProvider)); + } return Cache(cacheProvider.For(), new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheError); } @@ -84,7 +90,9 @@ public static CachePolicy Cache(ISyncCacheProvider cacheProvid public static CachePolicy Cache(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action? onCacheError = null) { if (cacheProvider == null) + { throw new ArgumentNullException(nameof(cacheProvider)); + } return Cache(cacheProvider.For(), ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheError); } @@ -106,7 +114,9 @@ public static CachePolicy Cache(ISyncCacheProvider cacheProvid public static CachePolicy Cache(ISyncCacheProvider cacheProvider, TimeSpan ttl, Func cacheKeyStrategy, Action? onCacheError = null) { if (cacheProvider == null) + { throw new ArgumentNullException(nameof(cacheProvider)); + } return Cache(cacheProvider.For(), new RelativeTtl(ttl), cacheKeyStrategy, onCacheError); } @@ -129,7 +139,9 @@ public static CachePolicy Cache(ISyncCacheProvider cacheProvid public static CachePolicy Cache(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Func cacheKeyStrategy, Action? onCacheError = null) { if (cacheProvider == null) + { throw new ArgumentNullException(nameof(cacheProvider)); + } return Cache(cacheProvider.For(), ttlStrategy, cacheKeyStrategy, onCacheError); } @@ -163,7 +175,9 @@ public static CachePolicy Cache(ISyncCacheProvider cacheProvid Action? onCachePutError) { if (cacheProvider == null) + { throw new ArgumentNullException(nameof(cacheProvider)); + } return Cache(cacheProvider.For(), new RelativeTtl(ttl), DefaultCacheKeyStrategy.Instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } @@ -198,7 +212,9 @@ public static CachePolicy Cache(ISyncCacheProvider cacheProvid Action? onCachePutError) { if (cacheProvider == null) + { throw new ArgumentNullException(nameof(cacheProvider)); + } return Cache(cacheProvider.For(), ttlStrategy, DefaultCacheKeyStrategy.Instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } @@ -235,7 +251,9 @@ public static CachePolicy Cache(ISyncCacheProvider cacheProvid Action? onCachePutError) { if (cacheProvider == null) + { throw new ArgumentNullException(nameof(cacheProvider)); + } return Cache(cacheProvider.For(), new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } @@ -273,7 +291,9 @@ public static CachePolicy Cache(ISyncCacheProvider cacheProvid Action? onCachePutError) { if (cacheProvider == null) + { throw new ArgumentNullException(nameof(cacheProvider)); + } return Cache(cacheProvider.For(), ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } @@ -310,7 +330,9 @@ public static CachePolicy Cache(ISyncCacheProvider cacheProvid Action? onCachePutError) { if (cacheProvider == null) + { throw new ArgumentNullException(nameof(cacheProvider)); + } return Cache(cacheProvider.For(), new RelativeTtl(ttl), cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } @@ -348,7 +370,9 @@ public static CachePolicy Cache(ISyncCacheProvider cacheProvid Action? onCachePutError) { if (cacheProvider == null) + { throw new ArgumentNullException(nameof(cacheProvider)); + } return Cache(cacheProvider.For(), ttlStrategy, cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } @@ -815,18 +839,34 @@ public static CachePolicy Cache(ISyncCacheProvider ca Action? onCachePutError) { if (cacheProvider == null) + { throw new ArgumentNullException(nameof(cacheProvider)); + } + if (ttlStrategy == null) + { throw new ArgumentNullException(nameof(ttlStrategy)); + } + if (cacheKeyStrategy == null) + { throw new ArgumentNullException(nameof(cacheKeyStrategy)); + } if (onCacheGet == null) + { throw new ArgumentNullException(nameof(onCacheGet)); + } + if (onCacheMiss == null) + { throw new ArgumentNullException(nameof(onCacheMiss)); + } + if (onCachePut == null) + { throw new ArgumentNullException(nameof(onCachePut)); + } return new CachePolicy(cacheProvider, ttlStrategy, cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } diff --git a/src/Polly/Caching/ContextualTtl.cs b/src/Polly/Caching/ContextualTtl.cs index c45e23a327..ac14b2f511 100644 --- a/src/Polly/Caching/ContextualTtl.cs +++ b/src/Polly/Caching/ContextualTtl.cs @@ -28,7 +28,9 @@ public class ContextualTtl : ITtlStrategy public Ttl GetTtl(Context context, object? result) { if (!context.ContainsKey(TimeSpanKey)) + { return _noTtl; + } bool sliding = false; diff --git a/src/Polly/Caching/RelativeTtl.cs b/src/Polly/Caching/RelativeTtl.cs index de192d9a8d..4f05440ca0 100644 --- a/src/Polly/Caching/RelativeTtl.cs +++ b/src/Polly/Caching/RelativeTtl.cs @@ -15,7 +15,9 @@ public class RelativeTtl : ITtlStrategy public RelativeTtl(TimeSpan ttl) { if (ttl < TimeSpan.Zero) + { throw new ArgumentOutOfRangeException(nameof(ttl), "The ttl for items to cache must be greater than zero."); + } this.ttl = ttl; } diff --git a/src/Polly/Caching/ResultTtl.cs b/src/Polly/Caching/ResultTtl.cs index ae24f731ea..c3b1038c18 100644 --- a/src/Polly/Caching/ResultTtl.cs +++ b/src/Polly/Caching/ResultTtl.cs @@ -16,7 +16,10 @@ public class ResultTtl : ITtlStrategy public ResultTtl(Func ttlFunc) { if (ttlFunc == null) + { throw new ArgumentNullException(nameof(ttlFunc)); + } + _ttlFunc = (_, result) => ttlFunc(result); } diff --git a/src/Polly/Caching/SlidingTtl.cs b/src/Polly/Caching/SlidingTtl.cs index 3c446dced4..7adfdc0b2e 100644 --- a/src/Polly/Caching/SlidingTtl.cs +++ b/src/Polly/Caching/SlidingTtl.cs @@ -15,7 +15,9 @@ public class SlidingTtl : ITtlStrategy public SlidingTtl(TimeSpan slidingTtl) { if (slidingTtl < TimeSpan.Zero) + { throw new ArgumentOutOfRangeException(nameof(slidingTtl), "The ttl for items to cache must be greater than zero."); + } ttl = new Ttl(slidingTtl, true); } diff --git a/src/Polly/CircuitBreaker/AdvancedCircuitBreakerSyntax.cs b/src/Polly/CircuitBreaker/AdvancedCircuitBreakerSyntax.cs index 2c30010871..2b91aeb1d0 100644 --- a/src/Polly/CircuitBreaker/AdvancedCircuitBreakerSyntax.cs +++ b/src/Polly/CircuitBreaker/AdvancedCircuitBreakerSyntax.cs @@ -224,22 +224,44 @@ public static CircuitBreakerPolicy AdvancedCircuitBreaker(this PolicyBuilder pol var resolutionOfCircuit = TimeSpan.FromTicks(AdvancedCircuitController.ResolutionOfCircuitTimer); if (failureThreshold <= 0) + { throw new ArgumentOutOfRangeException(nameof(failureThreshold), "Value must be greater than zero."); + } + if (failureThreshold > 1) + { throw new ArgumentOutOfRangeException(nameof(failureThreshold), "Value must be less than or equal to one."); + } + if (samplingDuration < resolutionOfCircuit) + { throw new ArgumentOutOfRangeException(nameof(samplingDuration), $"Value must be equal to or greater than {resolutionOfCircuit.TotalMilliseconds} milliseconds. This is the minimum resolution of the CircuitBreaker timer."); + } + if (minimumThroughput <= 1) + { throw new ArgumentOutOfRangeException(nameof(minimumThroughput), "Value must be greater than one."); + } + if (durationOfBreak < TimeSpan.Zero) + { throw new ArgumentOutOfRangeException(nameof(durationOfBreak), "Value must be greater than zero."); + } if (onBreak == null) + { throw new ArgumentNullException(nameof(onBreak)); + } + if (onReset == null) + { throw new ArgumentNullException(nameof(onReset)); + } + if (onHalfOpen == null) + { throw new ArgumentNullException(nameof(onHalfOpen)); + } var breakerController = new AdvancedCircuitController( failureThreshold, diff --git a/src/Polly/CircuitBreaker/AdvancedCircuitBreakerTResultSyntax.cs b/src/Polly/CircuitBreaker/AdvancedCircuitBreakerTResultSyntax.cs index bc19455523..5b521b5a0f 100644 --- a/src/Polly/CircuitBreaker/AdvancedCircuitBreakerTResultSyntax.cs +++ b/src/Polly/CircuitBreaker/AdvancedCircuitBreakerTResultSyntax.cs @@ -230,22 +230,44 @@ public static CircuitBreakerPolicy AdvancedCircuitBreaker(this var resolutionOfCircuit = TimeSpan.FromTicks(AdvancedCircuitController.ResolutionOfCircuitTimer); if (failureThreshold <= 0) + { throw new ArgumentOutOfRangeException(nameof(failureThreshold), "Value must be greater than zero."); + } + if (failureThreshold > 1) + { throw new ArgumentOutOfRangeException(nameof(failureThreshold), "Value must be less than or equal to one."); + } + if (samplingDuration < resolutionOfCircuit) + { throw new ArgumentOutOfRangeException(nameof(samplingDuration), $"Value must be equal to or greater than {resolutionOfCircuit.TotalMilliseconds} milliseconds. This is the minimum resolution of the CircuitBreaker timer."); + } + if (minimumThroughput <= 1) + { throw new ArgumentOutOfRangeException(nameof(minimumThroughput), "Value must be greater than one."); + } + if (durationOfBreak < TimeSpan.Zero) + { throw new ArgumentOutOfRangeException(nameof(durationOfBreak), "Value must be greater than zero."); + } if (onBreak == null) + { throw new ArgumentNullException(nameof(onBreak)); + } + if (onReset == null) + { throw new ArgumentNullException(nameof(onReset)); + } + if (onHalfOpen == null) + { throw new ArgumentNullException(nameof(onHalfOpen)); + } var breakerController = new AdvancedCircuitController( failureThreshold, diff --git a/src/Polly/CircuitBreaker/AsyncAdvancedCircuitBreakerSyntax.cs b/src/Polly/CircuitBreaker/AsyncAdvancedCircuitBreakerSyntax.cs index b5d464ac88..2901fcd57e 100644 --- a/src/Polly/CircuitBreaker/AsyncAdvancedCircuitBreakerSyntax.cs +++ b/src/Polly/CircuitBreaker/AsyncAdvancedCircuitBreakerSyntax.cs @@ -227,22 +227,44 @@ public static AsyncCircuitBreakerPolicy AdvancedCircuitBreakerAsync(this PolicyB var resolutionOfCircuit = TimeSpan.FromTicks(AdvancedCircuitController.ResolutionOfCircuitTimer); if (failureThreshold <= 0) + { throw new ArgumentOutOfRangeException(nameof(failureThreshold), "Value must be greater than zero."); + } + if (failureThreshold > 1) + { throw new ArgumentOutOfRangeException(nameof(failureThreshold), "Value must be less than or equal to one."); + } + if (samplingDuration < resolutionOfCircuit) + { throw new ArgumentOutOfRangeException(nameof(samplingDuration), $"Value must be equal to or greater than {resolutionOfCircuit.TotalMilliseconds} milliseconds. This is the minimum resolution of the CircuitBreaker timer."); + } + if (minimumThroughput <= 1) + { throw new ArgumentOutOfRangeException(nameof(minimumThroughput), "Value must be greater than one."); + } + if (durationOfBreak < TimeSpan.Zero) + { throw new ArgumentOutOfRangeException(nameof(durationOfBreak), "Value must be greater than zero."); + } if (onBreak == null) + { throw new ArgumentNullException(nameof(onBreak)); + } + if (onReset == null) + { throw new ArgumentNullException(nameof(onReset)); + } + if (onHalfOpen == null) + { throw new ArgumentNullException(nameof(onHalfOpen)); + } var breakerController = new AdvancedCircuitController( failureThreshold, diff --git a/src/Polly/CircuitBreaker/AsyncAdvancedCircuitBreakerTResultSyntax.cs b/src/Polly/CircuitBreaker/AsyncAdvancedCircuitBreakerTResultSyntax.cs index 67afe0c6ff..7d4b43f6eb 100644 --- a/src/Polly/CircuitBreaker/AsyncAdvancedCircuitBreakerTResultSyntax.cs +++ b/src/Polly/CircuitBreaker/AsyncAdvancedCircuitBreakerTResultSyntax.cs @@ -226,22 +226,44 @@ public static AsyncCircuitBreakerPolicy AdvancedCircuitBreakerAsync.ResolutionOfCircuitTimer); if (failureThreshold <= 0) + { throw new ArgumentOutOfRangeException(nameof(failureThreshold), "Value must be greater than zero."); + } + if (failureThreshold > 1) + { throw new ArgumentOutOfRangeException(nameof(failureThreshold), "Value must be less than or equal to one."); + } + if (samplingDuration < resolutionOfCircuit) + { throw new ArgumentOutOfRangeException(nameof(samplingDuration), $"Value must be equal to or greater than {resolutionOfCircuit.TotalMilliseconds} milliseconds. This is the minimum resolution of the CircuitBreaker timer."); + } + if (minimumThroughput <= 1) + { throw new ArgumentOutOfRangeException(nameof(minimumThroughput), "Value must be greater than one."); + } + if (durationOfBreak < TimeSpan.Zero) + { throw new ArgumentOutOfRangeException(nameof(durationOfBreak), "Value must be greater than zero."); + } if (onBreak == null) + { throw new ArgumentNullException(nameof(onBreak)); + } + if (onReset == null) + { throw new ArgumentNullException(nameof(onReset)); + } + if (onHalfOpen == null) + { throw new ArgumentNullException(nameof(onHalfOpen)); + } var breakerController = new AdvancedCircuitController( failureThreshold, diff --git a/src/Polly/CircuitBreaker/AsyncCircuitBreakerSyntax.cs b/src/Polly/CircuitBreaker/AsyncCircuitBreakerSyntax.cs index 557469c803..8869618ce9 100644 --- a/src/Polly/CircuitBreaker/AsyncCircuitBreakerSyntax.cs +++ b/src/Polly/CircuitBreaker/AsyncCircuitBreakerSyntax.cs @@ -188,16 +188,29 @@ public static AsyncCircuitBreakerPolicy CircuitBreakerAsync(this PolicyBuilder p public static AsyncCircuitBreakerPolicy CircuitBreakerAsync(this PolicyBuilder policyBuilder, int exceptionsAllowedBeforeBreaking, TimeSpan durationOfBreak, Action onBreak, Action onReset, Action onHalfOpen) { if (exceptionsAllowedBeforeBreaking <= 0) + { throw new ArgumentOutOfRangeException(nameof(exceptionsAllowedBeforeBreaking), "Value must be greater than zero."); + } + if (durationOfBreak < TimeSpan.Zero) + { throw new ArgumentOutOfRangeException(nameof(durationOfBreak), "Value must be greater than zero."); + } if (onBreak == null) + { throw new ArgumentNullException(nameof(onBreak)); + } + if (onReset == null) + { throw new ArgumentNullException(nameof(onReset)); + } + if (onHalfOpen == null) + { throw new ArgumentNullException(nameof(onHalfOpen)); + } var breakerController = new ConsecutiveCountCircuitController( exceptionsAllowedBeforeBreaking, diff --git a/src/Polly/CircuitBreaker/AsyncCircuitBreakerTResultSyntax.cs b/src/Polly/CircuitBreaker/AsyncCircuitBreakerTResultSyntax.cs index 885777e73d..08b164941b 100644 --- a/src/Polly/CircuitBreaker/AsyncCircuitBreakerTResultSyntax.cs +++ b/src/Polly/CircuitBreaker/AsyncCircuitBreakerTResultSyntax.cs @@ -194,16 +194,29 @@ public static AsyncCircuitBreakerPolicy CircuitBreakerAsync(th public static AsyncCircuitBreakerPolicy CircuitBreakerAsync(this PolicyBuilder policyBuilder, int handledEventsAllowedBeforeBreaking, TimeSpan durationOfBreak, Action, CircuitState, TimeSpan, Context> onBreak, Action onReset, Action onHalfOpen) { if (handledEventsAllowedBeforeBreaking <= 0) + { throw new ArgumentOutOfRangeException(nameof(handledEventsAllowedBeforeBreaking), "Value must be greater than zero."); + } + if (durationOfBreak < TimeSpan.Zero) + { throw new ArgumentOutOfRangeException(nameof(durationOfBreak), "Value must be greater than zero."); + } if (onBreak == null) + { throw new ArgumentNullException(nameof(onBreak)); + } + if (onReset == null) + { throw new ArgumentNullException(nameof(onReset)); + } + if (onHalfOpen == null) + { throw new ArgumentNullException(nameof(onHalfOpen)); + } var breakerController = new ConsecutiveCountCircuitController( handledEventsAllowedBeforeBreaking, diff --git a/src/Polly/CircuitBreaker/CircuitBreakerSyntax.cs b/src/Polly/CircuitBreaker/CircuitBreakerSyntax.cs index df960a15a1..aa8a4afe31 100644 --- a/src/Polly/CircuitBreaker/CircuitBreakerSyntax.cs +++ b/src/Polly/CircuitBreaker/CircuitBreakerSyntax.cs @@ -187,16 +187,29 @@ public static CircuitBreakerPolicy CircuitBreaker(this PolicyBuilder policyBuild public static CircuitBreakerPolicy CircuitBreaker(this PolicyBuilder policyBuilder, int exceptionsAllowedBeforeBreaking, TimeSpan durationOfBreak, Action onBreak, Action onReset, Action onHalfOpen) { if (exceptionsAllowedBeforeBreaking <= 0) + { throw new ArgumentOutOfRangeException(nameof(exceptionsAllowedBeforeBreaking), "Value must be greater than zero."); + } + if (durationOfBreak < TimeSpan.Zero) + { throw new ArgumentOutOfRangeException(nameof(durationOfBreak), "Value must be greater than zero."); + } if (onBreak == null) + { throw new ArgumentNullException(nameof(onBreak)); + } + if (onReset == null) + { throw new ArgumentNullException(nameof(onReset)); + } + if (onHalfOpen == null) + { throw new ArgumentNullException(nameof(onHalfOpen)); + } var breakerController = new ConsecutiveCountCircuitController( exceptionsAllowedBeforeBreaking, diff --git a/src/Polly/CircuitBreaker/CircuitBreakerTResultSyntax.cs b/src/Polly/CircuitBreaker/CircuitBreakerTResultSyntax.cs index b001aa2db5..6cdfde61ff 100644 --- a/src/Polly/CircuitBreaker/CircuitBreakerTResultSyntax.cs +++ b/src/Polly/CircuitBreaker/CircuitBreakerTResultSyntax.cs @@ -193,16 +193,29 @@ public static CircuitBreakerPolicy CircuitBreaker(this PolicyB public static CircuitBreakerPolicy CircuitBreaker(this PolicyBuilder policyBuilder, int handledEventsAllowedBeforeBreaking, TimeSpan durationOfBreak, Action, CircuitState, TimeSpan, Context> onBreak, Action onReset, Action onHalfOpen) { if (handledEventsAllowedBeforeBreaking <= 0) + { throw new ArgumentOutOfRangeException(nameof(handledEventsAllowedBeforeBreaking), "Value must be greater than zero."); + } + if (durationOfBreak < TimeSpan.Zero) + { throw new ArgumentOutOfRangeException(nameof(durationOfBreak), "Value must be greater than zero."); + } if (onBreak == null) + { throw new ArgumentNullException(nameof(onBreak)); + } + if (onReset == null) + { throw new ArgumentNullException(nameof(onReset)); + } + if (onHalfOpen == null) + { throw new ArgumentNullException(nameof(onHalfOpen)); + } ICircuitController breakerController = new ConsecutiveCountCircuitController( handledEventsAllowedBeforeBreaking, diff --git a/src/Polly/CircuitBreaker/RollingHealthMetrics.cs b/src/Polly/CircuitBreaker/RollingHealthMetrics.cs index 2117d3f8a6..167da0065f 100644 --- a/src/Polly/CircuitBreaker/RollingHealthMetrics.cs +++ b/src/Polly/CircuitBreaker/RollingHealthMetrics.cs @@ -70,8 +70,10 @@ private HealthCount ActualiseCurrentMetric_NeedsLock() } while (_windows.Count > 0 && now - _windows.Peek().StartedAt >= _samplingDuration) + { _windows.Dequeue(); + } return _currentWindow; } -} \ No newline at end of file +} diff --git a/src/Polly/Context.Dictionary.cs b/src/Polly/Context.Dictionary.cs index ff2c091d96..bbff2d05ed 100644 --- a/src/Polly/Context.Dictionary.cs +++ b/src/Polly/Context.Dictionary.cs @@ -26,7 +26,10 @@ internal Context(IDictionary contextData) : this() { if (contextData == null) + { throw new ArgumentNullException(nameof(contextData)); + } + wrappedDictionary = new Dictionary(contextData); } diff --git a/src/Polly/Fallback/AsyncFallbackSyntax.cs b/src/Polly/Fallback/AsyncFallbackSyntax.cs index d0b4cc9de9..6cc8dd5f0a 100644 --- a/src/Polly/Fallback/AsyncFallbackSyntax.cs +++ b/src/Polly/Fallback/AsyncFallbackSyntax.cs @@ -17,7 +17,9 @@ public static class AsyncFallbackSyntax public static AsyncFallbackPolicy FallbackAsync(this PolicyBuilder policyBuilder, Func fallbackAction) { if (fallbackAction == null) + { throw new ArgumentNullException(nameof(fallbackAction)); + } Func doNothing = _ => TaskHelper.EmptyTask; return policyBuilder.FallbackAsync( @@ -37,9 +39,14 @@ public static AsyncFallbackPolicy FallbackAsync(this PolicyBuilder policyBuilder public static AsyncFallbackPolicy FallbackAsync(this PolicyBuilder policyBuilder, Func fallbackAction, Func onFallbackAsync) { if (fallbackAction == null) + { throw new ArgumentNullException(nameof(fallbackAction)); + } + if (onFallbackAsync == null) + { throw new ArgumentNullException(nameof(onFallbackAsync)); + } return policyBuilder.FallbackAsync( (_, _, ct) => fallbackAction(ct), @@ -58,9 +65,14 @@ public static AsyncFallbackPolicy FallbackAsync(this PolicyBuilder policyBuilder public static AsyncFallbackPolicy FallbackAsync(this PolicyBuilder policyBuilder, Func fallbackAction, Func onFallbackAsync) { if (fallbackAction == null) + { throw new ArgumentNullException(nameof(fallbackAction)); + } + if (onFallbackAsync == null) + { throw new ArgumentNullException(nameof(onFallbackAsync)); + } return policyBuilder.FallbackAsync((_, ctx, ct) => fallbackAction(ctx, ct), onFallbackAsync); } @@ -77,9 +89,14 @@ public static AsyncFallbackPolicy FallbackAsync(this PolicyBuilder policyBuilder public static AsyncFallbackPolicy FallbackAsync(this PolicyBuilder policyBuilder, Func fallbackAction, Func onFallbackAsync) { if (fallbackAction == null) + { throw new ArgumentNullException(nameof(fallbackAction)); + } + if (onFallbackAsync == null) + { throw new ArgumentNullException(nameof(onFallbackAsync)); + } return new AsyncFallbackPolicy(policyBuilder, onFallbackAsync, fallbackAction); } @@ -116,7 +133,9 @@ public static AsyncFallbackPolicy FallbackAsync(this PolicyBui public static AsyncFallbackPolicy FallbackAsync(this PolicyBuilder policyBuilder, Func> fallbackAction) { if (fallbackAction == null) + { throw new ArgumentNullException(nameof(fallbackAction)); + } Func, Task> doNothing = _ => TaskHelper.EmptyTask; return policyBuilder.FallbackAsync( @@ -136,7 +155,9 @@ public static AsyncFallbackPolicy FallbackAsync(this PolicyBui public static AsyncFallbackPolicy FallbackAsync(this PolicyBuilder policyBuilder, TResult fallbackValue, Func, Task> onFallbackAsync) { if (onFallbackAsync == null) + { throw new ArgumentNullException(nameof(onFallbackAsync)); + } return policyBuilder.FallbackAsync( (_, _, _) => Task.FromResult(fallbackValue), @@ -156,9 +177,14 @@ public static AsyncFallbackPolicy FallbackAsync(this PolicyBui public static AsyncFallbackPolicy FallbackAsync(this PolicyBuilder policyBuilder, Func> fallbackAction, Func, Task> onFallbackAsync) { if (fallbackAction == null) + { throw new ArgumentNullException(nameof(fallbackAction)); + } + if (onFallbackAsync == null) + { throw new ArgumentNullException(nameof(onFallbackAsync)); + } return policyBuilder.FallbackAsync( (_, _, ct) => fallbackAction(ct), @@ -177,7 +203,9 @@ public static AsyncFallbackPolicy FallbackAsync(this PolicyBui public static AsyncFallbackPolicy FallbackAsync(this PolicyBuilder policyBuilder, TResult fallbackValue, Func, Context, Task> onFallbackAsync) { if (onFallbackAsync == null) + { throw new ArgumentNullException(nameof(onFallbackAsync)); + } return policyBuilder.FallbackAsync( (_, _, _) => Task.FromResult(fallbackValue), @@ -197,9 +225,14 @@ public static AsyncFallbackPolicy FallbackAsync(this PolicyBui public static AsyncFallbackPolicy FallbackAsync(this PolicyBuilder policyBuilder, Func> fallbackAction, Func, Context, Task> onFallbackAsync) { if (fallbackAction == null) + { throw new ArgumentNullException(nameof(fallbackAction)); + } + if (onFallbackAsync == null) + { throw new ArgumentNullException(nameof(onFallbackAsync)); + } return policyBuilder.FallbackAsync((_, ctx, ct) => fallbackAction(ctx, ct), onFallbackAsync); } @@ -217,9 +250,14 @@ public static AsyncFallbackPolicy FallbackAsync(this PolicyBui public static AsyncFallbackPolicy FallbackAsync(this PolicyBuilder policyBuilder, Func, Context, CancellationToken, Task> fallbackAction, Func, Context, Task> onFallbackAsync) { if (fallbackAction == null) + { throw new ArgumentNullException(nameof(fallbackAction)); + } + if (onFallbackAsync == null) + { throw new ArgumentNullException(nameof(onFallbackAsync)); + } return new AsyncFallbackPolicy( policyBuilder, diff --git a/src/Polly/Fallback/FallbackSyntax.cs b/src/Polly/Fallback/FallbackSyntax.cs index 91143eaa60..f93ccba6cc 100644 --- a/src/Polly/Fallback/FallbackSyntax.cs +++ b/src/Polly/Fallback/FallbackSyntax.cs @@ -17,7 +17,9 @@ public static class FallbackSyntax public static FallbackPolicy Fallback(this PolicyBuilder policyBuilder, Action fallbackAction) { if (fallbackAction == null) + { throw new ArgumentNullException(nameof(fallbackAction)); + } Action doNothing = _ => { }; return policyBuilder.Fallback(fallbackAction, doNothing); @@ -33,7 +35,9 @@ public static FallbackPolicy Fallback(this PolicyBuilder policyBuilder, Action f public static FallbackPolicy Fallback(this PolicyBuilder policyBuilder, Action fallbackAction) { if (fallbackAction == null) + { throw new ArgumentNullException(nameof(fallbackAction)); + } Action doNothing = _ => { }; return policyBuilder.Fallback(fallbackAction, doNothing); @@ -51,9 +55,14 @@ public static FallbackPolicy Fallback(this PolicyBuilder policyBuilder, Action onFallback) { if (fallbackAction == null) + { throw new ArgumentNullException(nameof(fallbackAction)); + } + if (onFallback == null) + { throw new ArgumentNullException(nameof(onFallback)); + } return policyBuilder.Fallback((_, _, _) => fallbackAction(), (exception, _) => onFallback(exception)); } @@ -70,9 +79,14 @@ public static FallbackPolicy Fallback(this PolicyBuilder policyBuilder, Action f public static FallbackPolicy Fallback(this PolicyBuilder policyBuilder, Action fallbackAction, Action onFallback) { if (fallbackAction == null) + { throw new ArgumentNullException(nameof(fallbackAction)); + } + if (onFallback == null) + { throw new ArgumentNullException(nameof(onFallback)); + } return policyBuilder.Fallback((_, _, ct) => fallbackAction(ct), (exception, _) => onFallback(exception)); } @@ -89,9 +103,14 @@ public static FallbackPolicy Fallback(this PolicyBuilder policyBuilder, Action fallbackAction, Action onFallback) { if (fallbackAction == null) + { throw new ArgumentNullException(nameof(fallbackAction)); + } + if (onFallback == null) + { throw new ArgumentNullException(nameof(onFallback)); + } return policyBuilder.Fallback((_, ctx, _) => fallbackAction(ctx), onFallback); } @@ -108,9 +127,14 @@ public static FallbackPolicy Fallback(this PolicyBuilder policyBuilder, Action fallbackAction, Action onFallback) { if (fallbackAction == null) + { throw new ArgumentNullException(nameof(fallbackAction)); + } + if (onFallback == null) + { throw new ArgumentNullException(nameof(onFallback)); + } return policyBuilder.Fallback((_, ctx, ct) => fallbackAction(ctx, ct), onFallback); } @@ -127,9 +151,14 @@ public static FallbackPolicy Fallback(this PolicyBuilder policyBuilder, Action fallbackAction, Action onFallback) { if (fallbackAction == null) + { throw new ArgumentNullException(nameof(fallbackAction)); + } + if (onFallback == null) + { throw new ArgumentNullException(nameof(onFallback)); + } return new FallbackPolicy( policyBuilder, @@ -167,7 +196,9 @@ public static FallbackPolicy Fallback(this PolicyBuilder Fallback(this PolicyBuilder policyBuilder, Func fallbackAction) { if (fallbackAction == null) + { throw new ArgumentNullException(nameof(fallbackAction)); + } Action> doNothing = _ => { }; return policyBuilder.Fallback(fallbackAction, doNothing); @@ -184,7 +215,9 @@ public static FallbackPolicy Fallback(this PolicyBuilder Fallback(this PolicyBuilder policyBuilder, Func fallbackAction) { if (fallbackAction == null) + { throw new ArgumentNullException(nameof(fallbackAction)); + } Action> doNothing = _ => { }; return policyBuilder.Fallback(fallbackAction, doNothing); @@ -202,7 +235,9 @@ public static FallbackPolicy Fallback(this PolicyBuilder Fallback(this PolicyBuilder policyBuilder, TResult fallbackValue, Action> onFallback) { if (onFallback == null) + { throw new ArgumentNullException(nameof(onFallback)); + } return policyBuilder.Fallback((_, _, _) => fallbackValue, (outcome, _) => onFallback(outcome)); } @@ -220,9 +255,14 @@ public static FallbackPolicy Fallback(this PolicyBuilder Fallback(this PolicyBuilder policyBuilder, Func fallbackAction, Action> onFallback) { if (fallbackAction == null) + { throw new ArgumentNullException(nameof(fallbackAction)); + } + if (onFallback == null) + { throw new ArgumentNullException(nameof(onFallback)); + } return policyBuilder.Fallback((_, _, _) => fallbackAction(), (outcome, _) => onFallback(outcome)); } @@ -240,9 +280,14 @@ public static FallbackPolicy Fallback(this PolicyBuilder Fallback(this PolicyBuilder policyBuilder, Func fallbackAction, Action> onFallback) { if (fallbackAction == null) + { throw new ArgumentNullException(nameof(fallbackAction)); + } + if (onFallback == null) + { throw new ArgumentNullException(nameof(onFallback)); + } return policyBuilder.Fallback((_, _, ct) => fallbackAction(ct), (outcome, _) => onFallback(outcome)); } @@ -259,7 +304,9 @@ public static FallbackPolicy Fallback(this PolicyBuilder Fallback(this PolicyBuilder policyBuilder, TResult fallbackValue, Action, Context> onFallback) { if (onFallback == null) + { throw new ArgumentNullException(nameof(onFallback)); + } return policyBuilder.Fallback((_, _, _) => fallbackValue, onFallback); } @@ -277,9 +324,14 @@ public static FallbackPolicy Fallback(this PolicyBuilder Fallback(this PolicyBuilder policyBuilder, Func fallbackAction, Action, Context> onFallback) { if (fallbackAction == null) + { throw new ArgumentNullException(nameof(fallbackAction)); + } + if (onFallback == null) + { throw new ArgumentNullException(nameof(onFallback)); + } return policyBuilder.Fallback((_, ctx, _) => fallbackAction(ctx), onFallback); } @@ -297,9 +349,14 @@ public static FallbackPolicy Fallback(this PolicyBuilder Fallback(this PolicyBuilder policyBuilder, Func fallbackAction, Action, Context> onFallback) { if (fallbackAction == null) + { throw new ArgumentNullException(nameof(fallbackAction)); + } + if (onFallback == null) + { throw new ArgumentNullException(nameof(onFallback)); + } return policyBuilder.Fallback((_, ctx, ct) => fallbackAction(ctx, ct), onFallback); } @@ -317,9 +374,14 @@ public static FallbackPolicy Fallback(this PolicyBuilder Fallback(this PolicyBuilder policyBuilder, Func, Context, CancellationToken, TResult> fallbackAction, Action, Context> onFallback) { if (fallbackAction == null) + { throw new ArgumentNullException(nameof(fallbackAction)); + } + if (onFallback == null) + { throw new ArgumentNullException(nameof(onFallback)); + } return new FallbackPolicy( policyBuilder, diff --git a/src/Polly/Policy.ContextAndKeys.cs b/src/Polly/Policy.ContextAndKeys.cs index f1fbbc18eb..cc68f5768d 100644 --- a/src/Polly/Policy.ContextAndKeys.cs +++ b/src/Polly/Policy.ContextAndKeys.cs @@ -10,7 +10,9 @@ public abstract partial class Policy public Policy WithPolicyKey(string policyKey) { if (policyKeyInternal != null) + { throw PolicyKeyMustBeImmutableException(nameof(policyKey)); + } policyKeyInternal = policyKey; return this; @@ -24,7 +26,9 @@ public Policy WithPolicyKey(string policyKey) ISyncPolicy ISyncPolicy.WithPolicyKey(string policyKey) { if (policyKeyInternal != null) + { throw PolicyKeyMustBeImmutableException(nameof(policyKey)); + } policyKeyInternal = policyKey; return this; @@ -41,7 +45,9 @@ public abstract partial class Policy public Policy WithPolicyKey(string policyKey) { if (policyKeyInternal != null) + { throw PolicyKeyMustBeImmutableException(nameof(policyKey)); + } policyKeyInternal = policyKey; return this; @@ -55,7 +61,9 @@ public Policy WithPolicyKey(string policyKey) ISyncPolicy ISyncPolicy.WithPolicyKey(string policyKey) { if (policyKeyInternal != null) + { throw PolicyKeyMustBeImmutableException(nameof(policyKey)); + } policyKeyInternal = policyKey; return this; diff --git a/src/Polly/Policy.ExecuteOverloads.cs b/src/Polly/Policy.ExecuteOverloads.cs index c4828d63f8..ce9e1ef6fa 100644 --- a/src/Polly/Policy.ExecuteOverloads.cs +++ b/src/Polly/Policy.ExecuteOverloads.cs @@ -60,7 +60,9 @@ public abstract partial class Policy : ISyncPolicy public void Execute(Action action, Context context, CancellationToken cancellationToken) { if (context == null) + { throw new ArgumentNullException(nameof(context)); + } SetPolicyContext(context, out string priorPolicyWrapKey, out string priorPolicyKey); @@ -146,7 +148,9 @@ public void Execute(Action action, Context context, public TResult Execute(Func action, Context context, CancellationToken cancellationToken) { if (context == null) + { throw new ArgumentNullException(nameof(context)); + } SetPolicyContext(context, out string priorPolicyWrapKey, out string priorPolicyKey); @@ -229,7 +233,9 @@ public TResult Execute(Func action public PolicyResult ExecuteAndCapture(Action action, Context context, CancellationToken cancellationToken) { if (context == null) + { throw new ArgumentNullException(nameof(context)); + } try { @@ -313,7 +319,9 @@ public PolicyResult ExecuteAndCapture(Action action, public PolicyResult ExecuteAndCapture(Func action, Context context, CancellationToken cancellationToken) { if (context == null) + { throw new ArgumentNullException(nameof(context)); + } try { diff --git a/src/Polly/Policy.TResult.ExecuteOverloads.cs b/src/Polly/Policy.TResult.ExecuteOverloads.cs index 1aaa40a0cb..7050636310 100644 --- a/src/Polly/Policy.TResult.ExecuteOverloads.cs +++ b/src/Polly/Policy.TResult.ExecuteOverloads.cs @@ -68,7 +68,9 @@ public abstract partial class Policy : ISyncPolicy public TResult Execute(Func action, Context context, CancellationToken cancellationToken) { if (context == null) + { throw new ArgumentNullException(nameof(context)); + } SetPolicyContext(context, out string priorPolicyWrapKey, out string priorPolicyKey); @@ -150,7 +152,9 @@ public TResult Execute(Func action, Context public PolicyResult ExecuteAndCapture(Func action, Context context, CancellationToken cancellationToken) { if (context == null) + { throw new ArgumentNullException(nameof(context)); + } try { diff --git a/src/Polly/PolicyBuilder.OrSyntax.cs b/src/Polly/PolicyBuilder.OrSyntax.cs index 27ce21e8cd..5d493c5aac 100644 --- a/src/Polly/PolicyBuilder.OrSyntax.cs +++ b/src/Polly/PolicyBuilder.OrSyntax.cs @@ -64,7 +64,9 @@ public PolicyBuilder OrInner(Func exceptionPredica { var matchedInAggregate = HandleInnerNested(predicate, innerException); if (matchedInAggregate != null) + { return matchedInAggregate; + } } } @@ -74,9 +76,14 @@ public PolicyBuilder OrInner(Func exceptionPredica private static Exception HandleInnerNested(Func predicate, Exception current) { if (current == null) + { return null; + } + if (predicate(current)) + { return current; + } return HandleInnerNested(predicate, current.InnerException); } diff --git a/src/Polly/Polly.csproj b/src/Polly/Polly.csproj index ef9bae12f9..891435d154 100644 --- a/src/Polly/Polly.csproj +++ b/src/Polly/Polly.csproj @@ -7,7 +7,7 @@ Library 70 true - $(NoWarn);IDE0011;S103;S3872;SA1402;SA1414;S3215 + $(NoWarn);S103;S3872;SA1402;SA1414;S3215 $(NoWarn);IDE1006;CA1062;S107;CA1068;S4039;CA1000;CA1063;CA1031;CA1051 $(NoWarn);CA2211;S2223;CA1032;CA1815;CA1816;S4457;SA1615;CA1033 $(NoWarn);S4023;CA1010;S3442;CA1064;SA1649;SA1625;SA1623;SA1118 diff --git a/src/Polly/RateLimit/AsyncRateLimitSyntax.cs b/src/Polly/RateLimit/AsyncRateLimitSyntax.cs index 7901f7524c..0a28bc5861 100644 --- a/src/Polly/RateLimit/AsyncRateLimitSyntax.cs +++ b/src/Polly/RateLimit/AsyncRateLimitSyntax.cs @@ -29,11 +29,19 @@ public partial class Policy int maxBurst) { if (numberOfExecutions < 1) + { throw new ArgumentOutOfRangeException(nameof(numberOfExecutions), numberOfExecutions, $"{nameof(numberOfExecutions)} per timespan must be an integer greater than or equal to 1."); + } + if (perTimeSpan <= TimeSpan.Zero) + { throw new ArgumentOutOfRangeException(nameof(perTimeSpan), perTimeSpan, $"{nameof(perTimeSpan)} must be a positive timespan."); + } + if (maxBurst < 1) + { throw new ArgumentOutOfRangeException(nameof(maxBurst), maxBurst, $"{nameof(maxBurst)} must be an integer greater than or equal to 1."); + } var onePer = TimeSpan.FromTicks(perTimeSpan.Ticks / numberOfExecutions); diff --git a/src/Polly/RateLimit/AsyncRateLimitTResultSyntax.cs b/src/Polly/RateLimit/AsyncRateLimitTResultSyntax.cs index b1f92e9aaf..baf5c3cf05 100644 --- a/src/Polly/RateLimit/AsyncRateLimitTResultSyntax.cs +++ b/src/Polly/RateLimit/AsyncRateLimitTResultSyntax.cs @@ -65,11 +65,19 @@ public partial class Policy Func? retryAfterFactory) { if (numberOfExecutions < 1) + { throw new ArgumentOutOfRangeException(nameof(numberOfExecutions), numberOfExecutions, $"{nameof(numberOfExecutions)} per timespan must be an integer greater than or equal to 1."); + } + if (perTimeSpan <= TimeSpan.Zero) + { throw new ArgumentOutOfRangeException(nameof(perTimeSpan), perTimeSpan, $"{nameof(perTimeSpan)} must be a positive timespan."); + } + if (maxBurst < 1) + { throw new ArgumentOutOfRangeException(nameof(maxBurst), maxBurst, $"{nameof(maxBurst)} must be an integer greater than or equal to 1."); + } var onePer = TimeSpan.FromTicks(perTimeSpan.Ticks / numberOfExecutions); diff --git a/src/Polly/RateLimit/LockFreeTokenBucketRateLimiter.cs b/src/Polly/RateLimit/LockFreeTokenBucketRateLimiter.cs index 22057a9a05..bb2ab0829e 100644 --- a/src/Polly/RateLimit/LockFreeTokenBucketRateLimiter.cs +++ b/src/Polly/RateLimit/LockFreeTokenBucketRateLimiter.cs @@ -28,9 +28,14 @@ internal sealed class LockFreeTokenBucketRateLimiter : IRateLimiter public LockFreeTokenBucketRateLimiter(TimeSpan onePer, long bucketCapacity) { if (onePer <= TimeSpan.Zero) + { throw new ArgumentOutOfRangeException(nameof(onePer), onePer, $"The {nameof(LockFreeTokenBucketRateLimiter)} must specify a positive TimeSpan for how often an execution is permitted."); + } + if (bucketCapacity <= 0) + { throw new ArgumentOutOfRangeException(nameof(bucketCapacity), bucketCapacity, $"{nameof(bucketCapacity)} must be greater than or equal to 1."); + } addTokenTickInterval = onePer.Ticks; this.bucketCapacity = bucketCapacity; diff --git a/src/Polly/RateLimit/RateLimitRejectedException.cs b/src/Polly/RateLimit/RateLimitRejectedException.cs index ec85760f6e..a1524edf27 100644 --- a/src/Polly/RateLimit/RateLimitRejectedException.cs +++ b/src/Polly/RateLimit/RateLimitRejectedException.cs @@ -61,7 +61,10 @@ public RateLimitRejectedException(TimeSpan retryAfter, string message, Exception private void SetRetryAfter(TimeSpan retryAfter) { if (retryAfter < TimeSpan.Zero) + { throw new ArgumentOutOfRangeException(nameof(retryAfter), retryAfter, $"The {nameof(retryAfter)} parameter must be a TimeSpan greater than or equal to TimeSpan.Zero."); + } + RetryAfter = retryAfter; } diff --git a/src/Polly/RateLimit/RateLimitSyntax.cs b/src/Polly/RateLimit/RateLimitSyntax.cs index 8da7ec127e..26b27c096a 100644 --- a/src/Polly/RateLimit/RateLimitSyntax.cs +++ b/src/Polly/RateLimit/RateLimitSyntax.cs @@ -29,11 +29,19 @@ public partial class Policy int maxBurst) { if (numberOfExecutions < 1) + { throw new ArgumentOutOfRangeException(nameof(numberOfExecutions), numberOfExecutions, $"{nameof(numberOfExecutions)} per timespan must be an integer greater than or equal to 1."); + } + if (perTimeSpan <= TimeSpan.Zero) + { throw new ArgumentOutOfRangeException(nameof(perTimeSpan), perTimeSpan, $"{nameof(perTimeSpan)} must be a positive timespan."); + } + if (maxBurst < 1) + { throw new ArgumentOutOfRangeException(nameof(maxBurst), maxBurst, $"{nameof(maxBurst)} must be an integer greater than or equal to 1."); + } var onePer = TimeSpan.FromTicks(perTimeSpan.Ticks / numberOfExecutions); diff --git a/src/Polly/RateLimit/RateLimitTResultSyntax.cs b/src/Polly/RateLimit/RateLimitTResultSyntax.cs index 67fc8fb5d0..c2d5e9867c 100644 --- a/src/Polly/RateLimit/RateLimitTResultSyntax.cs +++ b/src/Polly/RateLimit/RateLimitTResultSyntax.cs @@ -65,11 +65,19 @@ public partial class Policy Func? retryAfterFactory) { if (numberOfExecutions < 1) + { throw new ArgumentOutOfRangeException(nameof(numberOfExecutions), numberOfExecutions, $"{nameof(numberOfExecutions)} per timespan must be an integer greater than or equal to 1."); + } + if (perTimeSpan <= TimeSpan.Zero) + { throw new ArgumentOutOfRangeException(nameof(perTimeSpan), perTimeSpan, $"{nameof(perTimeSpan)} must be a positive timespan."); + } + if (maxBurst < 1) + { throw new ArgumentOutOfRangeException(nameof(maxBurst), maxBurst, $"{nameof(maxBurst)} must be an integer greater than or equal to 1."); + } var onePer = TimeSpan.FromTicks(perTimeSpan.Ticks / numberOfExecutions); diff --git a/src/Polly/ResultPredicates.cs b/src/Polly/ResultPredicates.cs index 63b02ddec3..ed98c3b317 100644 --- a/src/Polly/ResultPredicates.cs +++ b/src/Polly/ResultPredicates.cs @@ -22,7 +22,9 @@ internal void Add(ResultPredicate predicate) public bool AnyMatch(TResult result) { if (_predicates == null) + { return false; + } return _predicates.Any(predicate => predicate(result)); } diff --git a/src/Polly/Retry/AsyncRetrySyntax.cs b/src/Polly/Retry/AsyncRetrySyntax.cs index 7cbb1d0607..8f7d13651e 100644 --- a/src/Polly/Retry/AsyncRetrySyntax.cs +++ b/src/Polly/Retry/AsyncRetrySyntax.cs @@ -64,7 +64,9 @@ public static AsyncRetryPolicy RetryAsync(this PolicyBuilder policyBuilder, int public static AsyncRetryPolicy RetryAsync(this PolicyBuilder policyBuilder, int retryCount, Action onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.RetryAsync(retryCount, onRetryAsync: async (outcome, i, _) => onRetry(outcome, i)); @@ -84,7 +86,9 @@ public static AsyncRetryPolicy RetryAsync(this PolicyBuilder policyBuilder, int public static AsyncRetryPolicy RetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func onRetryAsync) { if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } return policyBuilder.RetryAsync(retryCount, onRetryAsync: (outcome, i, _) => onRetryAsync(outcome, i)); } @@ -124,7 +128,9 @@ public static AsyncRetryPolicy RetryAsync(this PolicyBuilder policyBuilder, int public static AsyncRetryPolicy RetryAsync(this PolicyBuilder policyBuilder, int retryCount, Action onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.RetryAsync(retryCount, @@ -145,9 +151,14 @@ public static AsyncRetryPolicy RetryAsync(this PolicyBuilder policyBuilder, int public static AsyncRetryPolicy RetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func onRetryAsync) { if (retryCount < 0) + { throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); + } + if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } return new AsyncRetryPolicy( policyBuilder, @@ -178,7 +189,9 @@ public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilde public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Action onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.RetryForeverAsync( @@ -197,7 +210,9 @@ public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilde public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Action onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.RetryForeverAsync( @@ -216,7 +231,9 @@ public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilde public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Func onRetryAsync) { if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } return policyBuilder.RetryForeverAsync(onRetryAsync: (Exception outcome, Context _) => onRetryAsync(outcome)); } @@ -232,7 +249,9 @@ public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilde public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Func onRetryAsync) { if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } return policyBuilder.RetryForeverAsync(onRetryAsync: (outcome, i, _) => onRetryAsync(outcome, i)); } @@ -248,7 +267,9 @@ public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilde public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Action onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.RetryForeverAsync( @@ -267,7 +288,9 @@ public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilde public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Action onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.RetryForeverAsync( @@ -286,7 +309,9 @@ public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilde public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Func onRetryAsync) { if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } return new AsyncRetryPolicy( policyBuilder, @@ -304,7 +329,9 @@ public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilde public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Func onRetryAsync) { if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } return new AsyncRetryPolicy( policyBuilder, @@ -343,7 +370,9 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -369,7 +398,9 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Func onRetryAsync) { if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -396,7 +427,9 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde Func sleepDurationProvider, Action onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -423,7 +456,9 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde Func sleepDurationProvider, Func onRetryAsync) { if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } return policyBuilder.WaitAndRetryAsync( retryCount, @@ -447,7 +482,9 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -474,11 +511,19 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde Func sleepDurationProvider, Func onRetryAsync) { if (retryCount < 0) + { throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); + } + if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } + if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } IEnumerable sleepDurations = Enumerable.Range(1, retryCount) .Select(sleepDurationProvider); @@ -507,7 +552,9 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde Func sleepDurationProvider, Action onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -534,7 +581,9 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde Func sleepDurationProvider, Func onRetryAsync) { if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } return policyBuilder.WaitAndRetryAsync( retryCount, @@ -558,7 +607,9 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -585,7 +636,10 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde Func sleepDurationProvider, Func onRetryAsync) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } + return policyBuilder.WaitAndRetryAsync( retryCount, (i, _, ctx) => sleepDurationProvider(i, ctx), @@ -609,11 +663,19 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde Func sleepDurationProvider, Func onRetryAsync) { if (retryCount < 0) + { throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); + } + if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } + if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } return new AsyncRetryPolicy( policyBuilder, @@ -651,7 +713,9 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Action onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -674,7 +738,9 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Func onRetryAsync) { if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } return policyBuilder.WaitAndRetryAsync( sleepDurations, @@ -695,7 +761,9 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Action onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -718,7 +786,9 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Func onRetryAsync) { if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } return policyBuilder.WaitAndRetryAsync( sleepDurations, @@ -739,7 +809,9 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Action onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -762,9 +834,14 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Func onRetryAsync) { if (sleepDurations == null) + { throw new ArgumentNullException(nameof(sleepDurations)); + } + if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } return new AsyncRetryPolicy( policyBuilder, @@ -784,7 +861,9 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } Action doNothing = (_, _) => { }; @@ -803,7 +882,9 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder polic public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } Action doNothing = (_, _, _) => { }; @@ -825,9 +906,14 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder polic public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action onRetry) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } + if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return policyBuilder.WaitAndRetryForeverAsync( (retryCount, _) => sleepDurationProvider(retryCount), @@ -849,9 +935,14 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder polic public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action onRetry) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } + if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return policyBuilder.WaitAndRetryForeverAsync( (retryCount, _) => sleepDurationProvider(retryCount), @@ -873,9 +964,14 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder polic public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Func onRetryAsync) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } + if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } return policyBuilder.WaitAndRetryForeverAsync( (retryCount, _) => sleepDurationProvider(retryCount), @@ -897,9 +993,14 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder polic public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Func onRetryAsync) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } + if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } return policyBuilder.WaitAndRetryForeverAsync( (retryCount, _) => sleepDurationProvider(retryCount), @@ -921,9 +1022,14 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder polic public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action onRetry) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } + if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryForeverAsync( @@ -947,9 +1053,14 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder polic public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action onRetry) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } + if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryForeverAsync( @@ -973,7 +1084,9 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder polic public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Func onRetryAsync) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } return policyBuilder.WaitAndRetryForeverAsync( (i, _, ctx) => sleepDurationProvider(i, ctx), @@ -995,7 +1108,9 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder polic public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Func onRetryAsync) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } return policyBuilder.WaitAndRetryForeverAsync( (i, _, ctx) => sleepDurationProvider(i, ctx), @@ -1017,9 +1132,14 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder polic public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Func onRetryAsync) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } + if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } return new AsyncRetryPolicy( policyBuilder, @@ -1042,9 +1162,14 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder polic public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Func onRetryAsync) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } + if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } return new AsyncRetryPolicy( policyBuilder, diff --git a/src/Polly/Retry/AsyncRetryTResultSyntax.cs b/src/Polly/Retry/AsyncRetryTResultSyntax.cs index 5d5a8ba922..799e3b3ee8 100644 --- a/src/Polly/Retry/AsyncRetryTResultSyntax.cs +++ b/src/Polly/Retry/AsyncRetryTResultSyntax.cs @@ -68,7 +68,9 @@ public static AsyncRetryPolicy RetryAsync(this PolicyBuilder RetryAsync(this PolicyBuilder policyBuilder, int retryCount, Action, int> onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.RetryAsync(retryCount, @@ -90,7 +92,9 @@ public static AsyncRetryPolicy RetryAsync(this PolicyBuilder RetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func, int, Task> onRetryAsync) { if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } return policyBuilder.RetryAsync(retryCount, onRetryAsync: (outcome, i, _) => onRetryAsync(outcome, i)); } @@ -133,7 +137,9 @@ public static AsyncRetryPolicy RetryAsync(this PolicyBuilder RetryAsync(this PolicyBuilder policyBuilder, int retryCount, Action, int, Context> onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.RetryAsync(retryCount, @@ -155,9 +161,14 @@ public static AsyncRetryPolicy RetryAsync(this PolicyBuilder RetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func, int, Context, Task> onRetryAsync) { if (retryCount < 0) + { throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); + } + if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } return new AsyncRetryPolicy( policyBuilder, @@ -190,7 +201,9 @@ public static AsyncRetryPolicy RetryForeverAsync(this PolicyBu public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Action> onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.RetryForeverAsync( @@ -210,7 +223,9 @@ public static AsyncRetryPolicy RetryForeverAsync(this PolicyBu public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Action, int> onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.RetryForeverAsync( @@ -230,7 +245,9 @@ public static AsyncRetryPolicy RetryForeverAsync(this PolicyBu public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Func, Task> onRetryAsync) { if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } return policyBuilder.RetryForeverAsync(onRetryAsync: (DelegateResult outcome, Context _) => onRetryAsync(outcome)); } @@ -247,7 +264,9 @@ public static AsyncRetryPolicy RetryForeverAsync(this PolicyBu public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Func, int, Task> onRetryAsync) { if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } return policyBuilder.RetryForeverAsync(onRetryAsync: (outcome, i, _) => onRetryAsync(outcome, i)); } @@ -264,7 +283,9 @@ public static AsyncRetryPolicy RetryForeverAsync(this PolicyBu public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Action, Context> onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.RetryForeverAsync( @@ -284,7 +305,9 @@ public static AsyncRetryPolicy RetryForeverAsync(this PolicyBu public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Action, int, Context> onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.RetryForeverAsync( @@ -304,7 +327,9 @@ public static AsyncRetryPolicy RetryForeverAsync(this PolicyBu public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Func, Context, Task> onRetryAsync) { if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } return new AsyncRetryPolicy( policyBuilder, @@ -323,7 +348,9 @@ public static AsyncRetryPolicy RetryForeverAsync(this PolicyBu public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Func, int, Context, Task> onRetryAsync) { if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } return new AsyncRetryPolicy( policyBuilder, @@ -365,7 +392,9 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu Func sleepDurationProvider, Action, TimeSpan> onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -392,7 +421,9 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Func, TimeSpan, Task> onRetryAsync) { if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -420,7 +451,9 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu Func sleepDurationProvider, Action, TimeSpan, Context> onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -447,7 +480,9 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Func, TimeSpan, Context, Task> onRetryAsync) { if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } return policyBuilder.WaitAndRetryAsync( retryCount, @@ -472,7 +507,9 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action, TimeSpan, int, Context> onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -500,11 +537,19 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu Func sleepDurationProvider, Func, TimeSpan, int, Context, Task> onRetryAsync) { if (retryCount < 0) + { throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); + } + if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } + if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } IEnumerable sleepDurations = Enumerable.Range(1, retryCount) .Select(sleepDurationProvider); @@ -534,7 +579,9 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu Func sleepDurationProvider, Action, TimeSpan, Context> onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -561,7 +608,9 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Func, TimeSpan, Context, Task> onRetryAsync) { if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } return policyBuilder.WaitAndRetryAsync( retryCount, @@ -586,7 +635,9 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action, TimeSpan, int, Context> onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -614,7 +665,9 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu Func sleepDurationProvider, Func, TimeSpan, int, Context, Task> onRetryAsync) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } return policyBuilder.WaitAndRetryAsync( retryCount, @@ -640,11 +693,19 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu Func, Context, TimeSpan> sleepDurationProvider, Func, TimeSpan, int, Context, Task> onRetryAsync) { if (retryCount < 0) + { throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); + } + if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } + if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } return new AsyncRetryPolicy( policyBuilder, @@ -684,7 +745,9 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Action, TimeSpan> onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -708,7 +771,9 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Func, TimeSpan, Task> onRetryAsync) { if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } return policyBuilder.WaitAndRetryAsync( sleepDurations, @@ -730,7 +795,9 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Action, TimeSpan, Context> onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -754,7 +821,9 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Func, TimeSpan, Context, Task> onRetryAsync) { if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } return policyBuilder.WaitAndRetryAsync( sleepDurations, @@ -776,7 +845,9 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Action, TimeSpan, int, Context> onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -800,9 +871,14 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Func, TimeSpan, int, Context, Task> onRetryAsync) { if (sleepDurations == null) + { throw new ArgumentNullException(nameof(sleepDurations)); + } + if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } return new AsyncRetryPolicy( policyBuilder, @@ -823,7 +899,9 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } Action, TimeSpan> doNothing = (_, _) => { }; @@ -843,7 +921,9 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this P public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } Action, TimeSpan, Context> doNothing = (_, _, _) => { }; @@ -866,9 +946,14 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this P public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action, TimeSpan> onRetry) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } + if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return policyBuilder.WaitAndRetryForeverAsync( (retryCount, _) => sleepDurationProvider(retryCount), @@ -891,9 +976,14 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this P public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action, int, TimeSpan> onRetry) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } + if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return policyBuilder.WaitAndRetryForeverAsync( (retryCount, _) => sleepDurationProvider(retryCount), @@ -916,9 +1006,14 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this P public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Func, TimeSpan, Task> onRetryAsync) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } + if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } return policyBuilder.WaitAndRetryForeverAsync( (retryCount, _) => sleepDurationProvider(retryCount), @@ -941,9 +1036,14 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this P public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Func, int, TimeSpan, Task> onRetryAsync) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } + if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } return policyBuilder.WaitAndRetryForeverAsync( (retryCount, _) => sleepDurationProvider(retryCount), @@ -966,7 +1066,9 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this P public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action, TimeSpan, Context> onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryForeverAsync( @@ -991,7 +1093,9 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this P public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action, int, TimeSpan, Context> onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryForeverAsync( @@ -1016,7 +1120,9 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this P public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Func, TimeSpan, Context, Task> onRetryAsync) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } return policyBuilder.WaitAndRetryForeverAsync( (i, _, ctx) => sleepDurationProvider(i, ctx), @@ -1039,7 +1145,9 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this P public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Func, int, TimeSpan, Context, Task> onRetryAsync) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } return policyBuilder.WaitAndRetryForeverAsync( (i, _, ctx) => sleepDurationProvider(i, ctx), @@ -1062,9 +1170,14 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this P public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func, Context, TimeSpan> sleepDurationProvider, Func, TimeSpan, Context, Task> onRetryAsync) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } + if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } return new AsyncRetryPolicy( policyBuilder, @@ -1088,9 +1201,14 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this P public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func, Context, TimeSpan> sleepDurationProvider, Func, int, TimeSpan, Context, Task> onRetryAsync) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } + if (onRetryAsync == null) + { throw new ArgumentNullException(nameof(onRetryAsync)); + } return new AsyncRetryPolicy( policyBuilder, diff --git a/src/Polly/Retry/RetrySyntax.cs b/src/Polly/Retry/RetrySyntax.cs index 8a371b9aa4..fb13862da0 100644 --- a/src/Polly/Retry/RetrySyntax.cs +++ b/src/Polly/Retry/RetrySyntax.cs @@ -50,9 +50,14 @@ public static RetryPolicy Retry(this PolicyBuilder policyBuilder, int retryCount public static RetryPolicy Retry(this PolicyBuilder policyBuilder, int retryCount, Action onRetry) { if (retryCount < 0) + { throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); + } + if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return policyBuilder.Retry(retryCount, (outcome, i, _) => onRetry(outcome, i)); } @@ -81,9 +86,14 @@ public static RetryPolicy Retry(this PolicyBuilder policyBuilder, int retryCount public static RetryPolicy Retry(this PolicyBuilder policyBuilder, int retryCount, Action onRetry) { if (retryCount < 0) + { throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); + } + if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return new RetryPolicy( policyBuilder, @@ -114,7 +124,9 @@ public static RetryPolicy RetryForever(this PolicyBuilder policyBuilder) public static RetryPolicy RetryForever(this PolicyBuilder policyBuilder, Action onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return policyBuilder.RetryForever((Exception outcome, Context _) => onRetry(outcome)); } @@ -130,7 +142,9 @@ public static RetryPolicy RetryForever(this PolicyBuilder policyBuilder, Action< public static RetryPolicy RetryForever(this PolicyBuilder policyBuilder, Action onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return policyBuilder.RetryForever((outcome, i, _) => onRetry(outcome, i)); } @@ -146,7 +160,9 @@ public static RetryPolicy RetryForever(this PolicyBuilder policyBuilder, Action< public static RetryPolicy RetryForever(this PolicyBuilder policyBuilder, Action onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return new RetryPolicy( policyBuilder, @@ -164,7 +180,9 @@ public static RetryPolicy RetryForever(this PolicyBuilder policyBuilder, Action< public static RetryPolicy RetryForever(this PolicyBuilder policyBuilder, Action onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return new RetryPolicy( policyBuilder, @@ -203,7 +221,9 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int ret public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return policyBuilder.WaitAndRetry( retryCount, @@ -227,7 +247,9 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int ret public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return policyBuilder.WaitAndRetry( retryCount, @@ -251,11 +273,19 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int ret public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action onRetry) { if (retryCount < 0) + { throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); + } + if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } + if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } var sleepDurations = Enumerable.Range(1, retryCount) .Select(sleepDurationProvider); @@ -299,7 +329,9 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int ret public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return policyBuilder.WaitAndRetry( retryCount, @@ -323,7 +355,10 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int ret public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action onRetry) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } + return policyBuilder.WaitAndRetry( retryCount, (i, _, ctx) => sleepDurationProvider(i, ctx), @@ -346,11 +381,19 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int ret public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action onRetry) { if (retryCount < 0) + { throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); + } + if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } + if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return new RetryPolicy( policyBuilder, @@ -386,7 +429,9 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, IEnumer public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Action onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return policyBuilder.WaitAndRetry(sleepDurations, (outcome, span, _, _) => onRetry(outcome, span)); } @@ -404,7 +449,9 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, IEnumer public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Action onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return policyBuilder.WaitAndRetry(sleepDurations, (outcome, span, _, ctx) => onRetry(outcome, span, ctx)); } @@ -422,9 +469,14 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, IEnumer public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Action onRetry) { if (sleepDurations == null) + { throw new ArgumentNullException(nameof(sleepDurations)); + } + if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return new RetryPolicy( policyBuilder, @@ -444,7 +496,9 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, IEnumer public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func sleepDurationProvider) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } Action doNothing = (_, _) => { }; @@ -463,7 +517,9 @@ public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func sleepDurationProvider) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } Action doNothing = (_, _, _) => { }; @@ -485,9 +541,14 @@ public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action onRetry) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } + if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return policyBuilder.WaitAndRetryForever( (retryCount, _) => sleepDurationProvider(retryCount), @@ -509,9 +570,14 @@ public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action onRetry) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } + if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return policyBuilder.WaitAndRetryForever( (retryCount, _, _) => sleepDurationProvider(retryCount), @@ -533,7 +599,9 @@ public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action onRetry) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } return policyBuilder.WaitAndRetryForever( (i, _, ctx) => sleepDurationProvider(i, ctx), @@ -555,7 +623,9 @@ public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action onRetry) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } return policyBuilder.WaitAndRetryForever( (i, _, ctx) => sleepDurationProvider(i, ctx), @@ -577,9 +647,14 @@ public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action onRetry) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } + if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return new RetryPolicy( policyBuilder, @@ -602,9 +677,14 @@ public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action onRetry) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } + if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return new RetryPolicy( policyBuilder, diff --git a/src/Polly/Retry/RetryTResultSyntax.cs b/src/Polly/Retry/RetryTResultSyntax.cs index 3c1fbc30be..0f6e29fbd3 100644 --- a/src/Polly/Retry/RetryTResultSyntax.cs +++ b/src/Polly/Retry/RetryTResultSyntax.cs @@ -54,9 +54,14 @@ public static RetryPolicy Retry(this PolicyBuilder po public static RetryPolicy Retry(this PolicyBuilder policyBuilder, int retryCount, Action, int> onRetry) { if (retryCount < 0) + { throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); + } + if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return policyBuilder.Retry(retryCount, (outcome, i, _) => onRetry(outcome, i)); } @@ -87,9 +92,14 @@ public static RetryPolicy Retry(this PolicyBuilder po public static RetryPolicy Retry(this PolicyBuilder policyBuilder, int retryCount, Action, int, Context> onRetry) { if (retryCount < 0) + { throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); + } + if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return new RetryPolicy( policyBuilder, @@ -122,7 +132,9 @@ public static RetryPolicy RetryForever(this PolicyBuilder RetryForever(this PolicyBuilder policyBuilder, Action> onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return policyBuilder.RetryForever((DelegateResult outcome, Context _) => onRetry(outcome)); } @@ -139,7 +151,9 @@ public static RetryPolicy RetryForever(this PolicyBuilder RetryForever(this PolicyBuilder policyBuilder, Action, int> onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return policyBuilder.RetryForever((outcome, i, _) => onRetry(outcome, i)); } @@ -156,7 +170,9 @@ public static RetryPolicy RetryForever(this PolicyBuilder RetryForever(this PolicyBuilder policyBuilder, Action, Context> onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return new RetryPolicy( policyBuilder, @@ -175,7 +191,9 @@ public static RetryPolicy RetryForever(this PolicyBuilder RetryForever(this PolicyBuilder policyBuilder, Action, int, Context> onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return new RetryPolicy( policyBuilder, @@ -216,7 +234,9 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action, TimeSpan> onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return policyBuilder.WaitAndRetry( retryCount, @@ -241,7 +261,9 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action, TimeSpan, Context> onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return policyBuilder.WaitAndRetry( retryCount, @@ -266,11 +288,19 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action, TimeSpan, int, Context> onRetry) { if (retryCount < 0) + { throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); + } + if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } + if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } var sleepDurations = Enumerable.Range(1, retryCount) .Select(sleepDurationProvider); @@ -316,7 +346,9 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action, TimeSpan, Context> onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return policyBuilder.WaitAndRetry( retryCount, @@ -378,7 +410,9 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func, Context, TimeSpan> sleepDurationProvider, Action, TimeSpan, Context> onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return policyBuilder.WaitAndRetry( retryCount, @@ -403,11 +437,19 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func, Context, TimeSpan> sleepDurationProvider, Action, TimeSpan, int, Context> onRetry) { if (retryCount < 0) + { throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); + } + if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } + if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return new RetryPolicy( policyBuilder, @@ -445,7 +487,9 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder WaitAndRetry(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Action, TimeSpan> onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return policyBuilder.WaitAndRetry(sleepDurations, (outcome, span, _, _) => onRetry(outcome, span)); } @@ -464,7 +508,9 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder WaitAndRetry(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Action, TimeSpan, Context> onRetry) { if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return policyBuilder.WaitAndRetry(sleepDurations, (outcome, span, _, ctx) => onRetry(outcome, span, ctx)); } @@ -483,9 +529,14 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder WaitAndRetry(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Action, TimeSpan, int, Context> onRetry) { if (sleepDurations == null) + { throw new ArgumentNullException(nameof(sleepDurations)); + } + if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return new RetryPolicy( policyBuilder, @@ -506,7 +557,9 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder WaitAndRetryForever(this PolicyBuilder policyBuilder, Func sleepDurationProvider) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } Action, TimeSpan> doNothing = (_, _) => { }; @@ -526,7 +579,9 @@ public static RetryPolicy WaitAndRetryForever(this PolicyBuild public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func sleepDurationProvider) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } Action, TimeSpan, Context> doNothing = (_, _, _) => { }; @@ -549,9 +604,14 @@ public static RetryPolicy WaitAndRetryForever(this PolicyBuild public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action, TimeSpan> onRetry) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } + if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return policyBuilder.WaitAndRetryForever( (retryCount, _) => sleepDurationProvider(retryCount), @@ -574,9 +634,14 @@ public static RetryPolicy WaitAndRetryForever(this PolicyBuild public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action, int, TimeSpan> onRetry) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } + if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return policyBuilder.WaitAndRetryForever( (retryCount, _, _) => sleepDurationProvider(retryCount), @@ -599,7 +664,9 @@ public static RetryPolicy WaitAndRetryForever(this PolicyBuild public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action, TimeSpan, Context> onRetry) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } return policyBuilder.WaitAndRetryForever( (i, _, ctx) => sleepDurationProvider(i, ctx), @@ -622,7 +689,9 @@ public static RetryPolicy WaitAndRetryForever(this PolicyBuild public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action, int, TimeSpan, Context> onRetry) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } return policyBuilder.WaitAndRetryForever( (i, _, ctx) => sleepDurationProvider(i, ctx), @@ -645,9 +714,14 @@ public static RetryPolicy WaitAndRetryForever(this PolicyBuild public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func, Context, TimeSpan> sleepDurationProvider, Action, TimeSpan, Context> onRetry) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } + if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return new RetryPolicy( policyBuilder, @@ -671,9 +745,14 @@ public static RetryPolicy WaitAndRetryForever(this PolicyBuild public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func, Context, TimeSpan> sleepDurationProvider, Action, int, TimeSpan, Context> onRetry) { if (sleepDurationProvider == null) + { throw new ArgumentNullException(nameof(sleepDurationProvider)); + } + if (onRetry == null) + { throw new ArgumentNullException(nameof(onRetry)); + } return new RetryPolicy( policyBuilder, diff --git a/src/Polly/Timeout/AsyncTimeoutSyntax.cs b/src/Polly/Timeout/AsyncTimeoutSyntax.cs index c268414569..5cb7153ac8 100644 --- a/src/Polly/Timeout/AsyncTimeoutSyntax.cs +++ b/src/Polly/Timeout/AsyncTimeoutSyntax.cs @@ -45,7 +45,9 @@ public static AsyncTimeoutPolicy TimeoutAsync(int seconds, TimeoutStrategy timeo { TimeoutValidator.ValidateSecondsTimeout(seconds); if (onTimeoutAsync == null) + { throw new ArgumentNullException(nameof(onTimeoutAsync)); + } return TimeoutAsync(_ => TimeSpan.FromSeconds(seconds), TimeoutStrategy.Optimistic, onTimeoutAsync); } @@ -62,9 +64,14 @@ public static AsyncTimeoutPolicy TimeoutAsync(int seconds, TimeoutStrategy timeo public static AsyncTimeoutPolicy TimeoutAsync(int seconds, Func onTimeoutAsync) { if (seconds <= 0) + { throw new ArgumentOutOfRangeException(nameof(seconds)); + } + if (onTimeoutAsync == null) + { throw new ArgumentNullException(nameof(onTimeoutAsync)); + } return TimeoutAsync(_ => TimeSpan.FromSeconds(seconds), TimeoutStrategy.Optimistic, onTimeoutAsync); } @@ -99,7 +106,9 @@ public static AsyncTimeoutPolicy TimeoutAsync(int seconds, TimeoutStrategy timeo public static AsyncTimeoutPolicy TimeoutAsync(int seconds, TimeoutStrategy timeoutStrategy, Func onTimeoutAsync) { if (seconds <= 0) + { throw new ArgumentOutOfRangeException(nameof(seconds)); + } return TimeoutAsync(_ => TimeSpan.FromSeconds(seconds), timeoutStrategy, onTimeoutAsync); } @@ -208,7 +217,9 @@ public static AsyncTimeoutPolicy TimeoutAsync(TimeSpan timeout, TimeoutStrategy public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider) { if (timeoutProvider == null) + { throw new ArgumentNullException(nameof(timeoutProvider)); + } Func doNothingAsync = (_, _, _, _) => TaskHelper.EmptyTask; return TimeoutAsync(_ => timeoutProvider(), TimeoutStrategy.Optimistic, doNothingAsync); @@ -224,7 +235,9 @@ public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider) public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider, TimeoutStrategy timeoutStrategy) { if (timeoutProvider == null) + { throw new ArgumentNullException(nameof(timeoutProvider)); + } Func doNothingAsync = (_, _, _, _) => TaskHelper.EmptyTask; return TimeoutAsync(_ => timeoutProvider(), timeoutStrategy, doNothingAsync); @@ -242,7 +255,9 @@ public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider, Ti public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider, Func onTimeoutAsync) { if (timeoutProvider == null) + { throw new ArgumentNullException(nameof(timeoutProvider)); + } return TimeoutAsync(_ => timeoutProvider(), TimeoutStrategy.Optimistic, onTimeoutAsync); } @@ -259,7 +274,9 @@ public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider, Fu public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider, Func onTimeoutAsync) { if (timeoutProvider == null) + { throw new ArgumentNullException(nameof(timeoutProvider)); + } return TimeoutAsync(_ => timeoutProvider(), TimeoutStrategy.Optimistic, onTimeoutAsync); } @@ -277,7 +294,9 @@ public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider, Fu public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider, TimeoutStrategy timeoutStrategy, Func onTimeoutAsync) { if (timeoutProvider == null) + { throw new ArgumentNullException(nameof(timeoutProvider)); + } return TimeoutAsync(_ => timeoutProvider(), timeoutStrategy, onTimeoutAsync); } @@ -295,7 +314,9 @@ public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider, Ti public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider, TimeoutStrategy timeoutStrategy, Func onTimeoutAsync) { if (timeoutProvider == null) + { throw new ArgumentNullException(nameof(timeoutProvider)); + } return TimeoutAsync(_ => timeoutProvider(), timeoutStrategy, onTimeoutAsync); } @@ -364,7 +385,9 @@ public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutPro public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider, TimeoutStrategy timeoutStrategy, Func onTimeoutAsync) { if (onTimeoutAsync == null) + { throw new ArgumentNullException(nameof(onTimeoutAsync)); + } return TimeoutAsync(timeoutProvider, timeoutStrategy, (ctx, timeout, task, _) => onTimeoutAsync(ctx, timeout, task)); } @@ -385,9 +408,14 @@ public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutPro Func onTimeoutAsync) { if (timeoutProvider == null) + { throw new ArgumentNullException(nameof(timeoutProvider)); + } + if (onTimeoutAsync == null) + { throw new ArgumentNullException(nameof(onTimeoutAsync)); + } return new AsyncTimeoutPolicy( timeoutProvider, diff --git a/src/Polly/Timeout/AsyncTimeoutTResultSyntax.cs b/src/Polly/Timeout/AsyncTimeoutTResultSyntax.cs index ff1986a2ad..b951a31d7c 100644 --- a/src/Polly/Timeout/AsyncTimeoutTResultSyntax.cs +++ b/src/Polly/Timeout/AsyncTimeoutTResultSyntax.cs @@ -63,7 +63,9 @@ public static AsyncTimeoutPolicy TimeoutAsync(int seconds, Fun public static AsyncTimeoutPolicy TimeoutAsync(int seconds, Func onTimeoutAsync) { if (seconds <= 0) + { throw new ArgumentOutOfRangeException(nameof(seconds)); + } return TimeoutAsync(_ => TimeSpan.FromSeconds(seconds), TimeoutStrategy.Optimistic, onTimeoutAsync); } @@ -100,7 +102,9 @@ public static AsyncTimeoutPolicy TimeoutAsync(int seconds, Tim public static AsyncTimeoutPolicy TimeoutAsync(int seconds, TimeoutStrategy timeoutStrategy, Func onTimeoutAsync) { if (seconds <= 0) + { throw new ArgumentOutOfRangeException(nameof(seconds)); + } return TimeoutAsync(_ => TimeSpan.FromSeconds(seconds), timeoutStrategy, onTimeoutAsync); } @@ -150,7 +154,9 @@ public static AsyncTimeoutPolicy TimeoutAsync(TimeSpan timeout { TimeoutValidator.ValidateTimeSpanTimeout(timeout); if (onTimeoutAsync == null) + { throw new ArgumentNullException(nameof(onTimeoutAsync)); + } return TimeoutAsync(_ => timeout, TimeoutStrategy.Optimistic, onTimeoutAsync); } @@ -168,9 +174,14 @@ public static AsyncTimeoutPolicy TimeoutAsync(TimeSpan timeout public static AsyncTimeoutPolicy TimeoutAsync(TimeSpan timeout, Func onTimeoutAsync) { if (timeout <= TimeSpan.Zero) + { throw new ArgumentOutOfRangeException(nameof(timeout)); + } + if (onTimeoutAsync == null) + { throw new ArgumentNullException(nameof(onTimeoutAsync)); + } return TimeoutAsync(_ => timeout, TimeoutStrategy.Optimistic, onTimeoutAsync); } @@ -206,7 +217,9 @@ public static AsyncTimeoutPolicy TimeoutAsync(TimeSpan timeout public static AsyncTimeoutPolicy TimeoutAsync(TimeSpan timeout, TimeoutStrategy timeoutStrategy, Func onTimeoutAsync) { if (timeout <= TimeSpan.Zero) + { throw new ArgumentOutOfRangeException(nameof(timeout)); + } return TimeoutAsync(_ => timeout, timeoutStrategy, onTimeoutAsync); } @@ -221,7 +234,9 @@ public static AsyncTimeoutPolicy TimeoutAsync(TimeSpan timeout public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider) { if (timeoutProvider == null) + { throw new ArgumentNullException(nameof(timeoutProvider)); + } Func doNothingAsync = (_, _, _, _) => Task.FromResult(default(TResult)); return TimeoutAsync(_ => timeoutProvider(), TimeoutStrategy.Optimistic, doNothingAsync); @@ -238,7 +253,9 @@ public static AsyncTimeoutPolicy TimeoutAsync(Func t public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider, TimeoutStrategy timeoutStrategy) { if (timeoutProvider == null) + { throw new ArgumentNullException(nameof(timeoutProvider)); + } Func doNothingAsync = (_, _, _, _) => Task.FromResult(default(TResult)); return TimeoutAsync(_ => timeoutProvider(), timeoutStrategy, doNothingAsync); @@ -257,7 +274,9 @@ public static AsyncTimeoutPolicy TimeoutAsync(Func t public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider, Func onTimeoutAsync) { if (timeoutProvider == null) + { throw new ArgumentNullException(nameof(timeoutProvider)); + } return TimeoutAsync(_ => timeoutProvider(), TimeoutStrategy.Optimistic, onTimeoutAsync); } @@ -275,7 +294,9 @@ public static AsyncTimeoutPolicy TimeoutAsync(Func t public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider, Func onTimeoutAsync) { if (timeoutProvider == null) + { throw new ArgumentNullException(nameof(timeoutProvider)); + } return TimeoutAsync(_ => timeoutProvider(), TimeoutStrategy.Optimistic, onTimeoutAsync); } @@ -294,7 +315,9 @@ public static AsyncTimeoutPolicy TimeoutAsync(Func t public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider, TimeoutStrategy timeoutStrategy, Func onTimeoutAsync) { if (timeoutProvider == null) + { throw new ArgumentNullException(nameof(timeoutProvider)); + } return TimeoutAsync(_ => timeoutProvider(), timeoutStrategy, onTimeoutAsync); } @@ -313,7 +336,9 @@ public static AsyncTimeoutPolicy TimeoutAsync(Func t public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider, TimeoutStrategy timeoutStrategy, Func onTimeoutAsync) { if (timeoutProvider == null) + { throw new ArgumentNullException(nameof(timeoutProvider)); + } return TimeoutAsync(_ => timeoutProvider(), timeoutStrategy, onTimeoutAsync); } @@ -385,7 +410,9 @@ public static AsyncTimeoutPolicy TimeoutAsync(Func TimeoutAsync(Func timeoutProvider, TimeoutStrategy timeoutStrategy, Func onTimeoutAsync) { if (onTimeoutAsync == null) + { throw new ArgumentNullException(nameof(onTimeoutAsync)); + } return TimeoutAsync(timeoutProvider, timeoutStrategy, (ctx, timeout, task, _) => onTimeoutAsync(ctx, timeout, task)); } @@ -404,9 +431,14 @@ public static AsyncTimeoutPolicy TimeoutAsync(Func TimeoutAsync(Func timeoutProvider, TimeoutStrategy timeoutStrategy, Func onTimeoutAsync) { if (timeoutProvider == null) + { throw new ArgumentNullException(nameof(timeoutProvider)); + } + if (onTimeoutAsync == null) + { throw new ArgumentNullException(nameof(onTimeoutAsync)); + } return new AsyncTimeoutPolicy( timeoutProvider, diff --git a/src/Polly/Timeout/TimeoutSyntax.cs b/src/Polly/Timeout/TimeoutSyntax.cs index e5e84ad903..11e93aef83 100644 --- a/src/Polly/Timeout/TimeoutSyntax.cs +++ b/src/Polly/Timeout/TimeoutSyntax.cs @@ -59,7 +59,9 @@ public static TimeoutPolicy Timeout(int seconds, Action public static TimeoutPolicy Timeout(int seconds, Action onTimeout) { if (seconds <= 0) + { throw new ArgumentOutOfRangeException(nameof(seconds)); + } return Timeout(_ => TimeSpan.FromSeconds(seconds), TimeoutStrategy.Optimistic, onTimeout); } @@ -94,7 +96,9 @@ public static TimeoutPolicy Timeout(int seconds, TimeoutStrategy timeoutStrategy public static TimeoutPolicy Timeout(int seconds, TimeoutStrategy timeoutStrategy, Action onTimeout) { if (seconds <= 0) + { throw new ArgumentOutOfRangeException(nameof(seconds)); + } return Timeout(_ => TimeSpan.FromSeconds(seconds), timeoutStrategy, onTimeout); } @@ -203,7 +207,9 @@ public static TimeoutPolicy Timeout(TimeSpan timeout, TimeoutStrategy timeoutStr public static TimeoutPolicy Timeout(Func timeoutProvider) { if (timeoutProvider == null) + { throw new ArgumentNullException(nameof(timeoutProvider)); + } Action doNothing = (_, _, _, _) => { }; return Timeout(_ => timeoutProvider(), TimeoutStrategy.Optimistic, doNothing); @@ -219,7 +225,9 @@ public static TimeoutPolicy Timeout(Func timeoutProvider) public static TimeoutPolicy Timeout(Func timeoutProvider, TimeoutStrategy timeoutStrategy) { if (timeoutProvider == null) + { throw new ArgumentNullException(nameof(timeoutProvider)); + } Action doNothing = (_, _, _, _) => { }; return Timeout(_ => timeoutProvider(), timeoutStrategy, doNothing); @@ -237,7 +245,9 @@ public static TimeoutPolicy Timeout(Func timeoutProvider, TimeoutStrat public static TimeoutPolicy Timeout(Func timeoutProvider, Action onTimeout) { if (timeoutProvider == null) + { throw new ArgumentNullException(nameof(timeoutProvider)); + } return Timeout(_ => timeoutProvider(), TimeoutStrategy.Optimistic, onTimeout); } @@ -254,7 +264,9 @@ public static TimeoutPolicy Timeout(Func timeoutProvider, Action timeoutProvider, Action onTimeout) { if (timeoutProvider == null) + { throw new ArgumentNullException(nameof(timeoutProvider)); + } return Timeout(_ => timeoutProvider(), TimeoutStrategy.Optimistic, onTimeout); } @@ -272,7 +284,9 @@ public static TimeoutPolicy Timeout(Func timeoutProvider, Action timeoutProvider, TimeoutStrategy timeoutStrategy, Action onTimeout) { if (timeoutProvider == null) + { throw new ArgumentNullException(nameof(timeoutProvider)); + } return Timeout(_ => timeoutProvider(), timeoutStrategy, onTimeout); } @@ -290,7 +304,9 @@ public static TimeoutPolicy Timeout(Func timeoutProvider, TimeoutStrat public static TimeoutPolicy Timeout(Func timeoutProvider, TimeoutStrategy timeoutStrategy, Action onTimeout) { if (timeoutProvider == null) + { throw new ArgumentNullException(nameof(timeoutProvider)); + } return Timeout(_ => timeoutProvider(), timeoutStrategy, onTimeout); } @@ -357,7 +373,9 @@ public static TimeoutPolicy Timeout(Func timeoutProvider, Tim public static TimeoutPolicy Timeout(Func timeoutProvider, TimeoutStrategy timeoutStrategy, Action onTimeout) { if (onTimeout == null) + { throw new ArgumentNullException(nameof(onTimeout)); + } return Timeout(timeoutProvider, timeoutStrategy, (ctx, timeout, task, _) => onTimeout(ctx, timeout, task)); } @@ -378,9 +396,14 @@ public static TimeoutPolicy Timeout(Func timeoutProvider, Tim Action onTimeout) { if (timeoutProvider == null) + { throw new ArgumentNullException(nameof(timeoutProvider)); + } + if (onTimeout == null) + { throw new ArgumentNullException(nameof(onTimeout)); + } return new TimeoutPolicy( timeoutProvider, diff --git a/src/Polly/Timeout/TimeoutTResultSyntax.cs b/src/Polly/Timeout/TimeoutTResultSyntax.cs index cab88febc5..4bac3b4d8b 100644 --- a/src/Polly/Timeout/TimeoutTResultSyntax.cs +++ b/src/Polly/Timeout/TimeoutTResultSyntax.cs @@ -62,7 +62,9 @@ public static TimeoutPolicy Timeout(int seconds, Action Timeout(int seconds, Action onTimeout) { if (seconds <= 0) + { throw new ArgumentOutOfRangeException(nameof(seconds)); + } return Timeout(_ => TimeSpan.FromSeconds(seconds), TimeoutStrategy.Optimistic, onTimeout); } @@ -99,7 +101,9 @@ public static TimeoutPolicy Timeout(int seconds, TimeoutStrate public static TimeoutPolicy Timeout(int seconds, TimeoutStrategy timeoutStrategy, Action onTimeout) { if (seconds <= 0) + { throw new ArgumentOutOfRangeException(nameof(seconds)); + } return Timeout(_ => TimeSpan.FromSeconds(seconds), timeoutStrategy, onTimeout); } @@ -211,7 +215,9 @@ public static TimeoutPolicy Timeout(TimeSpan timeout, TimeoutS public static TimeoutPolicy Timeout(Func timeoutProvider) { if (timeoutProvider == null) + { throw new ArgumentNullException(nameof(timeoutProvider)); + } Action doNothing = (_, _, _, _) => { }; return Timeout(_ => timeoutProvider(), TimeoutStrategy.Optimistic, doNothing); @@ -228,7 +234,9 @@ public static TimeoutPolicy Timeout(Func timeoutProv public static TimeoutPolicy Timeout(Func timeoutProvider, TimeoutStrategy timeoutStrategy) { if (timeoutProvider == null) + { throw new ArgumentNullException(nameof(timeoutProvider)); + } Action doNothing = (_, _, _, _) => { }; return Timeout(_ => timeoutProvider(), timeoutStrategy, doNothing); @@ -247,7 +255,9 @@ public static TimeoutPolicy Timeout(Func timeoutProv public static TimeoutPolicy Timeout(Func timeoutProvider, Action onTimeout) { if (timeoutProvider == null) + { throw new ArgumentNullException(nameof(timeoutProvider)); + } return Timeout(_ => timeoutProvider(), TimeoutStrategy.Optimistic, onTimeout); } @@ -265,7 +275,9 @@ public static TimeoutPolicy Timeout(Func timeoutProv public static TimeoutPolicy Timeout(Func timeoutProvider, Action onTimeout) { if (timeoutProvider == null) + { throw new ArgumentNullException(nameof(timeoutProvider)); + } return Timeout(_ => timeoutProvider(), TimeoutStrategy.Optimistic, onTimeout); } @@ -284,7 +296,9 @@ public static TimeoutPolicy Timeout(Func timeoutProv public static TimeoutPolicy Timeout(Func timeoutProvider, TimeoutStrategy timeoutStrategy, Action onTimeout) { if (timeoutProvider == null) + { throw new ArgumentNullException(nameof(timeoutProvider)); + } return Timeout(_ => timeoutProvider(), timeoutStrategy, onTimeout); } @@ -303,7 +317,9 @@ public static TimeoutPolicy Timeout(Func timeoutProv public static TimeoutPolicy Timeout(Func timeoutProvider, TimeoutStrategy timeoutStrategy, Action onTimeout) { if (timeoutProvider == null) + { throw new ArgumentNullException(nameof(timeoutProvider)); + } return Timeout(_ => timeoutProvider(), timeoutStrategy, onTimeout); } @@ -375,7 +391,9 @@ public static TimeoutPolicy Timeout(Func ti public static TimeoutPolicy Timeout(Func timeoutProvider, TimeoutStrategy timeoutStrategy, Action onTimeout) { if (onTimeout == null) + { throw new ArgumentNullException(nameof(onTimeout)); + } return Timeout(timeoutProvider, timeoutStrategy, (ctx, timeout, task, _) => onTimeout(ctx, timeout, task)); } @@ -397,9 +415,14 @@ public static TimeoutPolicy Timeout(Func ti Action onTimeout) { if (timeoutProvider == null) + { throw new ArgumentNullException(nameof(timeoutProvider)); + } + if (onTimeout == null) + { throw new ArgumentNullException(nameof(onTimeout)); + } return new TimeoutPolicy( timeoutProvider, diff --git a/src/Polly/Timeout/TimeoutValidator.cs b/src/Polly/Timeout/TimeoutValidator.cs index d240e983e4..ef0747eb98 100644 --- a/src/Polly/Timeout/TimeoutValidator.cs +++ b/src/Polly/Timeout/TimeoutValidator.cs @@ -5,7 +5,9 @@ internal static class TimeoutValidator internal static void ValidateSecondsTimeout(int seconds) { if (seconds <= 0) + { throw new ArgumentOutOfRangeException(nameof(seconds)); + } } internal static void ValidateTimeSpanTimeout(TimeSpan timeout) diff --git a/src/Polly/Utilities/SystemClock.cs b/src/Polly/Utilities/SystemClock.cs index 11a6c87143..abcadc847e 100644 --- a/src/Polly/Utilities/SystemClock.cs +++ b/src/Polly/Utilities/SystemClock.cs @@ -13,7 +13,9 @@ public static class SystemClock public static Action Sleep = (timeSpan, cancellationToken) => { if (cancellationToken.WaitHandle.WaitOne(timeSpan)) + { cancellationToken.ThrowIfCancellationRequested(); + } }; /// @@ -49,7 +51,9 @@ public static void Reset() Sleep = (timeSpan, cancellationToken) => { if (cancellationToken.WaitHandle.WaitOne(timeSpan)) + { cancellationToken.ThrowIfCancellationRequested(); + } }; SleepAsync = Task.Delay; diff --git a/src/Polly/Wrap/AsyncPolicyWrap.ContextAndKeys.cs b/src/Polly/Wrap/AsyncPolicyWrap.ContextAndKeys.cs index e8f62be902..55c3e43523 100644 --- a/src/Polly/Wrap/AsyncPolicyWrap.ContextAndKeys.cs +++ b/src/Polly/Wrap/AsyncPolicyWrap.ContextAndKeys.cs @@ -14,7 +14,9 @@ internal override void SetPolicyContext(Context executionContext, out string pri priorPolicyKey = executionContext.PolicyKey; if (executionContext.PolicyWrapKey == null) + { executionContext.PolicyWrapKey = PolicyKey; + } base.SetPolicyContext(executionContext, out _, out _); } @@ -34,7 +36,9 @@ internal override void SetPolicyContext(Context executionContext, out string pri priorPolicyKey = executionContext.PolicyKey; if (executionContext.PolicyWrapKey == null) + { executionContext.PolicyWrapKey = PolicyKey; + } base.SetPolicyContext(executionContext, out _, out _); } diff --git a/src/Polly/Wrap/AsyncPolicyWrapSyntax.cs b/src/Polly/Wrap/AsyncPolicyWrapSyntax.cs index cbc0cdd686..2e62bd01f3 100644 --- a/src/Polly/Wrap/AsyncPolicyWrapSyntax.cs +++ b/src/Polly/Wrap/AsyncPolicyWrapSyntax.cs @@ -14,7 +14,10 @@ public static class IAsyncPolicyPolicyWrapExtensions public static AsyncPolicyWrap WrapAsync(this IAsyncPolicy outerPolicy, IAsyncPolicy innerPolicy) { if (outerPolicy == null) + { throw new ArgumentNullException(nameof(outerPolicy)); + } + return ((AsyncPolicy)outerPolicy).WrapAsync(innerPolicy); } @@ -28,7 +31,10 @@ public static AsyncPolicyWrap WrapAsync(this IAsyncPolicy outerPolicy, IAsyncPol public static AsyncPolicyWrap WrapAsync(this IAsyncPolicy outerPolicy, IAsyncPolicy innerPolicy) { if (outerPolicy == null) + { throw new ArgumentNullException(nameof(outerPolicy)); + } + return ((AsyncPolicy)outerPolicy).WrapAsync(innerPolicy); } @@ -42,7 +48,10 @@ public static AsyncPolicyWrap WrapAsync(this IAsyncPolicy oute public static AsyncPolicyWrap WrapAsync(this IAsyncPolicy outerPolicy, IAsyncPolicy innerPolicy) { if (outerPolicy == null) + { throw new ArgumentNullException(nameof(outerPolicy)); + } + return ((AsyncPolicy)outerPolicy).WrapAsync(innerPolicy); } @@ -56,7 +65,10 @@ public static AsyncPolicyWrap WrapAsync(this IAsyncPolicy WrapAsync(this IAsyncPolicy outerPolicy, IAsyncPolicy innerPolicy) { if (outerPolicy == null) + { throw new ArgumentNullException(nameof(outerPolicy)); + } + return ((AsyncPolicy)outerPolicy).WrapAsync(innerPolicy); } } @@ -71,7 +83,9 @@ public partial class AsyncPolicy public AsyncPolicyWrap WrapAsync(IAsyncPolicy innerPolicy) { if (innerPolicy == null) + { throw new ArgumentNullException(nameof(innerPolicy)); + } return new AsyncPolicyWrap( this, @@ -87,7 +101,9 @@ public AsyncPolicyWrap WrapAsync(IAsyncPolicy innerPolicy) public AsyncPolicyWrap WrapAsync(IAsyncPolicy innerPolicy) { if (innerPolicy == null) + { throw new ArgumentNullException(nameof(innerPolicy)); + } return new AsyncPolicyWrap( this, @@ -105,7 +121,9 @@ public partial class AsyncPolicy public AsyncPolicyWrap WrapAsync(IAsyncPolicy innerPolicy) { if (innerPolicy == null) + { throw new ArgumentNullException(nameof(innerPolicy)); + } return new AsyncPolicyWrap( this, @@ -120,7 +138,9 @@ public AsyncPolicyWrap WrapAsync(IAsyncPolicy innerPolicy) public AsyncPolicyWrap WrapAsync(IAsyncPolicy innerPolicy) { if (innerPolicy == null) + { throw new ArgumentNullException(nameof(innerPolicy)); + } return new AsyncPolicyWrap( this, diff --git a/src/Polly/Wrap/IPolicyWrapExtension.cs b/src/Polly/Wrap/IPolicyWrapExtension.cs index cfe7d6f210..84f653993c 100644 --- a/src/Polly/Wrap/IPolicyWrapExtension.cs +++ b/src/Polly/Wrap/IPolicyWrapExtension.cs @@ -48,7 +48,9 @@ public static IEnumerable GetPolicies(this IPolicyWrap policyWrap) public static IEnumerable GetPolicies(this IPolicyWrap policyWrap, Func filter) { if (filter == null) + { throw new ArgumentNullException(nameof(filter)); + } return policyWrap.GetPolicies().OfType().Where(filter); } @@ -74,7 +76,9 @@ public static IEnumerable GetPolicies(this IPolicyWrap policyW public static TPolicy GetPolicy(this IPolicyWrap policyWrap, Func filter) { if (filter == null) + { throw new ArgumentNullException(nameof(filter)); + } return policyWrap.GetPolicies().OfType().SingleOrDefault(filter); } diff --git a/src/Polly/Wrap/PolicyWrap.ContextAndKeys.cs b/src/Polly/Wrap/PolicyWrap.ContextAndKeys.cs index 5e02d456d3..92c503dfbb 100644 --- a/src/Polly/Wrap/PolicyWrap.ContextAndKeys.cs +++ b/src/Polly/Wrap/PolicyWrap.ContextAndKeys.cs @@ -14,7 +14,9 @@ internal override void SetPolicyContext(Context executionContext, out string pri priorPolicyKey = executionContext.PolicyKey; if (executionContext.PolicyWrapKey == null) + { executionContext.PolicyWrapKey = PolicyKey; + } base.SetPolicyContext(executionContext, out _, out _); } @@ -34,7 +36,9 @@ internal override void SetPolicyContext(Context executionContext, out string pri priorPolicyKey = executionContext.PolicyKey; if (executionContext.PolicyWrapKey == null) + { executionContext.PolicyWrapKey = PolicyKey; + } base.SetPolicyContext(executionContext, out _, out _); } diff --git a/src/Polly/Wrap/PolicyWrapSyntax.cs b/src/Polly/Wrap/PolicyWrapSyntax.cs index e44fea20e3..08b35c73fb 100644 --- a/src/Polly/Wrap/PolicyWrapSyntax.cs +++ b/src/Polly/Wrap/PolicyWrapSyntax.cs @@ -14,7 +14,10 @@ public static class ISyncPolicyPolicyWrapExtensions public static PolicyWrap Wrap(this ISyncPolicy outerPolicy, ISyncPolicy innerPolicy) { if (outerPolicy == null) + { throw new ArgumentNullException(nameof(outerPolicy)); + } + return ((Policy)outerPolicy).Wrap(innerPolicy); } @@ -28,7 +31,10 @@ public static PolicyWrap Wrap(this ISyncPolicy outerPolicy, ISyncPolicy innerPol public static PolicyWrap Wrap(this ISyncPolicy outerPolicy, ISyncPolicy innerPolicy) { if (outerPolicy == null) + { throw new ArgumentNullException(nameof(outerPolicy)); + } + return ((Policy)outerPolicy).Wrap(innerPolicy); } @@ -42,7 +48,10 @@ public static PolicyWrap Wrap(this ISyncPolicy outerPolicy, IS public static PolicyWrap Wrap(this ISyncPolicy outerPolicy, ISyncPolicy innerPolicy) { if (outerPolicy == null) + { throw new ArgumentNullException(nameof(outerPolicy)); + } + return ((Policy)outerPolicy).Wrap(innerPolicy); } @@ -56,7 +65,10 @@ public static PolicyWrap Wrap(this ISyncPolicy outerP public static PolicyWrap Wrap(this ISyncPolicy outerPolicy, ISyncPolicy innerPolicy) { if (outerPolicy == null) + { throw new ArgumentNullException(nameof(outerPolicy)); + } + return ((Policy)outerPolicy).Wrap(innerPolicy); } } @@ -71,7 +83,9 @@ public partial class Policy public PolicyWrap Wrap(ISyncPolicy innerPolicy) { if (innerPolicy == null) + { throw new ArgumentNullException(nameof(innerPolicy)); + } return new PolicyWrap( this, @@ -87,7 +101,9 @@ public PolicyWrap Wrap(ISyncPolicy innerPolicy) public PolicyWrap Wrap(ISyncPolicy innerPolicy) { if (innerPolicy == null) + { throw new ArgumentNullException(nameof(innerPolicy)); + } return new PolicyWrap( this, @@ -105,7 +121,9 @@ public partial class Policy public PolicyWrap Wrap(ISyncPolicy innerPolicy) { if (innerPolicy == null) + { throw new ArgumentNullException(nameof(innerPolicy)); + } return new PolicyWrap( this, @@ -120,7 +138,9 @@ public PolicyWrap Wrap(ISyncPolicy innerPolicy) public PolicyWrap Wrap(ISyncPolicy innerPolicy) { if (innerPolicy == null) + { throw new ArgumentNullException(nameof(innerPolicy)); + } return new PolicyWrap( this,