Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-95216

Allow onFailed to forward the exception

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Unresolved
    • P3: Somewhat important
    • None
    • 6.1.2
    • None
    • All

    Description

      It would be good to be  able to forward an error passed to onFailed to either or both of

      1. Another QPromise,
      2. 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.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            sonakur Sona Kurazyan
            jschaubzes Johannes Schaub
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes