Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix S4457 warning #2202

Merged
merged 2 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 75 additions & 38 deletions src/Polly/AsyncPolicy.ExecuteOverloads.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,23 +100,14 @@ public Task ExecuteAsync(Func<Context, CancellationToken, Task> action, IDiction
/// <exception cref="InvalidOperationException">Please use asynchronous-defined policies when calling asynchronous ExecuteAsync (and similar) methods.</exception>
/// <returns>A <see cref="Task" /> which completes when <see cref="AsyncPolicy"/> is registered.</returns>
[DebuggerStepThrough]
public async Task ExecuteAsync(Func<Context, CancellationToken, Task> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext)
public Task ExecuteAsync(Func<Context, CancellationToken, Task> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

SetPolicyContext(context, out string priorPolicyWrapKey, out string priorPolicyKey);

try
{
await ImplementationAsync(action, context, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext);
}
finally
{
PolicyBase.RestorePolicyContext(context, priorPolicyWrapKey, priorPolicyKey);
}
return ExecuteInternalAsync(action, context, continueOnCapturedContext, cancellationToken);
}

#region Overloads method-generic in TResult
Expand Down Expand Up @@ -226,23 +217,14 @@ public Task<TResult> ExecuteAsync<TResult>(Func<Context, CancellationToken, Task
/// <returns>The value returned by the action.</returns>
/// <exception cref="InvalidOperationException">Please use asynchronous-defined policies when calling asynchronous ExecuteAsync (and similar) methods.</exception>
[DebuggerStepThrough]
public async Task<TResult> ExecuteAsync<TResult>(Func<Context, CancellationToken, Task<TResult>> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext)
public Task<TResult> ExecuteAsync<TResult>(Func<Context, CancellationToken, Task<TResult>> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

SetPolicyContext(context, out string priorPolicyWrapKey, out string priorPolicyKey);

try
{
return await ImplementationAsync(action, context, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext);
}
finally
{
PolicyBase.RestorePolicyContext(context, priorPolicyWrapKey, priorPolicyKey);
}
return ExecuteInternalAsync(action, context, continueOnCapturedContext, cancellationToken);
}

#endregion
Expand Down Expand Up @@ -349,22 +331,14 @@ public Task<PolicyResult> ExecuteAndCaptureAsync(Func<Context, CancellationToken
/// <exception cref="InvalidOperationException">Please use asynchronous-defined policies when calling asynchronous ExecuteAsync (and similar) methods.</exception>
/// <returns>The captured result.</returns>
[DebuggerStepThrough]
public async Task<PolicyResult> ExecuteAndCaptureAsync(Func<Context, CancellationToken, Task> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext)
public Task<PolicyResult> ExecuteAndCaptureAsync(Func<Context, CancellationToken, Task> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

try
{
await ExecuteAsync(action, context, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext);
return PolicyResult.Successful(context);
}
catch (Exception exception)
{
return PolicyResult.Failure(exception, GetExceptionType(ExceptionPredicates, exception), context);
}
return ExecuteAndCaptureInternalAsync(action, context, continueOnCapturedContext, cancellationToken);
}

#region Overloads method-generic in TResult
Expand Down Expand Up @@ -476,25 +450,88 @@ public Task<PolicyResult<TResult>> ExecuteAndCaptureAsync<TResult>(Func<Context,
/// <returns>The captured result.</returns>
/// <exception cref="InvalidOperationException">Please use asynchronous-defined policies when calling asynchronous ExecuteAsync (and similar) methods.</exception>
[DebuggerStepThrough]
public async Task<PolicyResult<TResult>> ExecuteAndCaptureAsync<TResult>(Func<Context, CancellationToken, Task<TResult>> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext)
public Task<PolicyResult<TResult>> ExecuteAndCaptureAsync<TResult>(
Func<Context, CancellationToken, Task<TResult>> action,
Context context,
CancellationToken cancellationToken,
bool continueOnCapturedContext)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

return ExecuteAndCaptureInternalAsync(action, context, continueOnCapturedContext, cancellationToken);
}

#endregion

#endregion

private async Task ExecuteInternalAsync(Func<Context, CancellationToken, Task> action, Context context, bool continueOnCapturedContext, CancellationToken cancellationToken)
{
SetPolicyContext(context, out string priorPolicyWrapKey, out string priorPolicyKey);

try
{
await ImplementationAsync(action, context, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext);
}
finally
{
PolicyBase.RestorePolicyContext(context, priorPolicyWrapKey, priorPolicyKey);
}
}

private async Task<TResult> ExecuteInternalAsync<TResult>(
Func<Context, CancellationToken, Task<TResult>> action,
Context context,
bool continueOnCapturedContext,
CancellationToken cancellationToken)
{
SetPolicyContext(context, out string priorPolicyWrapKey, out string priorPolicyKey);

try
{
return await ImplementationAsync(action, context, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext);
}
finally
{
PolicyBase.RestorePolicyContext(context, priorPolicyWrapKey, priorPolicyKey);
}
}

private async Task<PolicyResult> ExecuteAndCaptureInternalAsync(
Func<Context, CancellationToken, Task> action,
Context context,
bool continueOnCapturedContext,
CancellationToken cancellationToken)
{
try
{
await ExecuteAsync(action, context, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext);
return PolicyResult.Successful(context);
}
catch (Exception exception)
{
return PolicyResult.Failure(exception, GetExceptionType(ExceptionPredicates, exception), context);
}
}

private async Task<PolicyResult<TResult>> ExecuteAndCaptureInternalAsync<TResult>(
Func<Context, CancellationToken, Task<TResult>> action,
Context context,
bool continueOnCapturedContext,
CancellationToken cancellationToken)
{
try
{
return PolicyResult<TResult>.Successful(
await ExecuteAsync(action, context, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext), context);
await ExecuteAsync(action, context, cancellationToken, continueOnCapturedContext)
.ConfigureAwait(continueOnCapturedContext), context);
}
catch (Exception exception)
{
return PolicyResult<TResult>.Failure(exception, GetExceptionType(ExceptionPredicates, exception), context);
}
}

#endregion

#endregion
}
46 changes: 32 additions & 14 deletions src/Polly/AsyncPolicy.TResult.ExecuteOverloads.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,23 +100,14 @@ public Task<TResult> ExecuteAsync(Func<Context, CancellationToken, Task<TResult>
/// <returns>The value returned by the action.</returns>
/// <exception cref="InvalidOperationException">Please use asynchronous-defined policies when calling asynchronous ExecuteAsync (and similar) methods.</exception>
[DebuggerStepThrough]
public async Task<TResult> ExecuteAsync(Func<Context, CancellationToken, Task<TResult>> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext)
public Task<TResult> ExecuteAsync(Func<Context, CancellationToken, Task<TResult>> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

SetPolicyContext(context, out string priorPolicyWrapKey, out string priorPolicyKey);

try
{
return await ImplementationAsync(action, context, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext);
}
finally
{
PolicyBase.RestorePolicyContext(context, priorPolicyWrapKey, priorPolicyKey);
}
return ExecuteInternalAsync(action, context, continueOnCapturedContext, cancellationToken);
}

#endregion
Expand Down Expand Up @@ -221,13 +212,42 @@ public Task<PolicyResult<TResult>> ExecuteAndCaptureAsync(Func<Context, Cancella
/// <returns>The captured result.</returns>
/// <exception cref="InvalidOperationException">Please use asynchronous-defined policies when calling asynchronous ExecuteAsync (and similar) methods.</exception>
[DebuggerStepThrough]
public async Task<PolicyResult<TResult>> ExecuteAndCaptureAsync(Func<Context, CancellationToken, Task<TResult>> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext)
public Task<PolicyResult<TResult>> ExecuteAndCaptureAsync(Func<Context, CancellationToken, Task<TResult>> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

return ExecuteAndCaptureInternalAsync(action, context, continueOnCapturedContext, cancellationToken);
}

#endregion

private async Task<TResult> ExecuteInternalAsync(
Func<Context, CancellationToken, Task<TResult>> action,
Context context,
bool continueOnCapturedContext,
CancellationToken cancellationToken)
{
SetPolicyContext(context, out string priorPolicyWrapKey, out string priorPolicyKey);

try
{
return await ImplementationAsync(action, context, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext);
}
finally
{
PolicyBase.RestorePolicyContext(context, priorPolicyWrapKey, priorPolicyKey);
}
}

private async Task<PolicyResult<TResult>> ExecuteAndCaptureInternalAsync(
Func<Context, CancellationToken, Task<TResult>> action,
Context context,
bool continueOnCapturedContext,
CancellationToken cancellationToken)
{
try
{
TResult result = await ExecuteAsync(action, context, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext);
Expand All @@ -244,6 +264,4 @@ public async Task<PolicyResult<TResult>> ExecuteAndCaptureAsync(Func<Context, Ca
return PolicyResult<TResult>.Failure(exception, GetExceptionType(ExceptionPredicates, exception), context);
}
}

#endregion
}
2 changes: 1 addition & 1 deletion src/Polly/Polly.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<MutationScore>70</MutationScore>
<IncludePollyUsings>true</IncludePollyUsings>
<NoWarn>$(NoWarn);CA1010;CA1031;CA1051;CA1062;CA1063;CA1064;CA1710;CA1716;CA1724;CA1805;CA1815;CA1816;CA2211</NoWarn>
<NoWarn>$(NoWarn);S2223;S3215;S3246;S3971;S4039;S4457</NoWarn>
<NoWarn>$(NoWarn);S2223;S3215;S3246;S3971;S4039</NoWarn>
<!--Public API Analyzers: We do not need to fix these as it would break compatibility with released Polly versions-->
<NoWarn>$(NoWarn);RS0037;</NoWarn>
</PropertyGroup>
Expand Down