Commit f372f154 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

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
parent 4560ebca
...@@ -60,6 +60,20 @@ std::exception const* get_std_exception_(std::exception_ptr eptr) noexcept { ...@@ -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::exception_wrapper(std::exception_ptr ptr) noexcept
: exception_wrapper{} { : exception_wrapper{} {
if (ptr) { if (ptr) {
......
...@@ -368,6 +368,9 @@ class exception_wrapper final { ...@@ -368,6 +368,9 @@ class exception_wrapper final {
static bool with_exception_(This& this_, Fn fn_); static bool with_exception_(This& this_, Fn fn_);
public: public:
static exception_wrapper from_exception_ptr(
std::exception_ptr const& eptr) noexcept;
//! Default-constructs an empty `exception_wrapper` //! Default-constructs an empty `exception_wrapper`
//! \post `type() == none()` //! \post `type() == none()`
exception_wrapper() noexcept {} exception_wrapper() noexcept {}
......
...@@ -209,6 +209,28 @@ TEST(ExceptionWrapper, get_or_make_exception_ptr_test) { ...@@ -209,6 +209,28 @@ TEST(ExceptionWrapper, get_or_make_exception_ptr_test) {
EXPECT_FALSE(eptr); 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<std::runtime_error>());
}
TEST(ExceptionWrapper, from_exception_ptr_any) {
auto ep = std::make_exception_ptr<int>(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<int>());
}
TEST(ExceptionWrapper, with_exception_ptr_empty) { TEST(ExceptionWrapper, with_exception_ptr_empty) {
auto ew = exception_wrapper(std::exception_ptr()); auto ew = exception_wrapper(std::exception_ptr());
EXPECT_EQ(exception_wrapper::none(), ew.type()); EXPECT_EQ(exception_wrapper::none(), ew.type());
......
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