Use exception_wrapper::from_exception_ptr in Try and Promise
authorYedidya Feldblum <yfeldblum@fb.com>
Tue, 22 Aug 2017 04:10:34 +0000 (21:10 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Tue, 22 Aug 2017 04:20:23 +0000 (21:20 -0700)
Summary:
[Folly] Use `exception_wrapper::from_exception_ptr` in `Try` and `Promise`.

It does just what we need; DRY.

Reviewed By: andrewjcg

Differential Revision: D5674663

fbshipit-source-id: 73e46df62c06736c4eaf013d05dfea819cbec515

folly/Try.h
folly/futures/Promise-inl.h

index f38362b803c580ea0062a8616ae69dabf73a4b57..fb783b010a0f9ff303332139b694423026bbb49f 100644 (file)
@@ -114,15 +114,8 @@ class Try {
    */
   FOLLY_DEPRECATED("use Try(exception_wrapper)")
   explicit Try(std::exception_ptr ep)
-    : contains_(Contains::EXCEPTION) {
-    try {
-      std::rethrow_exception(ep);
-    } catch (std::exception& e) {
-      e_ = exception_wrapper(std::current_exception(), e);
-    } catch (...) {
-      e_ = exception_wrapper(std::current_exception());
-    }
-  }
+      : contains_(Contains::EXCEPTION),
+        e_(exception_wrapper::from_exception_ptr(ep)) {}
 
   // Move constructor
   Try(Try<T>&& t) noexcept;
@@ -339,15 +332,8 @@ class Try<void> {
    * @param ep The exception_pointer. Will be rethrown.
    */
   FOLLY_DEPRECATED("use Try(exception_wrapper)")
-  explicit Try(std::exception_ptr ep) : hasValue_(false) {
-    try {
-      std::rethrow_exception(ep);
-    } catch (const std::exception& e) {
-      e_ = exception_wrapper(std::current_exception(), e);
-    } catch (...) {
-      e_ = exception_wrapper(std::current_exception());
-    }
-  }
+  explicit Try(std::exception_ptr ep)
+      : hasValue_(false), e_(exception_wrapper::from_exception_ptr(ep)) {}
 
   // Copy assigner
   Try& operator=(const Try<void>& t) {
index 3c6ff17345965552b303bb943638fe0c046e0f4e..5fe4ed2cb029c782c39e3f54ac9fa2fdb0fc2222 100644 (file)
@@ -99,13 +99,7 @@ Promise<T>::setException(E const& e) {
 
 template <class T>
 void Promise<T>::setException(std::exception_ptr const& ep) {
-  try {
-    std::rethrow_exception(ep);
-  } catch (const std::exception& e) {
-    setException(exception_wrapper(std::current_exception(), e));
-  } catch (...) {
-    setException(exception_wrapper(std::current_exception()));
-  }
+  setException(exception_wrapper::from_exception_ptr(ep));
 }
 
 template <class T>