From 3012dc36bbc40e6f8c07a6af7b2fba9d6c826399 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Tue, 28 Apr 2015 09:45:26 +0200 Subject: Async: Nested job error propagation. --- async/autotests/asynctest.cpp | 23 +++++++++++++++++++++++ async/src/async.h | 6 +++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/async/autotests/asynctest.cpp b/async/autotests/asynctest.cpp index 9789950..9bc9f6b 100644 --- a/async/autotests/asynctest.cpp +++ b/async/autotests/asynctest.cpp @@ -68,6 +68,7 @@ private Q_SLOTS: void testErrorPropagation(); void testErrorHandlerAsync(); void testErrorPropagationAsync(); + void testNestedErrorPropagation(); void testChainingRunningJob(); void testChainingFinishedJob(); @@ -688,6 +689,28 @@ void AsyncTest::testErrorPropagationAsync() QCOMPARE(error, 1); } +void AsyncTest::testNestedErrorPropagation() +{ + int error = 0; + auto job = Async::start([](){}) + .then(Async::error(1, "error")) //Nested job that throws error + .then([](Async::Future &future) { + //We should never get here + Q_ASSERT(false); + }, + [&error](int errorCode, const QString &errorMessage) { + error += errorCode; + } + ); + auto future = job.exec(); + + future.waitForFinished(); + + QVERIFY(future.isFinished()); + QCOMPARE(future.errorCode(), 1); + QCOMPARE(future.errorMessage(), QString::fromLatin1("error")); + QCOMPARE(error, 1); +} diff --git a/async/src/async.h b/async/src/async.h index d6a74c0..18b5979 100644 --- a/async/src/async.h +++ b/async/src/async.h @@ -514,7 +514,11 @@ private: // copy by value is const auto outFuture = future; Async::detail::copyFutureValue(watcher->future(), outFuture); - outFuture.setFinished(); + if (watcher->future().errorCode()) { + outFuture.setError(watcher->future().errorCode(), watcher->future().errorMessage()); + } else { + outFuture.setFinished(); + } delete watcher; }); watcher->setFuture(job.exec(in ...)); -- cgit v1.2.3