question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do you set up a HandleResult<Result<T>> for any T?

See original GitHub issue

I understand the requirements of having a strongly-typed HandleResult<T> and only having one T per policy, however I’m looking to do something with a library that returns a generic wrapped result, with the error message contained within the wrapper.

Example (from memory code, may contain issues):

interface IWebCallResult<T> {
     public bool Success { get; }
     public Error? Error { get; }  // Will be filled in if there's an error.
     public T? Data { get; } // Will be filled in if there's no error
}

I’d like to do something like:

TimoutPolicy = Policy
    .HandleResult<IWebCallResult>(r => r.Success == false && (r.Error?.Message.Contains("timed out") ?? false))
    .RetryAsync();

In other words, if the success is false and the error is timed out, then retry, otherwise fail. I have about 40 of these, so writing loads of boilerplate all saying exactly the same really feels DRY. Is there a way to handle this in Polly?

As it stands, I’ve got to do something like:

StringTimeoutPolicy = Policy
    .HandleResult<IWebCallResult<string>>(r => r.Success == false && (r.Error?.Message.Contains("timed out") ?? false))
    .RetryAsync();
ObjectTimeoutPolicy = Policy
    .HandleResult<IWebCallResult<object>>(r => r.Success == false && (r.Error?.Message.Contains("timed out") ?? false))
    .RetryAsync();
ThingTimeoutPolicy = Policy
    .HandleResult<IWebCallResult<Thing>>(r => r.Success == false && (r.Error?.Message.Contains("timed out") ?? false))
    .RetryAsync();

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
martintmkcommented, Apr 25, 2023

@LazerFX , @orty

This issue is addressed in v8 and will be closed in once v8 is released.

See the example: https://github.com/App-vNext/Polly/blob/v8-fixed-issues/src/Polly.Core.Tests/Issues/IssuesTests.HandleMultipleResults_898.cs

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to build a networking in Swift #195 - onmyway133/blog
Understanding HTML Form Encoding: URL Encoded and Multipart Forms ... .failure(error)) } public func handle(result: Result<T, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found