ExceptionWrapper.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright 2016-present Facebook, Inc.
  3. *
  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
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  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.
  15. */
  16. #include <folly/ExceptionWrapper.h>
  17. #include <iostream>
  18. #include <folly/GLog.h>
  19. namespace folly {
  20. exception_wrapper::VTable const exception_wrapper::uninit_{
  21. &noop_<void, exception_wrapper const*, exception_wrapper*>,
  22. &noop_<void, exception_wrapper*, exception_wrapper*>,
  23. &noop_<void, exception_wrapper*>,
  24. &noop_<void, exception_wrapper const*>,
  25. &uninit_type_,
  26. &noop_<std::exception const*, exception_wrapper const*>,
  27. &noop_<exception_wrapper, exception_wrapper const*>};
  28. exception_wrapper::VTable const exception_wrapper::ExceptionPtr::ops_{
  29. copy_,
  30. move_,
  31. delete_,
  32. throw_,
  33. type_,
  34. get_exception_,
  35. get_exception_ptr_};
  36. exception_wrapper::VTable const exception_wrapper::SharedPtr::ops_{
  37. copy_,
  38. move_,
  39. delete_,
  40. throw_,
  41. type_,
  42. get_exception_,
  43. get_exception_ptr_};
  44. namespace {
  45. std::exception const* get_std_exception_(std::exception_ptr eptr) noexcept {
  46. try {
  47. std::rethrow_exception(eptr);
  48. } catch (const std::exception& ex) {
  49. return &ex;
  50. } catch (...) {
  51. return nullptr;
  52. }
  53. }
  54. } // namespace
  55. exception_wrapper exception_wrapper::from_exception_ptr(
  56. std::exception_ptr const& ptr) noexcept {
  57. if (!ptr) {
  58. return exception_wrapper();
  59. }
  60. try {
  61. std::rethrow_exception(ptr);
  62. } catch (std::exception& e) {
  63. return exception_wrapper(std::current_exception(), e);
  64. } catch (...) {
  65. return exception_wrapper(std::current_exception());
  66. }
  67. }
  68. exception_wrapper::exception_wrapper(std::exception_ptr ptr) noexcept
  69. : exception_wrapper{} {
  70. if (ptr) {
  71. if (auto e = get_std_exception_(ptr)) {
  72. LOG(DFATAL)
  73. << "Performance error: Please construct exception_wrapper with a "
  74. "reference to the std::exception along with the "
  75. "std::exception_ptr.";
  76. *this = exception_wrapper{std::move(ptr), *e};
  77. } else {
  78. Unknown uk;
  79. *this = exception_wrapper{ptr, uk};
  80. }
  81. }
  82. }
  83. [[noreturn]] void exception_wrapper::onNoExceptionError(
  84. char const* const name) {
  85. std::ios_base::Init ioinit_; // ensure std::cerr is alive
  86. std::cerr << "Cannot use `" << name
  87. << "` with an empty folly::exception_wrapper" << std::endl;
  88. std::terminate();
  89. }
  90. fbstring exceptionStr(exception_wrapper const& ew) {
  91. return ew.what();
  92. }
  93. } // namespace folly