From b3b5682cc083f220c6cc4ba6d236e88e6f677b57 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Thu, 6 Dec 2018 17:15:19 +0900 Subject: [PATCH] add message to bad_unwrap --- toml/result.hpp | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/toml/result.hpp b/toml/result.hpp index 67249f0..fde9729 100644 --- a/toml/result.hpp +++ b/toml/result.hpp @@ -1,8 +1,11 @@ #ifndef TOML11_RESULT_H #define TOML11_RESULT_H #include +#include #include #include +#include +#include #include namespace toml @@ -365,17 +368,29 @@ struct result value_type& unwrap() & { - if(is_err()) {throw std::runtime_error("result: bad unwrap");} + if(is_err()) + { + throw std::runtime_error("result: bad unwrap: " + + format_error(this->as_err())); + } return this->succ.value; } value_type const& unwrap() const& { - if(is_err()) {throw std::runtime_error("result: bad unwrap");} + if(is_err()) + { + throw std::runtime_error("result: bad unwrap: " + + format_error(this->as_err())); + } return this->succ.value; } value_type&& unwrap() && { - if(is_err()) {throw std::runtime_error("result: bad unwrap");} + if(is_err()) + { + throw std::runtime_error("result: bad unwrap: " + + format_error(this->as_err())); + } return std::move(this->succ.value); } @@ -541,6 +556,18 @@ struct result private: + static std::string format_error(std::exception const& excpt) + { + return std::string(excpt.what()); + } + template::value, std::nullptr_t>::type = nullptr> + static std::string format_error(U const& others) + { + std::ostringstream oss; oss << others; + return oss.str(); + } + void cleanup() noexcept { if(this->is_ok_) {this->succ.~success_type();}