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

EnsureSuccess for PolicyResult #1238

Closed
michalczerwinski opened this issue May 30, 2023 · 4 comments
Closed

EnsureSuccess for PolicyResult #1238

michalczerwinski opened this issue May 30, 2023 · 4 comments
Labels
v8 Issues related to the new version 8 of the Polly library.
Milestone

Comments

@michalczerwinski
Copy link

When replacing try/catch block with Polly equivalent one has to manually check the result and make sure there is no exception:

    var policy = Policy
        .Handle<InvalidDataException>()
        .RetryAsync(3, (e, r) => Console.WriteLine($"Retry no {r}"));

    var policyResult = await policy.ExecuteAndCaptureAsync(async () =>{ /*...*/});

    if (policyResult.Outcome == OutcomeType.Successful)
    {
        return policyResult.Result;
    }
    else throw new SomeException(policyResult.FinalException);

It would be nice to be able to do this instead:

   policyResult.EnsureSuccess();
   return policyResult.Result;
@martincostello
Copy link
Member

@martintmk Is there anything that would essentially fulfil this request in v8?

@martintmk
Copy link
Contributor

There is low-level method in V8 to retrieve the outcome:
https://github.com/App-vNext/Polly/blob/main/src/Polly.Core/ResilienceStrategy.TResult.Async.cs#L104

I think we can add this helper method to Outcome in V8. It will be useful.

@PeterCsalaHbo
Copy link

This can be easily done via extension method(s):

public static void EnsureSuccess(this PolicyResult result) 
{
     if (result.Outcome == OutcomeType.Failure) 
        throw new SomeException(result.FinalException)
}

public static void EnsureSuccess<TResult>(this PolicyResult<TResult> result) 
{
     if (result.Outcome == OutcomeType.Failure) 
        throw new SomeException(result.FinalException)
}

@martintmk
Copy link
Contributor

The V8 Outcome<T> now contains the EnsureSuccess. For V7 you can use the @PeterCsalaHbo suggestion.

@martincostello martincostello added the v8 Issues related to the new version 8 of the Polly library. label Jun 19, 2023
@martincostello martincostello added this to the v8.0.0 milestone Jun 19, 2023
@martincostello martincostello moved this to Done in Polly v8 Sep 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v8 Issues related to the new version 8 of the Polly library.
Projects
No open projects
Status: Done
Development

No branches or pull requests

4 participants