Construct Later with exception_ptr
authorRushi Desai <rushix@fb.com>
Thu, 31 Jul 2014 04:08:51 +0000 (21:08 -0700)
committerSara Golemon <sgolemon@fb.com>
Thu, 14 Aug 2014 18:49:03 +0000 (11:49 -0700)
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
folly/wangle/Later.h
folly/wangle/test/LaterTest.cpp

index c92ceb55f8267f536eafe7e393b31a0655e6a79e..95d4461fe615f1b71155256b495ca7a96e0bc22b 100644 (file)
@@ -77,6 +77,21 @@ Later<T>::Later(U&& input) {
   });
 }
 
+template <typename T>
+Later<T>::Later(std::exception_ptr const& eptr) {
+  folly::MoveWrapper<Promise<T>> promise;
+  future_ = promise->getFuture();
+  starter_.getFuture().then([=](Try<void>&& t) mutable {
+    promise->setException(eptr);
+  });
+}
+
+template <typename T>
+template <typename E, class Unused>
+Later<T>::Later(E const& e) :
+    Later<T>::Later(std::make_exception_ptr<E>(e)) {
+}
+
 template <class T>
 template <class U, class Unused, class Unused2>
 Later<T>::Later(std::function<void(std::function<void(U&&)>&&)>&& fn) {
index d169317544c0a395e543323c566e1b7c480c0be2..354f7d5a5614110af9bd64c6d605071458f9297e 100644 (file)
@@ -104,6 +104,21 @@ class Later {
             class = typename std::enable_if<std::is_same<T, U>::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 <class E,
+            class = typename std::enable_if<
+                std::is_base_of<std::exception, E>::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
index bdc03832cc10cc5a743f0245144b5c5a1e4ebf52..b4cd96be90ed9788c0147cb2022840099ab290e7 100644 (file)
@@ -83,6 +83,11 @@ TEST(Later, construct_and_launch) {
   EXPECT_TRUE(fulfilled);
 }
 
+TEST(Later, exception_on_launch) {
+  auto later = Later<void>(std::runtime_error("E"));
+  EXPECT_THROW(later.launch().value(), std::runtime_error);
+}
+
 TEST(Later, then_value) {
   auto future = Later<int>(std::move(1))
     .then([](Try<int>&& t) {