2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <folly/ExceptionWrapper.h>
20 #include <folly/Conv.h>
21 #include <folly/portability/GTest.h>
23 using namespace folly;
25 class AbstractIntException : public std::exception {
27 virtual int getInt() const = 0;
30 class IntException : public AbstractIntException {
32 explicit IntException(int i) : i_(i) {}
34 int getInt() const override { return i_; }
35 const char* what() const noexcept override {
36 what_ = folly::to<std::string>("int == ", i_);
42 mutable std::string what_;
45 const static std::string kExceptionClassName =
46 demangle(typeid(std::exception)).toStdString();
47 const static std::string kRuntimeErrorClassName =
48 demangle(typeid(std::runtime_error)).toStdString();
49 const static std::string kIntExceptionClassName =
50 demangle(typeid(IntException)).toStdString();
51 const static std::string kIntClassName = demangle(typeid(int)).toStdString();
54 T& from_eptr(std::exception_ptr& eptr) {
56 std::rethrow_exception(eptr);
60 throw std::logic_error("impossible");
64 // Tests that when we call throw_exception, the proper type is thrown (derived)
65 TEST(ExceptionWrapper, throw_test) {
66 std::runtime_error e("payload");
67 auto ew = make_exception_wrapper<std::runtime_error>(e);
69 std::vector<exception_wrapper> container;
70 container.push_back(ew);
73 container[0].throw_exception();
74 } catch (std::runtime_error& err) {
75 std::string expected = "payload";
76 std::string actual = err.what();
77 EXPECT_EQ(expected, actual);
81 TEST(ExceptionWrapper, members) {
82 auto ew = exception_wrapper();
83 EXPECT_FALSE(bool(ew));
84 EXPECT_EQ(ew.what(), "");
85 EXPECT_EQ(ew.class_name(), "");
86 ew = make_exception_wrapper<std::runtime_error>("payload");
87 EXPECT_TRUE(bool(ew));
88 EXPECT_EQ(ew.what(), kRuntimeErrorClassName + ": payload");
89 EXPECT_EQ(ew.class_name(), kRuntimeErrorClassName);
92 TEST(ExceptionWrapper, try_and_catch_test) {
93 std::string expected = "payload";
95 // Catch rightmost matching exception type
96 exception_wrapper ew = try_and_catch<std::exception, std::runtime_error>(
98 throw std::runtime_error(expected);
100 EXPECT_TRUE(bool(ew));
101 EXPECT_EQ(ew.what(), kRuntimeErrorClassName + ": payload");
102 EXPECT_EQ(ew.class_name(), kRuntimeErrorClassName);
103 auto rep = ew.is_compatible_with<std::runtime_error>();
106 // Changing order is like catching in wrong order. Beware of this in your
108 auto ew2 = try_and_catch<std::runtime_error, std::exception>([=]() {
109 throw std::runtime_error(expected);
111 EXPECT_TRUE(bool(ew2));
112 // We are catching a std::exception, not std::runtime_error.
113 // But, we can still get the actual type if we want it.
114 rep = ew2.is_compatible_with<std::runtime_error>();
117 // Catches even if not rightmost.
118 auto ew3 = try_and_catch<std::exception, std::runtime_error>([]() {
119 throw std::exception();
121 EXPECT_TRUE(bool(ew3));
122 EXPECT_EQ(ew3.what(), kExceptionClassName + ": std::exception");
123 EXPECT_EQ(ew3.class_name(), kExceptionClassName);
124 rep = ew3.is_compatible_with<std::runtime_error>();
127 // If does not catch, throws.
129 try_and_catch<std::runtime_error>([]() {
130 throw std::exception();
135 TEST(ExceptionWrapper, with_exception_test) {
138 // This works, and doesn't slice.
139 exception_wrapper ew = try_and_catch<std::exception, std::runtime_error>(
141 throw IntException(expected);
143 EXPECT_TRUE(bool(ew));
144 EXPECT_EQ(ew.what(), kIntExceptionClassName + ": int == 23");
145 EXPECT_EQ(ew.class_name(), kIntExceptionClassName);
146 EXPECT_TRUE(ew.with_exception(
147 [&](const IntException& ie) { EXPECT_EQ(ie.getInt(), expected); }));
149 // I can try_and_catch a non-copyable base class. This will use
150 // std::exception_ptr internally.
151 exception_wrapper ew2 = try_and_catch<AbstractIntException>(
153 throw IntException(expected);
155 EXPECT_TRUE(bool(ew2));
156 EXPECT_EQ(ew2.what(), kIntExceptionClassName + ": int == 23");
157 EXPECT_EQ(ew2.class_name(), kIntExceptionClassName);
158 bool res = ew2.with_exception([&](AbstractIntException& ie) {
159 EXPECT_EQ(ie.getInt(), expected);
160 EXPECT_TRUE(dynamic_cast<IntException*>(&ie));
164 // Test with const this. If this compiles and does not crash due to
165 // infinite loop when it runs, it succeeds.
166 const exception_wrapper& cew = ew;
168 cew.with_exception([&](const IntException& /* ie */) { SUCCEED(); }));
170 // Test with empty ew.
171 exception_wrapper empty_ew;
173 empty_ew.with_exception([&](const std::exception& /* ie */) { FAIL(); }));
175 // Testing with const exception_wrapper; sanity check first:
176 EXPECT_FALSE(cew.with_exception([&](const std::runtime_error&) {}));
177 EXPECT_FALSE(cew.with_exception([&](const int&) {}));
178 // This won't even compile. You can't use a function which takes a
179 // non-const reference with a const exception_wrapper.
181 EXPECT_FALSE(cew.with_exception([&](std::runtime_error&) {}));
182 EXPECT_FALSE(cew.with_exception([&](int&) {}));
186 TEST(ExceptionWrapper, get_or_make_exception_ptr_test) {
189 // This works, and doesn't slice.
190 exception_wrapper ew = try_and_catch<std::exception, std::runtime_error>(
191 [=]() { throw IntException(expected); });
192 std::exception_ptr eptr = ew.to_exception_ptr();
193 EXPECT_THROW(std::rethrow_exception(eptr), IntException);
195 // I can try_and_catch a non-copyable base class. This will use
196 // std::exception_ptr internally.
197 exception_wrapper ew2 = try_and_catch<AbstractIntException>(
198 [=]() { throw IntException(expected); });
199 eptr = ew2.to_exception_ptr();
200 EXPECT_THROW(std::rethrow_exception(eptr), IntException);
202 // Test with const this.
203 const exception_wrapper& cew = ew;
204 eptr = cew.to_exception_ptr();
205 EXPECT_THROW(std::rethrow_exception(eptr), IntException);
207 // Test with empty ew.
208 exception_wrapper empty_ew;
209 eptr = empty_ew.to_exception_ptr();
213 TEST(ExceptionWrapper, with_exception_ptr_empty) {
214 auto ew = exception_wrapper(std::exception_ptr());
215 EXPECT_EQ(exception_wrapper::none(), ew.type());
216 EXPECT_FALSE(bool(ew));
217 EXPECT_EQ(nullptr, ew.get_exception());
218 EXPECT_FALSE(ew.has_exception_ptr());
219 EXPECT_EQ(nullptr, ew.to_exception_ptr());
220 EXPECT_FALSE(ew.has_exception_ptr());
221 EXPECT_EQ("", ew.class_name());
222 EXPECT_EQ("", ew.what());
223 EXPECT_FALSE(ew.is_compatible_with<std::exception>());
224 EXPECT_FALSE(ew.is_compatible_with<int>());
225 EXPECT_DEATH(ew.throw_exception(), "empty folly::exception_wrapper");
228 TEST(ExceptionWrapper, with_shared_ptr_test) {
229 auto ew = exception_wrapper(std::runtime_error("foo"));
230 EXPECT_TRUE(bool(ew));
231 EXPECT_EQ(typeid(std::runtime_error), ew.type());
232 EXPECT_NE(nullptr, ew.get_exception());
233 EXPECT_FALSE(ew.has_exception_ptr());
234 EXPECT_NE(nullptr, ew.to_exception_ptr());
235 EXPECT_TRUE(ew.has_exception_ptr());
236 EXPECT_EQ("std::runtime_error", ew.class_name());
237 EXPECT_EQ("std::runtime_error: foo", ew.what());
238 EXPECT_TRUE(ew.is_compatible_with<std::exception>());
239 EXPECT_TRUE(ew.is_compatible_with<std::runtime_error>());
240 EXPECT_FALSE(ew.is_compatible_with<int>());
241 EXPECT_THROW(ew.throw_exception(), std::runtime_error);
243 exception_wrapper(std::move(ew));
244 EXPECT_FALSE(bool(ew));
245 EXPECT_EQ(exception_wrapper::none(), ew.type());
246 EXPECT_EQ(nullptr, ew.get_exception());
247 EXPECT_EQ(nullptr, ew.to_exception_ptr());
248 EXPECT_EQ("", ew.class_name());
249 EXPECT_EQ("", ew.what());
250 EXPECT_FALSE(ew.is_compatible_with<std::exception>());
251 EXPECT_FALSE(ew.is_compatible_with<std::runtime_error>());
252 EXPECT_FALSE(ew.is_compatible_with<int>());
255 TEST(ExceptionWrapper, with_exception_ptr_exn_test) {
256 auto ep = std::make_exception_ptr(std::runtime_error("foo"));
257 auto ew = exception_wrapper(ep, from_eptr<std::runtime_error>(ep));
258 EXPECT_TRUE(bool(ew));
259 EXPECT_EQ(typeid(std::runtime_error), ew.type());
260 EXPECT_NE(nullptr, ew.get_exception());
261 EXPECT_TRUE(ew.has_exception_ptr());
262 EXPECT_EQ(ep, ew.to_exception_ptr());
263 EXPECT_TRUE(ew.has_exception_ptr());
264 EXPECT_EQ("std::runtime_error", ew.class_name());
265 EXPECT_EQ("std::runtime_error: foo", ew.what());
266 EXPECT_TRUE(ew.is_compatible_with<std::exception>());
267 EXPECT_TRUE(ew.is_compatible_with<std::runtime_error>());
268 EXPECT_FALSE(ew.is_compatible_with<int>());
269 EXPECT_THROW(ew.throw_exception(), std::runtime_error);
271 exception_wrapper(std::move(ew));
272 EXPECT_FALSE(bool(ew));
273 EXPECT_EQ(exception_wrapper::none(), ew.type());
274 EXPECT_EQ(nullptr, ew.get_exception());
275 EXPECT_EQ(nullptr, ew.to_exception_ptr());
276 EXPECT_EQ("", ew.class_name());
277 EXPECT_EQ("", ew.what());
278 EXPECT_FALSE(ew.is_compatible_with<std::exception>());
279 EXPECT_FALSE(ew.is_compatible_with<std::runtime_error>());
280 EXPECT_FALSE(ew.is_compatible_with<int>());
283 TEST(ExceptionWrapper, with_exception_ptr_any_test) {
284 auto ep = std::make_exception_ptr<int>(12);
285 auto ew = exception_wrapper(ep, from_eptr<int>(ep));
286 EXPECT_TRUE(bool(ew));
287 EXPECT_EQ(nullptr, ew.get_exception());
288 EXPECT_TRUE(ew.has_exception_ptr());
289 EXPECT_EQ(ep, ew.to_exception_ptr());
290 EXPECT_TRUE(ew.has_exception_ptr());
291 EXPECT_EQ("int", ew.class_name());
292 EXPECT_EQ("int", ew.what());
293 EXPECT_FALSE(ew.is_compatible_with<std::exception>());
294 EXPECT_FALSE(ew.is_compatible_with<std::runtime_error>());
295 EXPECT_TRUE(ew.is_compatible_with<int>());
296 EXPECT_THROW(ew.throw_exception(), int);
298 exception_wrapper(std::move(ew));
299 EXPECT_FALSE(bool(ew));
300 EXPECT_EQ(nullptr, ew.get_exception());
301 EXPECT_EQ(nullptr, ew.to_exception_ptr());
302 EXPECT_FALSE(ew.has_exception_ptr());
303 EXPECT_EQ("", ew.class_name());
304 EXPECT_EQ("", ew.what());
305 EXPECT_FALSE(ew.is_compatible_with<std::exception>());
306 EXPECT_FALSE(ew.is_compatible_with<std::runtime_error>());
307 EXPECT_FALSE(ew.is_compatible_with<int>());
310 TEST(ExceptionWrapper, with_non_std_exception_test) {
311 auto ew = exception_wrapper(folly::in_place, 42);
312 EXPECT_TRUE(bool(ew));
313 EXPECT_EQ(nullptr, ew.get_exception());
314 EXPECT_FALSE(ew.has_exception_ptr());
315 EXPECT_EQ("int", ew.class_name());
316 EXPECT_EQ("int", ew.what());
317 EXPECT_NE(nullptr, ew.to_exception_ptr());
318 EXPECT_TRUE(ew.has_exception_ptr());
319 EXPECT_EQ("int", ew.class_name());
320 EXPECT_EQ("int", ew.what());
321 EXPECT_FALSE(ew.is_compatible_with<std::exception>());
322 EXPECT_FALSE(ew.is_compatible_with<std::runtime_error>());
323 EXPECT_TRUE(ew.is_compatible_with<int>());
324 EXPECT_THROW(ew.throw_exception(), int);
326 exception_wrapper(std::move(ew));
327 EXPECT_FALSE(bool(ew));
328 EXPECT_EQ(nullptr, ew.get_exception());
329 EXPECT_EQ(nullptr, ew.to_exception_ptr());
330 EXPECT_FALSE(ew.has_exception_ptr());
331 EXPECT_EQ("", ew.class_name());
332 EXPECT_EQ("", ew.what());
333 EXPECT_FALSE(ew.is_compatible_with<std::exception>());
334 EXPECT_FALSE(ew.is_compatible_with<std::runtime_error>());
335 EXPECT_FALSE(ew.is_compatible_with<int>());
338 TEST(ExceptionWrapper, with_exception_ptr_any_nil_test) {
339 auto ep = std::make_exception_ptr<int>(12);
340 auto ew = exception_wrapper(ep); // concrete type is erased
341 EXPECT_TRUE(bool(ew));
342 EXPECT_EQ(nullptr, ew.get_exception());
343 EXPECT_EQ(ep, ew.to_exception_ptr());
344 EXPECT_EQ("<unknown exception>", ew.class_name()); // because concrete type is
346 EXPECT_EQ("<unknown exception>", ew.what());
347 EXPECT_FALSE(ew.is_compatible_with<std::exception>());
348 EXPECT_FALSE(ew.is_compatible_with<std::runtime_error>());
349 EXPECT_TRUE(ew.is_compatible_with<int>());
350 EXPECT_THROW(ew.throw_exception(), int);
352 exception_wrapper(std::move(ew));
353 EXPECT_FALSE(bool(ew));
354 EXPECT_EQ(nullptr, ew.get_exception());
355 EXPECT_EQ(nullptr, ew.to_exception_ptr());
356 EXPECT_EQ("", ew.class_name());
357 EXPECT_EQ("", ew.what());
358 EXPECT_FALSE(ew.is_compatible_with<std::exception>());
359 EXPECT_FALSE(ew.is_compatible_with<std::runtime_error>());
360 EXPECT_FALSE(ew.is_compatible_with<int>());
363 TEST(ExceptionWrapper, with_exception_deduction) {
364 auto ew = make_exception_wrapper<std::runtime_error>("hi");
365 EXPECT_TRUE(ew.with_exception([](std::runtime_error&) {}));
366 EXPECT_TRUE(ew.with_exception([](std::exception&) {}));
367 EXPECT_FALSE(ew.with_exception([](std::logic_error&) {}));
370 TEST(ExceptionWrapper, with_exception_deduction_exn_const) {
371 auto ew = make_exception_wrapper<std::runtime_error>("hi");
372 EXPECT_TRUE(ew.with_exception([](const std::runtime_error&) {}));
373 EXPECT_TRUE(ew.with_exception([](const std::exception&) {}));
374 EXPECT_FALSE(ew.with_exception([](const std::logic_error&) {}));
377 TEST(ExceptionWrapper, with_exception_deduction_wrap_const_exn_const) {
378 const auto cew = make_exception_wrapper<std::runtime_error>("hi");
379 EXPECT_TRUE(cew.with_exception([](const std::runtime_error&) {}));
380 EXPECT_TRUE(cew.with_exception([](const std::exception&) {}));
381 EXPECT_FALSE(cew.with_exception([](const std::logic_error&) {}));
384 TEST(ExceptionWrapper, with_exception_deduction_returning) {
385 auto ew = make_exception_wrapper<std::runtime_error>("hi");
386 EXPECT_TRUE(ew.with_exception([](std::runtime_error&) { return 3; }));
387 EXPECT_TRUE(ew.with_exception([](std::exception&) { return "hello"; }));
388 EXPECT_FALSE(ew.with_exception([](std::logic_error&) { return nullptr; }));
392 template <typename T>
393 T& r_to_l(T v) { return std::ref(v); }
396 TEST(ExceptionWrapper, with_exception_deduction_functor_lvalue) {
397 auto ew = make_exception_wrapper<std::runtime_error>("hi");
398 EXPECT_TRUE(ew.with_exception(r_to_l([](std::runtime_error&) {})));
399 EXPECT_TRUE(ew.with_exception(r_to_l([](std::exception&) {})));
400 EXPECT_FALSE(ew.with_exception(r_to_l([](std::logic_error&) {})));
403 TEST(ExceptionWrapper, non_std_exception_test) {
406 exception_wrapper ew = try_and_catch<std::exception, int>(
410 EXPECT_TRUE(bool(ew));
411 EXPECT_FALSE(ew.is_compatible_with<std::exception>());
412 EXPECT_TRUE(ew.is_compatible_with<int>());
413 EXPECT_EQ(ew.what(), kIntClassName);
414 EXPECT_EQ(ew.class_name(), kIntClassName);
415 // non-std::exception types are supported, but the only way to
416 // access their value is to explicity rethrow and catch it.
418 ew.throw_exception();
419 } catch /* nolint */ (int& i) {
420 EXPECT_EQ(i, expected);
425 TEST(ExceptionWrapper, exceptionStr) {
426 auto ew = make_exception_wrapper<std::runtime_error>("argh");
427 EXPECT_EQ(kRuntimeErrorClassName + ": argh", exceptionStr(ew));
430 TEST(ExceptionWrapper, throwException_noException) {
431 exception_wrapper ew;
432 ASSERT_DEATH(ew.throw_exception(), "empty folly::exception_wrapper");
436 class TestException : public std::exception { };
437 void testEW(const exception_wrapper& ew) {
438 EXPECT_THROW(ew.throw_exception(), TestException);
442 TEST(ExceptionWrapper, implicitConstruction) {
443 // Try with both lvalue and rvalue references
446 testEW(TestException());
450 struct BaseException {
451 virtual ~BaseException() {}
453 struct DerivedException : BaseException {};
454 exception_wrapper testNonStdException() {
456 throw DerivedException{};
457 } catch (const BaseException& e) {
458 return exception_wrapper{std::current_exception(), e};
463 TEST(ExceptionWrapper, base_derived_non_std_exception_test) {
464 auto ew = testNonStdException();
465 EXPECT_TRUE(ew.type() == typeid(DerivedException));
466 EXPECT_TRUE(ew.with_exception([](const DerivedException&) {}));
470 // Cannot be stored within an exception_wrapper
471 struct BigRuntimeError : std::runtime_error {
472 using std::runtime_error::runtime_error;
473 char data_[sizeof(exception_wrapper) + 1]{};
476 struct BigNonStdError {
477 char data_[sizeof(exception_wrapper) + 1]{};
481 TEST(ExceptionWrapper, handle_std_exception) {
482 auto ep = std::make_exception_ptr(std::runtime_error{"hello world"});
483 exception_wrapper const ew_eptr(ep, from_eptr<std::runtime_error>(ep));
484 exception_wrapper const ew_small(std::runtime_error{"hello world"});
485 exception_wrapper const ew_big(BigRuntimeError{"hello world"});
487 bool handled = false;
488 auto expect_runtime_error_yes_catch_all = [&](const exception_wrapper& ew) {
490 [](const std::logic_error&) { EXPECT_TRUE(false); },
491 [&](const std::runtime_error&) { handled = true; },
492 [](const std::exception&) { EXPECT_TRUE(false); },
493 [](...) { EXPECT_TRUE(false); });
496 expect_runtime_error_yes_catch_all(ew_eptr);
497 EXPECT_TRUE(handled);
499 expect_runtime_error_yes_catch_all(ew_small);
500 EXPECT_TRUE(handled);
502 expect_runtime_error_yes_catch_all(ew_big);
503 EXPECT_TRUE(handled);
506 auto expect_runtime_error_no_catch_all = [&](const exception_wrapper& ew) {
508 [](const std::logic_error&) { EXPECT_TRUE(false); },
509 [&](const std::runtime_error&) { handled = true; },
510 [](const std::exception&) { EXPECT_TRUE(false); });
513 expect_runtime_error_no_catch_all(ew_eptr);
514 EXPECT_TRUE(handled);
516 expect_runtime_error_no_catch_all(ew_small);
517 EXPECT_TRUE(handled);
519 expect_runtime_error_no_catch_all(ew_big);
520 EXPECT_TRUE(handled);
523 auto expect_runtime_error_catch_non_std = [&](const exception_wrapper& ew) {
525 [](const std::logic_error&) { EXPECT_TRUE(false); },
526 [&](const std::runtime_error&) { handled = true; },
527 [](const std::exception&) { EXPECT_TRUE(false); },
528 [](const int&) { EXPECT_TRUE(false); });
531 expect_runtime_error_catch_non_std(ew_eptr);
532 EXPECT_TRUE(handled);
534 expect_runtime_error_catch_non_std(ew_small);
535 EXPECT_TRUE(handled);
537 expect_runtime_error_catch_non_std(ew_big);
538 EXPECT_TRUE(handled);
541 // Test that an exception thrown from one handler is not caught by an
543 auto expect_runtime_error_rethrow = [&](const exception_wrapper& ew) {
545 [](const std::logic_error&) { EXPECT_TRUE(false); },
546 [&](const std::runtime_error& e) {
550 [](const std::exception&) { EXPECT_TRUE(false); });
553 EXPECT_THROW(expect_runtime_error_rethrow(ew_eptr), std::runtime_error);
554 EXPECT_TRUE(handled);
556 EXPECT_THROW(expect_runtime_error_rethrow(ew_small), std::runtime_error);
557 EXPECT_TRUE(handled);
559 EXPECT_THROW(expect_runtime_error_rethrow(ew_big), std::runtime_error);
560 EXPECT_TRUE(handled);
563 TEST(ExceptionWrapper, handle_std_exception_unhandled) {
564 auto ep = std::make_exception_ptr(std::exception{});
565 exception_wrapper const ew_eptr(ep, from_eptr<std::exception>(ep));
566 exception_wrapper const ew_small(std::exception{});
568 bool handled = false;
569 auto expect_runtime_error_yes_catch_all = [&](const exception_wrapper& ew) {
571 [](const std::logic_error&) { EXPECT_TRUE(false); },
572 [](const std::runtime_error&) { EXPECT_TRUE(false); },
573 [&](...) { handled = true; });
576 expect_runtime_error_yes_catch_all(ew_eptr);
577 EXPECT_TRUE(handled);
579 expect_runtime_error_yes_catch_all(ew_small);
580 EXPECT_TRUE(handled);
583 TEST(ExceptionWrapper, handle_non_std_exception_small) {
584 auto ep = std::make_exception_ptr(42);
585 exception_wrapper const ew_eptr1(ep);
586 exception_wrapper const ew_eptr2(ep, from_eptr<int>(ep));
587 exception_wrapper const ew_small(folly::in_place, 42);
588 bool handled = false;
590 auto expect_int_yes_catch_all = [&](const exception_wrapper& ew) {
592 [](const std::exception&) { EXPECT_TRUE(false); },
593 [&](...) { handled = true; });
596 expect_int_yes_catch_all(ew_eptr1);
597 EXPECT_TRUE(handled);
599 expect_int_yes_catch_all(ew_eptr2);
600 EXPECT_TRUE(handled);
602 expect_int_yes_catch_all(ew_small);
603 EXPECT_TRUE(handled);
606 auto expect_int_no_catch_all = [&](const exception_wrapper& ew) {
608 [](const std::exception&) { EXPECT_TRUE(false); },
609 [&](const int&) { handled = true; });
612 expect_int_no_catch_all(ew_eptr1);
613 EXPECT_TRUE(handled);
615 expect_int_no_catch_all(ew_eptr2);
616 EXPECT_TRUE(handled);
618 expect_int_no_catch_all(ew_small);
619 EXPECT_TRUE(handled);
622 auto expect_int_no_catch_all_2 = [&](const exception_wrapper& ew) {
624 [&](const int&) { handled = true; },
625 [](const std::exception&) { EXPECT_TRUE(false); });
628 expect_int_no_catch_all_2(ew_eptr1);
629 EXPECT_TRUE(handled);
631 expect_int_no_catch_all_2(ew_eptr2);
632 EXPECT_TRUE(handled);
634 expect_int_no_catch_all_2(ew_small);
635 EXPECT_TRUE(handled);
638 TEST(ExceptionWrapper, handle_non_std_exception_big) {
639 auto ep = std::make_exception_ptr(BigNonStdError{});
640 exception_wrapper const ew_eptr1(ep);
641 exception_wrapper const ew_eptr2(ep, from_eptr<BigNonStdError>(ep));
642 exception_wrapper const ew_big(folly::in_place, BigNonStdError{});
643 bool handled = false;
645 auto expect_int_yes_catch_all = [&](const exception_wrapper& ew) {
647 [](const std::exception&) { EXPECT_TRUE(false); },
648 [&](...) { handled = true; });
651 expect_int_yes_catch_all(ew_eptr1);
652 EXPECT_TRUE(handled);
654 expect_int_yes_catch_all(ew_eptr2);
655 EXPECT_TRUE(handled);
657 expect_int_yes_catch_all(ew_big);
658 EXPECT_TRUE(handled);
661 auto expect_int_no_catch_all = [&](const exception_wrapper& ew) {
663 [](const std::exception&) { EXPECT_TRUE(false); },
664 [&](const BigNonStdError&) { handled = true; });
667 expect_int_no_catch_all(ew_eptr1);
668 EXPECT_TRUE(handled);
670 expect_int_no_catch_all(ew_eptr2);
671 EXPECT_TRUE(handled);
673 expect_int_no_catch_all(ew_big);
674 EXPECT_TRUE(handled);
677 auto expect_int_no_catch_all_2 = [&](const exception_wrapper& ew) {
679 [&](const BigNonStdError&) { handled = true; },
680 [](const std::exception&) { EXPECT_TRUE(false); });
683 expect_int_no_catch_all_2(ew_eptr1);
684 EXPECT_TRUE(handled);
686 expect_int_no_catch_all_2(ew_eptr2);
687 EXPECT_TRUE(handled);
689 expect_int_no_catch_all_2(ew_big);
690 EXPECT_TRUE(handled);
694 expect_int_no_catch_all_2(exception_wrapper{folly::in_place, 42}), int);
697 TEST(ExceptionWrapper, handle_non_std_exception_rethrow_base_derived) {
698 auto ew = testNonStdException();
699 bool handled = false;
702 [&](const DerivedException& e) {
706 [](const BaseException&) { EXPECT_TRUE(false); }),
708 EXPECT_TRUE(handled);
712 [&](const DerivedException& e) {
716 [](...) { EXPECT_TRUE(false); }),
718 EXPECT_TRUE(handled);
721 TEST(ExceptionWrapper, self_swap_test) {
722 exception_wrapper ew(std::runtime_error("hello world"));
724 EXPECT_STREQ("std::runtime_error: hello world", ew.what().c_str());
726 ew = std::move(ew2); // should not crash