utils.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* Tries to convert an exception ptr into its equivalent error code
  2. (C) 2017-2019 Niall Douglas <http://www.nedproductions.biz/> (11 commits)
  3. File Created: July 2017
  4. Boost Software License - Version 1.0 - August 17th, 2003
  5. Permission is hereby granted, free of charge, to any person or organization
  6. obtaining a copy of the software and accompanying documentation covered by
  7. this license (the "Software") to use, reproduce, display, distribute,
  8. execute, and transmit the Software, and to prepare derivative works of the
  9. Software, and to permit third-parties to whom the Software is furnished to
  10. do so, all subject to the following:
  11. The copyright notices in the Software and this entire statement, including
  12. the above license grant, this restriction and the following disclaimer,
  13. must be included in all copies of the Software, in whole or in part, and
  14. all derivative works of the Software, unless such copies or derivative
  15. works are solely in the form of machine-executable object code generated by
  16. a source language processor.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  20. SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  21. FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  22. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. DEALINGS IN THE SOFTWARE.
  24. */
  25. #ifndef BOOST_OUTCOME_UTILS_HPP
  26. #define BOOST_OUTCOME_UTILS_HPP
  27. #include "config.hpp"
  28. #include <exception>
  29. #include <string>
  30. #include <system_error>
  31. BOOST_OUTCOME_V2_NAMESPACE_BEGIN
  32. #ifndef BOOST_NO_EXCEPTIONS
  33. /*! AWAITING HUGO JSON CONVERSION TOOL
  34. SIGNATURE NOT RECOGNISED
  35. */
  36. inline std::error_code error_from_exception(std::exception_ptr &&ep = std::current_exception(), std::error_code not_matched = std::make_error_code(std::errc::resource_unavailable_try_again)) noexcept
  37. {
  38. if(!ep)
  39. {
  40. return {};
  41. }
  42. try
  43. {
  44. std::rethrow_exception(ep);
  45. }
  46. catch(const std::invalid_argument & /*unused*/)
  47. {
  48. ep = std::exception_ptr();
  49. return std::make_error_code(std::errc::invalid_argument);
  50. }
  51. catch(const std::domain_error & /*unused*/)
  52. {
  53. ep = std::exception_ptr();
  54. return std::make_error_code(std::errc::argument_out_of_domain);
  55. }
  56. catch(const std::length_error & /*unused*/)
  57. {
  58. ep = std::exception_ptr();
  59. return std::make_error_code(std::errc::argument_list_too_long);
  60. }
  61. catch(const std::out_of_range & /*unused*/)
  62. {
  63. ep = std::exception_ptr();
  64. return std::make_error_code(std::errc::result_out_of_range);
  65. }
  66. catch(const std::logic_error & /*unused*/) /* base class for this group */
  67. {
  68. ep = std::exception_ptr();
  69. return std::make_error_code(std::errc::invalid_argument);
  70. }
  71. catch(const std::system_error &e) /* also catches ios::failure */
  72. {
  73. ep = std::exception_ptr();
  74. return e.code();
  75. }
  76. catch(const std::overflow_error & /*unused*/)
  77. {
  78. ep = std::exception_ptr();
  79. return std::make_error_code(std::errc::value_too_large);
  80. }
  81. catch(const std::range_error & /*unused*/)
  82. {
  83. ep = std::exception_ptr();
  84. return std::make_error_code(std::errc::result_out_of_range);
  85. }
  86. catch(const std::runtime_error & /*unused*/) /* base class for this group */
  87. {
  88. ep = std::exception_ptr();
  89. return std::make_error_code(std::errc::resource_unavailable_try_again);
  90. }
  91. catch(const std::bad_alloc & /*unused*/)
  92. {
  93. ep = std::exception_ptr();
  94. return std::make_error_code(std::errc::not_enough_memory);
  95. }
  96. catch(...)
  97. {
  98. }
  99. return not_matched;
  100. }
  101. /*! AWAITING HUGO JSON CONVERSION TOOL
  102. SIGNATURE NOT RECOGNISED
  103. */
  104. inline void try_throw_std_exception_from_error(std::error_code ec, const std::string &msg = std::string{})
  105. {
  106. if(!ec || (ec.category() != std::generic_category()
  107. #ifndef _WIN32
  108. && ec.category() != std::system_category()
  109. #endif
  110. ))
  111. {
  112. return;
  113. }
  114. switch(ec.value())
  115. {
  116. case EINVAL:
  117. throw msg.empty() ? std::invalid_argument("invalid argument") : std::invalid_argument(msg);
  118. case EDOM:
  119. throw msg.empty() ? std::domain_error("domain error") : std::domain_error(msg);
  120. case E2BIG:
  121. throw msg.empty() ? std::length_error("length error") : std::length_error(msg);
  122. case ERANGE:
  123. throw msg.empty() ? std::out_of_range("out of range") : std::out_of_range(msg);
  124. case EOVERFLOW:
  125. throw msg.empty() ? std::overflow_error("overflow error") : std::overflow_error(msg);
  126. case ENOMEM:
  127. throw std::bad_alloc();
  128. }
  129. }
  130. #endif
  131. BOOST_OUTCOME_V2_NAMESPACE_END
  132. #endif