123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- BOOST_OUTCOME_V2_NAMESPACE_BEGIN
- 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
- {
- if(!ep)
- {
- return {};
- }
- try
- {
- std::rethrow_exception(ep);
- }
- catch(const std::invalid_argument & )
- {
- ep = std::exception_ptr();
- return std::make_error_code(std::errc::invalid_argument);
- }
- catch(const std::domain_error & )
- {
- ep = std::exception_ptr();
- return std::make_error_code(std::errc::argument_out_of_domain);
- }
- catch(const std::length_error & )
- {
- ep = std::exception_ptr();
- return std::make_error_code(std::errc::argument_list_too_long);
- }
- catch(const std::out_of_range & )
- {
- ep = std::exception_ptr();
- return std::make_error_code(std::errc::result_out_of_range);
- }
- catch(const std::logic_error & )
- {
- ep = std::exception_ptr();
- return std::make_error_code(std::errc::invalid_argument);
- }
- catch(const std::system_error &e)
- {
- ep = std::exception_ptr();
- return e.code();
- }
- catch(const std::overflow_error & )
- {
- ep = std::exception_ptr();
- return std::make_error_code(std::errc::value_too_large);
- }
- catch(const std::range_error & )
- {
- ep = std::exception_ptr();
- return std::make_error_code(std::errc::result_out_of_range);
- }
- catch(const std::runtime_error & )
- {
- ep = std::exception_ptr();
- return std::make_error_code(std::errc::resource_unavailable_try_again);
- }
- catch(const std::bad_alloc & )
- {
- ep = std::exception_ptr();
- return std::make_error_code(std::errc::not_enough_memory);
- }
- catch(...)
- {
- }
- return not_matched;
- }
- inline void try_throw_std_exception_from_error(std::error_code ec, const std::string &msg = std::string{})
- {
- if(!ec || (ec.category() != std::generic_category()
- && ec.category() != std::system_category()
- ))
- {
- return;
- }
- switch(ec.value())
- {
- case EINVAL:
- throw msg.empty() ? std::invalid_argument("invalid argument") : std::invalid_argument(msg);
- case EDOM:
- throw msg.empty() ? std::domain_error("domain error") : std::domain_error(msg);
- case E2BIG:
- throw msg.empty() ? std::length_error("length error") : std::length_error(msg);
- case ERANGE:
- throw msg.empty() ? std::out_of_range("out of range") : std::out_of_range(msg);
- case EOVERFLOW:
- throw msg.empty() ? std::overflow_error("overflow error") : std::overflow_error(msg);
- case ENOMEM:
- throw std::bad_alloc();
- }
- }
- BOOST_OUTCOME_V2_NAMESPACE_END
|