From f372f154be5b9c44a7e0322d6fd64166485688ec Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Mon, 21 Aug 2017 11:38:39 -0700 Subject: [PATCH] exception_wrapper::from_exception_ptr Summary: [Folly] `exception_wrapper::from_exception_ptr`. A handry helper for converting from `std::exception_ptr` to `folly::exception_wrapper`. Reviewed By: ericniebler Differential Revision: D5668179 fbshipit-source-id: a81a60cb22a2a697714268e027af62dd8825d2c3 --- folly/ExceptionWrapper.cpp | 14 ++++++++++++++ folly/ExceptionWrapper.h | 3 +++ folly/test/ExceptionWrapperTest.cpp | 22 ++++++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/folly/ExceptionWrapper.cpp b/folly/ExceptionWrapper.cpp index 6d14f0ad..7cda0b76 100644 --- a/folly/ExceptionWrapper.cpp +++ b/folly/ExceptionWrapper.cpp @@ -60,6 +60,20 @@ std::exception const* get_std_exception_(std::exception_ptr eptr) noexcept { } } +exception_wrapper exception_wrapper::from_exception_ptr( + std::exception_ptr const& ptr) noexcept { + if (!ptr) { + return exception_wrapper(); + } + try { + std::rethrow_exception(ptr); + } catch (std::exception& e) { + return exception_wrapper(std::current_exception(), e); + } catch (...) { + return exception_wrapper(std::current_exception()); + } +} + exception_wrapper::exception_wrapper(std::exception_ptr ptr) noexcept : exception_wrapper{} { if (ptr) { diff --git a/folly/ExceptionWrapper.h b/folly/ExceptionWrapper.h index 489d158a..eb493fc6 100644 --- a/folly/ExceptionWrapper.h +++ b/folly/ExceptionWrapper.h @@ -368,6 +368,9 @@ class exception_wrapper final { static bool with_exception_(This& this_, Fn fn_); public: + static exception_wrapper from_exception_ptr( + std::exception_ptr const& eptr) noexcept; + //! Default-constructs an empty `exception_wrapper` //! \post `type() == none()` exception_wrapper() noexcept {} diff --git a/folly/test/ExceptionWrapperTest.cpp b/folly/test/ExceptionWrapperTest.cpp index e182196d..fdb68407 100644 --- a/folly/test/ExceptionWrapperTest.cpp +++ b/folly/test/ExceptionWrapperTest.cpp @@ -209,6 +209,28 @@ TEST(ExceptionWrapper, get_or_make_exception_ptr_test) { EXPECT_FALSE(eptr); } +TEST(ExceptionWrapper, from_exception_ptr_empty) { + auto ep = std::exception_ptr(); + auto ew = exception_wrapper::from_exception_ptr(ep); + EXPECT_FALSE(bool(ew)); +} + +TEST(ExceptionWrapper, from_exception_ptr_exn) { + auto ep = std::make_exception_ptr(std::runtime_error("foo")); + auto ew = exception_wrapper::from_exception_ptr(ep); + EXPECT_TRUE(bool(ew)); + EXPECT_EQ(ep, ew.to_exception_ptr()); + EXPECT_TRUE(ew.is_compatible_with()); +} + +TEST(ExceptionWrapper, from_exception_ptr_any) { + auto ep = std::make_exception_ptr(12); + auto ew = exception_wrapper::from_exception_ptr(ep); + EXPECT_TRUE(bool(ew)); + EXPECT_EQ(ep, ew.to_exception_ptr()); + EXPECT_TRUE(ew.is_compatible_with()); +} + TEST(ExceptionWrapper, with_exception_ptr_empty) { auto ew = exception_wrapper(std::exception_ptr()); EXPECT_EQ(exception_wrapper::none(), ew.type()); -- 2.34.1