From c15bc8df4a43ce355a1f6c9cccc2b846b14bd097 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Sun, 16 Dec 2018 21:46:32 +0900 Subject: [PATCH 1/3] add format_error(toml::value, msg, comment) --- toml/value.hpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/toml/value.hpp b/toml/value.hpp index 68c063a..0d14fe7 100644 --- a/toml/value.hpp +++ b/toml/value.hpp @@ -800,5 +800,11 @@ inline bool operator>=(const toml::value& lhs, const toml::value& rhs) return !(lhs < rhs); } +inline std::string format_error(const toml::value& v, + const std::string& error_msg, const std::string& comment) +{ + return detail::format_underline(error_msg, detail::get_region(v), comment); +} + }// toml #endif// TOML11_VALUE From ab41e7acb9a1b4ab716a34ee4ec368d9f8fc5ee6 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Sun, 16 Dec 2018 21:50:18 +0900 Subject: [PATCH 2/3] enable to pass 2 value and change interface for clarity --- toml/value.hpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/toml/value.hpp b/toml/value.hpp index 0d14fe7..5d7898c 100644 --- a/toml/value.hpp +++ b/toml/value.hpp @@ -800,10 +800,18 @@ inline bool operator>=(const toml::value& lhs, const toml::value& rhs) return !(lhs < rhs); } -inline std::string format_error(const toml::value& v, - const std::string& error_msg, const std::string& comment) +inline std::string format_error(const std::string& err_msg, + const toml::value& v, const std::string& comment) { - return detail::format_underline(error_msg, detail::get_region(v), comment); + return detail::format_underline(err_msg, detail::get_region(v), comment); +} + +inline std::string format_error(const std::string& err_msg, + const toml::value& v1, const std::string& comment1, + const toml::value& v2, const std::string& comment2) +{ + return detail::format_underline(err_msg, detail::get_region(v1), comment1, + detail::get_region(v2), comment2); } }// toml From 130609bf5f3350e0c8546bec97c460f74b0b7285 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Sun, 16 Dec 2018 23:51:38 +0900 Subject: [PATCH 3/3] update README --- README.md | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/README.md b/README.md index 7f62b2c..208c4b5 100644 --- a/README.md +++ b/README.md @@ -402,6 +402,59 @@ terminate called after throwing an instance of 'std::range_error' | ~~~~~~~~~ should be in [0x00..0x10FFFF] ``` +### Formatting your error + +When you encounter an error after you read the toml value, you may want to +show the error with the value. + +toml11 provides you a function that formats user-defined error message with +related values. With a code like the following, + +```cpp +const auto value = toml::find(data, "num"); +if(value < 0) +{ + std::cerr << toml::format_error("[error] value should be positive", + data.at("num"), "positive number required") + << std::endl; +} +``` + +you will get an error message like this. + +```console +[error] value should be positive + --> example.toml + 3 | num = -42 + | ~~~ positive number required +``` + +When you pass two values to `toml::format_error`, + +```cpp +const auto min = toml::find(range, "min"); +const auto max = toml::find(range, "max"); +if(max < min) +{ + std::cerr << toml::format_error("[error] max should be larger than min", + data.at("min"), "minimum number here", + data.at("max"), "maximum number here"); + << std::endl; +} +``` + +you will get an error message like this. + +```console +[error] value should be positive + --> example.toml + 3 | min = 54 + | ~~ minimum number here + ... + 4 | max = 42 + | ~~ maximum number here +``` + ## Underlying types The toml types (can be used as `toml::*` in this library) and corresponding `enum` names are listed in the table below.