Skip to content

feat: Add Recover with async extensions and tests#151

Open
mark-pro wants to merge 2 commits intoamantinband:mainfrom
mark-pro:if-fail-then
Open

feat: Add Recover with async extensions and tests#151
mark-pro wants to merge 2 commits intoamantinband:mainfrom
mark-pro:if-fail-then

Conversation

@mark-pro
Copy link

Currently, there is no way to provide alternative branching logic when there is a failure. That is, there is no way to recover. Think cache miss -> call db.

To address the gap a new "Recover" feature can be added to the ErrorOr library, allowing consumers to handle error states by providing recovery logic, both synchronously and asynchronously. The changes include the implementation of Recover and RecoverAsync methods, extension methods for use with Task<ErrorOr<TValue>>, comprehensive documentation updates, and unit tests to ensure correct behavior.

cache.GetUser(userId).Recover(_ => db.GetUser(userId).Then(cache.Save));

In more complex scenarios where the errors need to be inspected for the path the consumer can do the following:

cache.GetUser(userId)
    .Recover(errors => errors.Any(error => error.Type == ErrorType.NotFound)
        ? db.GetUser(userId).Then(cache.Save)
        : errors)

New Recover Feature Implementation:

  • Added Recover and RecoverAsync methods to ErrorOr<TValue>, allowing users to recover from errors by providing custom logic to handle error states.
  • Introduced extension methods for Task<ErrorOr<TValue>> to support fluent async recovery with Recover and RecoverAsync.

Documentation Updates:

  • Updated README.md to document the new Recover and RecoverAsync methods, including usage examples and updates to feature-mixing sections. [1] [2]

Testing:

  • Added unit tests in ErrorOr.RecoverTests.cs to verify the behavior of the synchronous Recover method and its extension.
  • Added unit tests in ErrorOr.RecoverAsyncTests.cs to verify the behavior of the asynchronous RecoverAsync method and its extension.

@mark-pro
Copy link
Author

mark-pro commented Feb 18, 2026

The new Recover feature is useful as we use this library actively and often have to recreate the extension in several times over. I am open to suggestions and comments or name changes to Recover.

@mark-pro mark-pro changed the title Add Recover APIs with async extensions and tests feat: Add Recover APIs with async extensions and tests Feb 19, 2026
@mark-pro mark-pro changed the title feat: Add Recover APIs with async extensions and tests feat: Add Recover with async extensions and tests Feb 19, 2026
@mark-pro
Copy link
Author

After looking at the open PRs I noticed there is a very similar PR (#149) to extend Else to do the same functionality as the Recover function here. This PR can be closed if extending Else is more desirable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant