From fe77bf9d7d45cdf953c5745861b6db0c12fe31a5 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Fri, 13 Apr 2012 11:23:13 +1000 Subject: [PATCH] Add and use type_error for type conversion failure --- json.cpp.rl | 14 +++++++------- json.hpp | 8 ++++++++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/json.cpp.rl b/json.cpp.rl index b9896b4e..b5c2a364 100644 --- a/json.cpp.rl +++ b/json.cpp.rl @@ -282,32 +282,32 @@ json::write (const json::node &node, std::ostream &os) const json::object& json::node::as_object (void) const - { throw parse_error ("node is not an object"); } + { throw type_error ("node is not an object"); } const json::array& json::node::as_array (void) const - { throw parse_error ("node is not an array"); } + { throw type_error ("node is not an array"); } const json::string& json::node::as_string (void) const - { throw parse_error ("node is not a string"); } + { throw type_error ("node is not a string"); } const json::number& json::node::as_number (void) const - { throw parse_error ("node is not a number"); } + { throw type_error ("node is not a number"); } const json::boolean& json::node::as_boolean (void) const - { throw parse_error ("node is not a boolean"); } + { throw type_error ("node is not a boolean"); } const json::null& json::node::as_null (void) const - { throw parse_error ("node is not a null"); } + { throw type_error ("node is not a null"); } bool @@ -363,7 +363,7 @@ json::object::operator[](const std::string &key) const { if (value == m_values.end ()) { ostringstream ss; ss << "no key: " << key; - throw json::parse_error (ss.str()); + throw json::error (ss.str()); } return *value->second; diff --git a/json.hpp b/json.hpp index b7c2bf7a..5d01e74a 100644 --- a/json.hpp +++ b/json.hpp @@ -226,6 +226,14 @@ namespace json { { ; } }; + /// Base class for all type conversion errors + class type_error : public error { + public: + type_error (const std::string &_what): + error (_what) + { ; } + }; + /// Base class for errors thrown during parsing class parse_error : public error { public: