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
6 Boost Software License - Version 1.0 - August 17th, 2003
8 Permission is hereby granted, free of charge, to any person or organization
9 obtaining a copy of the software and accompanying documentation covered by
10 this license (the "Software") to use, reproduce, display, distribute,
11 execute, and transmit the Software, and to prepare derivative works of the
12 Software, and to permit third-parties to whom the Software is furnished to
13 do so, all subject to the following:
15 The copyright notices in the Software and this entire statement, including
16 the above license grant, this restriction and the following disclaimer,
17 must be included in all copies of the Software, in whole or in part, and
18 all derivative works of the Software, unless such copies or derivative
19 works are solely in the form of machine-executable object code generated by
20 a source language processor.
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
25 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
26 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
27 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 DEALINGS IN THE SOFTWARE.
31 #ifndef BOOST_OUTCOME_UTILS_HPP
32 #define BOOST_OUTCOME_UTILS_HPP
38 #include <system_error>
40 BOOST_OUTCOME_V2_NAMESPACE_BEGIN
42 #ifndef BOOST_NO_EXCEPTIONS
43 /*! AWAITING HUGO JSON CONVERSION TOOL
44 SIGNATURE NOT RECOGNISED
46 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
54 std::rethrow_exception(ep);
56 catch(const std::invalid_argument & /*unused*/)
58 ep = std::exception_ptr();
59 return std::make_error_code(std::errc::invalid_argument);
61 catch(const std::domain_error & /*unused*/)
63 ep = std::exception_ptr();
64 return std::make_error_code(std::errc::argument_out_of_domain);
66 catch(const std::length_error & /*unused*/)
68 ep = std::exception_ptr();
69 return std::make_error_code(std::errc::argument_list_too_long);
71 catch(const std::out_of_range & /*unused*/)
73 ep = std::exception_ptr();
74 return std::make_error_code(std::errc::result_out_of_range);
76 catch(const std::logic_error & /*unused*/) /* base class for this group */
78 ep = std::exception_ptr();
79 return std::make_error_code(std::errc::invalid_argument);
81 catch(const std::system_error &e) /* also catches ios::failure */
83 ep = std::exception_ptr();
86 catch(const std::overflow_error & /*unused*/)
88 ep = std::exception_ptr();
89 return std::make_error_code(std::errc::value_too_large);
91 catch(const std::range_error & /*unused*/)
93 ep = std::exception_ptr();
94 return std::make_error_code(std::errc::result_out_of_range);
96 catch(const std::runtime_error & /*unused*/) /* base class for this group */
98 ep = std::exception_ptr();
99 return std::make_error_code(std::errc::resource_unavailable_try_again);
101 catch(const std::bad_alloc & /*unused*/)
103 ep = std::exception_ptr();
104 return std::make_error_code(std::errc::not_enough_memory);
112 /*! AWAITING HUGO JSON CONVERSION TOOL
113 SIGNATURE NOT RECOGNISED
115 inline void try_throw_std_exception_from_error(std::error_code ec, const std::string &msg = std::string{})
117 if(!ec || (ec.category() != std::generic_category()
119 && ec.category() != std::system_category()
128 throw msg.empty() ? std::invalid_argument("invalid argument") : std::invalid_argument(msg);
130 throw msg.empty() ? std::domain_error("domain error") : std::domain_error(msg);
132 throw msg.empty() ? std::length_error("length error") : std::length_error(msg);
134 throw msg.empty() ? std::out_of_range("out of range") : std::out_of_range(msg);
136 throw msg.empty() ? std::overflow_error("overflow error") : std::overflow_error(msg);
138 throw std::bad_alloc();
143 BOOST_OUTCOME_V2_NAMESPACE_END