std::execution::upon_error
| Defined in header <execution>
|
||
struct upon_error_t { /* unspecified */ };
|
(1) | (since C++26) |
inline constexpr upon_error_t upon_error{};
|
(2) | (since C++26) (customization point object) |
| Call signature |
||
execution::sender auto upon_error( execution::sender auto sndr,
/*movable-value*/ auto f );
|
(3) | (since C++26) |
Attaches an error handler to a sender: when sndr completes with an error, f is called with the error, and the result of the call becomes a value the returned sender completes with.
This is how a failure is turned back into an ordinary result in a sender pipeline: a fallback value, a log entry plus a default, or an alternative representation of the failure. It is the error-channel counterpart of then, which does the same for values.
upon_error is a pipeable sender adaptor: sndr | upon_error(f) is equivalent to upon_error(sndr, f).
Parameters
| sndr | - | the sender whose errors are passed to f
|
| f | - | the function (or other callable) to call with the error sent by sndr
|
Return value
A sender that behaves like sndr, except that each error completion of sndr is replaced by a call of f with the error it carries:
- The result of
fis sent as a value of the returned sender, so the failure is gone. Iffreturnsvoid, the returned sender completes with no values. - If
fthrows, the returned sender completes with the exception as an error (set_errorwith a std::exception_ptr).
Value and stopped completions of sndr are forwarded unchanged: f is not called for them. Use then and upon_stopped to handle those.
upon_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 f cannot be called with the error type of some error completion of sndr.
Customization point objects
The name execution::upon_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::upon_error_t. See CustomizationPointObject for details.
Details
- Nothing runs when the sender is built.
fis called only after the returned sender has been connected to a receiver and started, and only ifsndrcompletes with an error. fruns on the execution agent on whichsndrcompleted with the error.upon_errornever moves work to another thread.freceives the error as the single argument. Ifsndrcan fail with several error types,fmust be callable with each of them (a generic lambda works). Exceptions thrown by earlier steps, for example by a function passed to then, arrive as a std::exception_ptr.
Notes
- After
upon_error, the failure has become a value. If the input sender also completes with values, the returned sender has both kinds of value completion, and they must agree to be useful: to treat success and failure alike afterwards, makefreturn the same type as the values ofsndr. A sender with two different value types cannot be passed to this_thread::sync_wait, which requires a single set of values. - To handle an error by running another asynchronous operation (for example to retry), use let_error: its function returns a sender, whereas the result of
fhere is a plain value.
Example
An error code from an operation is turned into a message.
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 <execution>
#include <iostream>
#include <string>
#include <system_error>
#include <thread>
namespace ex = std::execution;
int main()
{
auto sndr = ex::just_error(std::make_error_code(std::errc::timed_out))
| ex::upon_error([](std::error_code ec)
{
return "request failed: " + ec.message();
});
auto [message] = std::this_thread::sync_wait(std::move(sndr)).value();
std::cout << message << '\n';
}
Output:
request failed: Connection timed out
Success and failure both end in the same kind of value. The handler runs only for the failing input; the value completion passes through.
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 <iostream>
#include <stdexcept>
#include <thread>
namespace ex = std::execution;
auto checked_division(int divisor)
{
return ex::just(divisor)
| ex::then([](int d)
{
if (d == 0)
throw std::runtime_error("division by zero");
return 100 / d;
})
| ex::upon_error([](std::exception_ptr)
{
std::cout << "handling the error\n";
return -1;
});
}
int main()
{
auto [ok] = std::this_thread::sync_wait(checked_division(4)).value();
auto [failed] = std::this_thread::sync_wait(checked_division(0)).value();
std::cout << "100 / 4 = " << ok << '\n'
<< "100 / 0 = " << failed << '\n';
}
Output:
handling the error
100 / 4 = 25
100 / 0 = -1
See also
(C++26) |
chains a function onto a sender, calling it with the values the sender sends and sending its result on (customization point object) |
(C++26) |
chains a function onto a sender, calling it if the sender is cancelled and sending its result on as a value (customization point object) |
(C++26) |
chains a function onto a sender, calling it with the error if the sender fails and continuing with the sender it returns (customization point object) |