From fae5ce8795080018875227aee8613677f92e99ce Mon Sep 17 00:00:00 2001 From: MacroFake Date: Mon, 18 Jul 2022 11:13:22 +0200 Subject: univalue: Return more detailed type check error messages --- src/univalue/include/univalue.h | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'src/univalue/include') diff --git a/src/univalue/include/univalue.h b/src/univalue/include/univalue.h index b665954927..dff544f96f 100644 --- a/src/univalue/include/univalue.h +++ b/src/univalue/include/univalue.h @@ -106,6 +106,7 @@ private: std::vector keys; std::vector values; + void checkType(const VType& expected) const; bool findKey(const std::string& key, size_t& retIdx) const; void writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const; void writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const; @@ -130,7 +131,7 @@ public: template void UniValue::push_backV(It first, It last) { - if (typ != VARR) throw std::runtime_error{"JSON value is not an array as expected"}; + checkType(VARR); values.insert(values.end(), first, last); } @@ -138,9 +139,7 @@ template Int UniValue::getInt() const { static_assert(std::is_integral::value); - if (typ != VNUM) { - throw std::runtime_error("JSON value is not an integer as expected"); - } + checkType(VNUM); Int result; const auto [first_nonmatching, error_condition] = std::from_chars(val.data(), val.data() + val.size(), result); if (first_nonmatching != val.data() + val.size() || error_condition != std::errc{}) { -- cgit v1.2.3