Details
-
Suggestion
-
Resolution: Unresolved
-
P3: Somewhat important
-
None
-
6.1.2
-
None
Description
It would be good to be able to forward an error passed to onFailed to either or both of
- Another QPromise,
- The returned QFuture<T>
Currently, onFailed apparently must return a value of type T, and must accept a statically determined exception type. What would be desirable is
future.onFailed([&](std::exception_ptr p) { anotherPromise.setException(p); })
Or (to disambiguate the case where 'p' is thrown as an exception - phew), suggestion by peppe,
future.onFailed([&](auto e) { anotherPromise.setException(std::make_exception_ptr(e)); });
The latter fails currently, because Qt tries to determine the parameter type of the lambda, but which is a template here.
It would also desirable to me to forward the error to the returned QFuture<T>. In the following, the "exception catcher" is supposed to work around the above limitation. But then, I would like to forward it to the return future. For example:
class ExceptionCatcher { public: std::exception_ptr caught; template<typename T> ExceptionCatcher(T &&t) :caught(std::make_exception_ptr(std::forward<T>(t))) {} };
QFuture<X> x = future.onFailed([](ExceptionCatcher e) {
std::rethrow_exception(e.caught);
});
Instead, currently "onFailed" always has to return a value of type X, making it impossible to forward an error, or translate an error to a different error. Maybe even allowing to make the lambda return "void" should implicitly make the error be forwarded to the returned future.
QFuture<X> x = future.onFailed([](ExceptionCatcher e) {
antoherPromise.setException(e.caught);
});
Here, the lambda doesn't return anything, so I would expect that on failure, for whatever exception, we get a future in exceptional state.