From ac639d5621e642628d99a7c02e08b38607d77aa7 Mon Sep 17 00:00:00 2001 From: Rushi Desai Date: Wed, 30 Jul 2014 21:08:51 -0700 Subject: [PATCH] Construct Later with exception_ptr Summary: It would be nice to be able to create a Later with pre-loaded exception. Test Plan: Unit test Reviewed By: hans@fb.com Subscribers: fugalh FB internal diff: D1462810 --- folly/wangle/Later-inl.h | 15 +++++++++++++++ folly/wangle/Later.h | 15 +++++++++++++++ folly/wangle/test/LaterTest.cpp | 5 +++++ 3 files changed, 35 insertions(+) diff --git a/folly/wangle/Later-inl.h b/folly/wangle/Later-inl.h index c92ceb55..95d4461f 100644 --- a/folly/wangle/Later-inl.h +++ b/folly/wangle/Later-inl.h @@ -77,6 +77,21 @@ Later::Later(U&& input) { }); } +template +Later::Later(std::exception_ptr const& eptr) { + folly::MoveWrapper> promise; + future_ = promise->getFuture(); + starter_.getFuture().then([=](Try&& t) mutable { + promise->setException(eptr); + }); +} + +template +template +Later::Later(E const& e) : + Later::Later(std::make_exception_ptr(e)) { +} + template template Later::Later(std::function&&)>&& fn) { diff --git a/folly/wangle/Later.h b/folly/wangle/Later.h index d1693175..354f7d5a 100644 --- a/folly/wangle/Later.h +++ b/folly/wangle/Later.h @@ -104,6 +104,21 @@ class Later { class = typename std::enable_if::value>::type> explicit Later(U&& input); + /* + * This constructor is used to build an asynchronous workflow that takes an + * exception_ptr as input, and throws it on completion. + */ + explicit Later(std::exception_ptr const&); + + /* + * This constructor is used to build an asynchronous workflow that takes an + * exception as input, and throws it on completion. + */ + template ::value>::type> + explicit Later(E const& e); + /* * This constructor is used to wrap a pre-existing cob-style asynchronous api * so that it can be used in wangle. wangle provides the callback to this diff --git a/folly/wangle/test/LaterTest.cpp b/folly/wangle/test/LaterTest.cpp index bdc03832..b4cd96be 100644 --- a/folly/wangle/test/LaterTest.cpp +++ b/folly/wangle/test/LaterTest.cpp @@ -83,6 +83,11 @@ TEST(Later, construct_and_launch) { EXPECT_TRUE(fulfilled); } +TEST(Later, exception_on_launch) { + auto later = Later(std::runtime_error("E")); + EXPECT_THROW(later.launch().value(), std::runtime_error); +} + TEST(Later, then_value) { auto future = Later(std::move(1)) .then([](Try&& t) { -- 2.34.1