Namespaces
Variants

std::execution::let_error

From cppreference.com
 
 
Execution control library
Concepts
Utility components
Execution contexts
Execution domains
Forward progress guarantee
Environments
    
    
    
Queries
Completion signatures
Pipeable sender adaptors
Coroutine utility
 
Defined in header <execution>
struct let_error_t { /* unspecified */ };
(1) (since C++26)
inline constexpr let_error_t let_error{};
(2) (since C++26)
(customization point object)
Call signature
execution::sender auto let_error( execution::sender auto sndr,
                                  /*movable-value*/ auto f );
(3) (since C++26)

Recovers from a failure with more asynchronous work: when sndr completes with an error, f is called with the error, and f returns a sender. That sender is started, and its completion becomes the completion of the returned sender.

This is how a pipeline handles an error by doing something asynchronous: retrying the operation, switching to a backup, cleaning up with an asynchronous call. It is the error-channel counterpart of let_value, and the asynchronous counterpart of upon_error, whose function returns a plain value.

let_error is a pipeable sender adaptor: sndr | let_error(f) is equivalent to let_error(sndr, f).

Parameters

sndr - the sender whose errors are passed to f
f - a function (or other callable) called with the error sent by sndr, which returns a sender

Return value

A sender that behaves like sndr until sndr completes with an error. Then:

  • f is called with the error, and the sender it returns is started.
  • The returned sender completes the way that inner sender does: with its values, with its error, or stopped. If the inner sender succeeds, the failure is gone.
  • If f throws, the returned sender completes with the exception as an error (set_error with a std::exception_ptr).

Value and stopped completions of sndr are forwarded unchanged: f is not called for them. Use let_value and let_stopped to continue with a sender after those.

let_error(sndr, f) is ill-formed if sndr does not satisfy execution::sender or f cannot be copied or moved into the sender (it must be a movable value, and not an array). It is also diagnosed at compile time if, for some error completion of sndr, f cannot be called with the error as an lvalue or does not return a sender.

Customization point objects

The name execution::let_error denotes a customization point object, which is a const function object of a literal semiregular class type. The cv-unqualified version of its type is a tag type denoted as execution::let_error_t. See CustomizationPointObject for details.

Details

  • Nothing runs when the sender is built. f is called only after the returned sender has been connected to a receiver and started, and only if sndr completes with an error.
  • f receives the error as an lvalue that stays alive until the sender returned by f completes. A parameter of type E&, const E& or E accepts it; a parameter of type E&& does not. The returned sender may therefore keep references to the error.
  • f is called on the execution agent on which sndr completed with the error, and the sender it returns is started there.
  • If sndr can fail with several error types, f must be callable with each of them (a generic lambda works), and must return a sender for each. Exceptions thrown by earlier steps arrive as a std::exception_ptr.
  • f must return one sender type for every error type it handles: a lambda whose return statements return different sender types does not compile.

Notes

  • The result of let_error depends on what the returned sender does. To make success and recovery interchangeable for what follows, return a sender that completes with the same type of value as sndr. A sender with two different value types cannot be passed to this_thread::sync_wait, which requires a single set of values.
  • If the recovery work can fail too, its error becomes the error of the result. That makes let_error suitable for chains such as "try the primary, then the backup".
  • If recovery is a plain computation, upon_error is simpler: its function returns a value, not a sender.
  • Unless f is noexcept, the returned sender can also complete with a std::exception_ptr error, and that is part of its type even if f never throws. What follows must then handle that error too: a function passed to upon_error has to accept a std::exception_ptr, and spawn does not accept the sender. Declare f noexcept if it cannot throw. The same applies if copying the error, or connecting the sender returned by f, can throw.

Example

A fetch from the primary server fails, so let_error starts a fetch from the backup, and the pipeline continues with its result.
The standard header is not available in the online compilers yet, so this link runs the example on Compiler Explorer with stdexec, the reference implementation.

#include <exception>
#include <execution>
#include <print>
#include <stdexcept>
#include <string>
#include <thread>

namespace ex = std::execution;

// Stand-in for fetching data from a server, which fails when the server is down.
ex::sender auto fetch(std::string server, bool up)
{
    return ex::just(std::move(server))
         | ex::then([up](std::string name)
           {
               if (!up)
                   throw std::runtime_error(name + " is down");
               return "data from " + name;
           });
}

ex::sender auto fetch_with_fallback(bool primary_up)
{
    return fetch("primary", primary_up)
         | ex::let_error([](std::exception_ptr)
           {
               std::println("primary failed, trying the backup");
               return fetch("backup", true);
           });
}

int main()
{
    auto [a] = std::this_thread::sync_wait(fetch_with_fallback(true)).value();
    std::println("{}", a);

    auto [b] = std::this_thread::sync_wait(fetch_with_fallback(false)).value();
    std::println("{}", b);
}

Output:

data from primary
primary failed, trying the backup
data from backup

See also

chains a function onto a sender, calling it with the error if the sender fails and sending its result on as a value
(customization point object)[edit]
chains a function onto a sender, calling it with the values the sender sends and continuing with the sender it returns
(customization point object)[edit]
chains a function onto a sender, calling it if the sender is cancelled and continuing with the sender it returns
(customization point object)[edit]