system_code_from_exception.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* Proposed SG14 status_code
  2. (C) 2018-2019 Niall Douglas <http://www.nedproductions.biz/> (5 commits)
  3. File Created: June 2018
  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_SYSTEM_ERROR2_SYSTEM_CODE_FROM_EXCEPTION_HPP
  26. #define BOOST_OUTCOME_SYSTEM_ERROR2_SYSTEM_CODE_FROM_EXCEPTION_HPP
  27. #include "system_code.hpp"
  28. #include <exception> // for exception_ptr
  29. #include <stdexcept> // for the exception types
  30. #include <system_error> // for std::system_error
  31. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
  32. /*! A utility function which returns the closest matching system_code to a supplied
  33. exception ptr.
  34. */
  35. inline system_code system_code_from_exception(std::exception_ptr &&ep = std::current_exception(), system_code not_matched = generic_code(errc::resource_unavailable_try_again)) noexcept
  36. {
  37. if(!ep)
  38. {
  39. return generic_code(errc::success);
  40. }
  41. try
  42. {
  43. std::rethrow_exception(ep);
  44. }
  45. catch(const std::invalid_argument & /*unused*/)
  46. {
  47. ep = std::exception_ptr();
  48. return generic_code(errc::invalid_argument);
  49. }
  50. catch(const std::domain_error & /*unused*/)
  51. {
  52. ep = std::exception_ptr();
  53. return generic_code(errc::argument_out_of_domain);
  54. }
  55. catch(const std::length_error & /*unused*/)
  56. {
  57. ep = std::exception_ptr();
  58. return generic_code(errc::argument_list_too_long);
  59. }
  60. catch(const std::out_of_range & /*unused*/)
  61. {
  62. ep = std::exception_ptr();
  63. return generic_code(errc::result_out_of_range);
  64. }
  65. catch(const std::logic_error & /*unused*/) /* base class for this group */
  66. {
  67. ep = std::exception_ptr();
  68. return generic_code(errc::invalid_argument);
  69. }
  70. catch(const std::system_error &e) /* also catches ios::failure */
  71. {
  72. ep = std::exception_ptr();
  73. if(e.code().category() == std::generic_category())
  74. {
  75. return generic_code(static_cast<errc>(static_cast<int>(e.code().value())));
  76. }
  77. if(e.code().category() == std::system_category())
  78. {
  79. #ifdef _WIN32
  80. return win32_code(e.code().value());
  81. #else
  82. return posix_code(e.code().value());
  83. #endif
  84. }
  85. // Don't know this error code category, can't wrap it into std_error_code
  86. // as its payload won't fit into system_code, so fall through.
  87. }
  88. catch(const std::overflow_error & /*unused*/)
  89. {
  90. ep = std::exception_ptr();
  91. return generic_code(errc::value_too_large);
  92. }
  93. catch(const std::range_error & /*unused*/)
  94. {
  95. ep = std::exception_ptr();
  96. return generic_code(errc::result_out_of_range);
  97. }
  98. catch(const std::runtime_error & /*unused*/) /* base class for this group */
  99. {
  100. ep = std::exception_ptr();
  101. return generic_code(errc::resource_unavailable_try_again);
  102. }
  103. catch(const std::bad_alloc & /*unused*/)
  104. {
  105. ep = std::exception_ptr();
  106. return generic_code(errc::not_enough_memory);
  107. }
  108. catch(...)
  109. {
  110. }
  111. return not_matched;
  112. }
  113. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
  114. #endif