Commit ac639d56 authored by Rushi Desai's avatar Rushi Desai Committed by Sara Golemon

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
parent fa6a9824
...@@ -77,6 +77,21 @@ Later<T>::Later(U&& input) { ...@@ -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 T>
template <class U, class Unused, class Unused2> template <class U, class Unused, class Unused2>
Later<T>::Later(std::function<void(std::function<void(U&&)>&&)>&& fn) { Later<T>::Later(std::function<void(std::function<void(U&&)>&&)>&& fn) {
......
...@@ -104,6 +104,21 @@ class Later { ...@@ -104,6 +104,21 @@ class Later {
class = typename std::enable_if<std::is_same<T, U>::value>::type> class = typename std::enable_if<std::is_same<T, U>::value>::type>
explicit Later(U&& input); 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 * 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 * so that it can be used in wangle. wangle provides the callback to this
......
...@@ -83,6 +83,11 @@ TEST(Later, construct_and_launch) { ...@@ -83,6 +83,11 @@ TEST(Later, construct_and_launch) {
EXPECT_TRUE(fulfilled); 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) { TEST(Later, then_value) {
auto future = Later<int>(std::move(1)) auto future = Later<int>(std::move(1))
.then([](Try<int>&& t) { .then([](Try<int>&& t) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment