23 using namespace folly;
27 virtual int getInt()
const = 0;
57 std::rethrow_exception(eptr);
61 throw std::logic_error(
"impossible");
65 TEST(ExceptionWrapper, nothrow) {
74 TEST(ExceptionWrapper, throw_test) {
75 std::runtime_error e(
"payload");
76 auto ew = make_exception_wrapper<std::runtime_error>(e);
78 std::vector<exception_wrapper> container;
79 container.push_back(ew);
82 container[0].throw_exception();
83 }
catch (std::runtime_error& err) {
91 TEST(ExceptionWrapper, throw_with_nested) {
92 auto ew = make_exception_wrapper<std::runtime_error>(
"inner");
94 ew.throw_with_nested(std::runtime_error(
"outer"));
96 }
catch (std::runtime_error& outer) {
99 std::rethrow_if_nested(outer);
101 }
catch (std::runtime_error& inner) {
107 TEST(ExceptionWrapper, members) {
112 ew = make_exception_wrapper<std::runtime_error>(
"payload");
114 EXPECT_EQ(ew.what(), kRuntimeErrorClassName +
": payload");
118 TEST(ExceptionWrapper, try_and_catch_test) {
123 [=]() {
throw std::runtime_error(expected); });
132 auto ew2 = try_and_catch<std::runtime_error, std::exception>(
133 [=]() {
throw std::runtime_error(expected); });
137 rep = ew2.is_compatible_with<std::runtime_error>();
141 auto ew3 = try_and_catch<std::exception, std::runtime_error>(
142 []() {
throw std::exception(); });
146 rep = ew3.is_compatible_with<std::runtime_error>();
151 try_and_catch<std::runtime_error>([]() {
throw std::exception(); }),
155 TEST(ExceptionWrapper, with_exception_test) {
162 EXPECT_EQ(ew.
what(), kIntExceptionClassName +
": int == 23");
165 [&](
const IntException&
ie) { EXPECT_EQ(ie.getInt(), expected); }));
172 EXPECT_EQ(ew2.what(), kIntExceptionClassName +
": int == 23");
189 empty_ew.with_exception([&](
const std::exception& ) { FAIL(); }));
202 TEST(ExceptionWrapper, get_or_make_exception_ptr_test) {
229 TEST(ExceptionWrapper, from_exception_ptr_empty) {
230 auto ep = std::exception_ptr();
235 TEST(ExceptionWrapper, from_exception_ptr_exn) {
236 auto ep = std::make_exception_ptr(std::runtime_error(
"foo"));
240 EXPECT_TRUE(ew.is_compatible_with<std::runtime_error>());
243 TEST(ExceptionWrapper, from_exception_ptr_any) {
244 auto ep = std::make_exception_ptr<int>(12);
251 TEST(ExceptionWrapper, with_exception_ptr_empty) {
256 EXPECT_EQ(
nullptr, ew.get_exception<std::exception>());
257 EXPECT_EQ(
nullptr, ew.get_exception<
int>());
259 EXPECT_EQ(
nullptr, ew.to_exception_ptr());
265 EXPECT_DEATH(ew.throw_exception(),
"empty folly::exception_wrapper");
268 TEST(ExceptionWrapper, with_shared_ptr_test) {
271 EXPECT_EQ(
typeid(std::runtime_error), ew.type());
273 EXPECT_NE(
nullptr, ew.get_exception<std::exception>());
274 EXPECT_STREQ(
"foo", ew.get_exception<std::exception>()->what());
275 EXPECT_EQ(
nullptr, ew.get_exception<
int>());
277 EXPECT_NE(
nullptr, ew.to_exception_ptr());
279 EXPECT_EQ(kRuntimeErrorClassName, ew.class_name());
280 EXPECT_EQ(kRuntimeErrorClassName +
": foo", ew.what());
281 EXPECT_TRUE(ew.is_compatible_with<std::exception>());
282 EXPECT_TRUE(ew.is_compatible_with<std::runtime_error>());
290 EXPECT_EQ(
nullptr, ew.get_exception<std::exception>());
291 EXPECT_EQ(
nullptr, ew.get_exception<
int>());
292 EXPECT_EQ(
nullptr, ew.to_exception_ptr());
296 EXPECT_FALSE(ew.is_compatible_with<std::runtime_error>());
300 TEST(ExceptionWrapper, with_exception_ptr_exn_test) {
301 auto ep = std::make_exception_ptr(std::runtime_error(
"foo"));
304 EXPECT_EQ(
typeid(std::runtime_error), ew.type());
306 EXPECT_NE(
nullptr, ew.get_exception<std::exception>());
307 EXPECT_STREQ(
"foo", ew.get_exception<std::exception>()->what());
308 EXPECT_EQ(
nullptr, ew.get_exception<
int>());
312 EXPECT_EQ(kRuntimeErrorClassName, ew.class_name());
313 EXPECT_EQ(kRuntimeErrorClassName +
": foo", ew.what());
314 EXPECT_TRUE(ew.is_compatible_with<std::exception>());
315 EXPECT_TRUE(ew.is_compatible_with<std::runtime_error>());
323 EXPECT_EQ(
nullptr, ew.get_exception<std::exception>());
324 EXPECT_EQ(
nullptr, ew.get_exception<
int>());
325 EXPECT_EQ(
nullptr, ew.to_exception_ptr());
329 EXPECT_FALSE(ew.is_compatible_with<std::runtime_error>());
333 TEST(ExceptionWrapper, with_exception_ptr_any_test) {
334 auto ep = std::make_exception_ptr<int>(12);
338 EXPECT_EQ(
nullptr, ew.get_exception<std::exception>());
339 EXPECT_NE(
nullptr, ew.get_exception<
int>());
347 EXPECT_FALSE(ew.is_compatible_with<std::runtime_error>());
354 EXPECT_EQ(
nullptr, ew.get_exception<std::exception>());
355 EXPECT_EQ(
nullptr, ew.get_exception<
int>());
356 EXPECT_EQ(
nullptr, ew.to_exception_ptr());
361 EXPECT_FALSE(ew.is_compatible_with<std::runtime_error>());
365 TEST(ExceptionWrapper, with_non_std_exception_test) {
369 EXPECT_EQ(
nullptr, ew.get_exception<std::exception>());
370 EXPECT_NE(
nullptr, ew.get_exception<
int>());
375 EXPECT_NE(
nullptr, ew.to_exception_ptr());
380 EXPECT_FALSE(ew.is_compatible_with<std::runtime_error>());
387 EXPECT_EQ(
nullptr, ew.get_exception<std::exception>());
388 EXPECT_EQ(
nullptr, ew.get_exception<
int>());
389 EXPECT_EQ(
nullptr, ew.to_exception_ptr());
394 EXPECT_FALSE(ew.is_compatible_with<std::runtime_error>());
398 TEST(ExceptionWrapper, with_exception_ptr_any_nil_test) {
399 auto ep = std::make_exception_ptr<int>(12);
403 EXPECT_EQ(
nullptr, ew.get_exception<std::exception>());
404 EXPECT_NE(
nullptr, ew.get_exception<
int>());
407 EXPECT_EQ(
"<unknown exception>", ew.class_name());
409 EXPECT_EQ(
"<unknown exception>", ew.what());
411 EXPECT_FALSE(ew.is_compatible_with<std::runtime_error>());
418 EXPECT_EQ(
nullptr, ew.get_exception<std::exception>());
419 EXPECT_EQ(
nullptr, ew.get_exception<
int>());
420 EXPECT_EQ(
nullptr, ew.to_exception_ptr());
424 EXPECT_FALSE(ew.is_compatible_with<std::runtime_error>());
428 TEST(ExceptionWrapper, with_exception_deduction) {
429 auto ew = make_exception_wrapper<std::runtime_error>(
"hi");
430 EXPECT_TRUE(ew.with_exception([](std::runtime_error&) {}));
431 EXPECT_TRUE(ew.with_exception([](std::exception&) {}));
432 EXPECT_FALSE(ew.with_exception([](std::logic_error&) {}));
435 TEST(ExceptionWrapper, with_exception_deduction_exn_const) {
436 auto ew = make_exception_wrapper<std::runtime_error>(
"hi");
437 EXPECT_TRUE(ew.with_exception([](
const std::runtime_error&) {}));
438 EXPECT_TRUE(ew.with_exception([](
const std::exception&) {}));
439 EXPECT_FALSE(ew.with_exception([](
const std::logic_error&) {}));
442 TEST(ExceptionWrapper, with_exception_deduction_wrap_const_exn_const) {
443 const auto cew = make_exception_wrapper<std::runtime_error>(
"hi");
444 EXPECT_TRUE(cew.with_exception([](
const std::runtime_error&) {}));
445 EXPECT_TRUE(cew.with_exception([](
const std::exception&) {}));
446 EXPECT_FALSE(cew.with_exception([](
const std::logic_error&) {}));
449 TEST(ExceptionWrapper, with_exception_deduction_returning) {
450 auto ew = make_exception_wrapper<std::runtime_error>(
"hi");
451 EXPECT_TRUE(ew.with_exception([](std::runtime_error&) { return 3; }));
452 EXPECT_TRUE(ew.with_exception([](std::exception&) { return
"hello"; }));
453 EXPECT_FALSE(ew.with_exception([](std::logic_error&) { return nullptr; }));
457 template <
typename T>
463 TEST(ExceptionWrapper, with_exception_deduction_functor_lvalue) {
464 auto ew = make_exception_wrapper<std::runtime_error>(
"hi");
465 EXPECT_TRUE(ew.with_exception(r_to_l([](std::runtime_error&) {})));
466 EXPECT_TRUE(ew.with_exception(r_to_l([](std::exception&) {})));
467 EXPECT_FALSE(ew.with_exception(r_to_l([](std::logic_error&) {})));
470 TEST(ExceptionWrapper, non_std_exception_test) {
474 try_and_catch<std::exception, int>([=]() {
throw expected; });
490 auto ew = make_exception_wrapper<std::runtime_error>(
"argh");
494 TEST(ExceptionWrapper, throwException_noException) {
500 class TestException :
public std::exception {};
506 TEST(ExceptionWrapper, implicitConstruction) {
510 testEW(TestException());
514 struct BaseException {
515 virtual ~BaseException() {}
517 struct DerivedException : BaseException {};
520 throw DerivedException{};
521 }
catch (
const BaseException& e) {
527 TEST(ExceptionWrapper, base_derived_non_std_exception_test) {
528 auto ew = testNonStdException();
529 EXPECT_TRUE(ew.type() ==
typeid(DerivedException));
530 EXPECT_TRUE(ew.with_exception([](
const DerivedException&) {}));
535 struct BigRuntimeError : std::runtime_error {
536 using std::runtime_error::runtime_error;
540 struct BigNonStdError {
545 TEST(ExceptionWrapper, handle_std_exception) {
546 auto ep = std::make_exception_ptr(std::runtime_error{
"hello world"});
555 [&](
const std::runtime_error&) { handled =
true; },
560 expect_runtime_error_yes_catch_all(ew_eptr);
563 expect_runtime_error_yes_catch_all(ew_small);
566 expect_runtime_error_yes_catch_all(ew_big);
573 [&](
const std::runtime_error&) { handled =
true; },
577 expect_runtime_error_no_catch_all(ew_eptr);
580 expect_runtime_error_no_catch_all(ew_small);
583 expect_runtime_error_no_catch_all(ew_big);
590 [&](
const std::runtime_error&) { handled =
true; },
595 expect_runtime_error_catch_non_std(ew_eptr);
598 expect_runtime_error_catch_non_std(ew_small);
601 expect_runtime_error_catch_non_std(ew_big);
610 [&](
const std::runtime_error& e) {
617 EXPECT_THROW(expect_runtime_error_rethrow(ew_eptr), std::runtime_error);
620 EXPECT_THROW(expect_runtime_error_rethrow(ew_small), std::runtime_error);
623 EXPECT_THROW(expect_runtime_error_rethrow(ew_big), std::runtime_error);
627 TEST(ExceptionWrapper, handle_std_exception_unhandled) {
628 auto ep = std::make_exception_ptr(std::exception{});
637 [&](...) { handled =
true; });
640 expect_runtime_error_yes_catch_all(ew_eptr);
643 expect_runtime_error_yes_catch_all(ew_small);
647 TEST(ExceptionWrapper, handle_std_exception_propagated) {
648 auto ep = std::make_exception_ptr(std::runtime_error{
"hello world"});
655 }
catch (
const std::runtime_error&) {
657 }
catch (
const std::exception&) {
663 }
catch (
const std::runtime_error&) {
665 }
catch (
const std::exception&) {
671 }
catch (
const std::runtime_error&) {
673 }
catch (
const std::exception&) {
678 TEST(ExceptionWrapper, handle_non_std_exception_small) {
679 auto ep = std::make_exception_ptr(42);
688 [&](...) { handled =
true; });
691 expect_int_yes_catch_all(ew_eptr1);
694 expect_int_yes_catch_all(ew_eptr2);
697 expect_int_yes_catch_all(ew_small);
704 [&](
const int&) { handled =
true; });
707 expect_int_no_catch_all(ew_eptr1);
710 expect_int_no_catch_all(ew_eptr2);
713 expect_int_no_catch_all(ew_small);
719 [&](
const int&) { handled =
true; },
723 expect_int_no_catch_all_2(ew_eptr1);
726 expect_int_no_catch_all_2(ew_eptr2);
729 expect_int_no_catch_all_2(ew_small);
733 TEST(ExceptionWrapper, handle_non_std_exception_big) {
734 auto ep = std::make_exception_ptr(BigNonStdError{});
743 [&](...) { handled =
true; });
746 expect_int_yes_catch_all(ew_eptr1);
749 expect_int_yes_catch_all(ew_eptr2);
752 expect_int_yes_catch_all(ew_big);
759 [&](
const BigNonStdError&) { handled =
true; });
762 expect_int_no_catch_all(ew_eptr1);
765 expect_int_no_catch_all(ew_eptr2);
768 expect_int_no_catch_all(ew_big);
774 [&](
const BigNonStdError&) { handled =
true; },
778 expect_int_no_catch_all_2(ew_eptr1);
781 expect_int_no_catch_all_2(ew_eptr2);
784 expect_int_no_catch_all_2(ew_big);
792 TEST(ExceptionWrapper, handle_non_std_exception_rethrow_base_derived) {
793 auto ew = testNonStdException();
797 [&](
const DerivedException& e) {
801 [](
const BaseException&) { ADD_FAILURE(); }),
807 [&](
const DerivedException& e) {
811 [](...) { ADD_FAILURE(); }),
816 TEST(ExceptionWrapper, self_swap_test) {
819 EXPECT_EQ(kRuntimeErrorClassName +
": hello world", ew.
what());
folly::fbstring what() const
static const std::string kExceptionClassName
void handle(CatchFns...fns)
#define EXPECT_THROW(statement, expected_exception)
bool is_compatible_with() const noexcept
#define EXPECT_EQ(val1, val2)
const char * what() const noexceptoverride
fbstring exceptionStr(const std::exception &e)
constexpr detail::Map< Move > move
static exception_wrapper from_exception_ptr(std::exception_ptr const &eptr) noexcept
static const std::string kRuntimeErrorClassName
static std::type_info const & none() noexcept
int getInt() const override
—— Concurrent Priority Queue Implementation ——
in_place_tag in_place(in_place_tag={})
requires E e noexcept(noexcept(s.error(std::move(e))))
folly::fbstring class_name() const
#define EXPECT_STREQ(s1, s2)
std::exception_ptr const & to_exception_ptr() noexcept
static const std::string kIntExceptionClassName
std::enable_if< detail::is_chrono_conversion< Tgt, Src >::value, Tgt >::type to(const Src &value)
std::basic_string< E, T, A > toStdString() const
static const char *const value
static const std::string kIntClassName
virtual int getInt() const =0
void throw_exception() const
#define EXPECT_TRUE(condition)
void swap(exception_wrapper &a, exception_wrapper &b) noexcept
#define EXPECT_NE(val1, val2)
bool with_exception(Fn fn)
#define EXPECT_FALSE(condition)
T & from_eptr(std::exception_ptr &eptr)
TEST(SequencedExecutor, CPUThreadPoolExecutor)
fbstring demangle(const char *name)