Add and use type_error for type conversion failure

This commit is contained in:
Danny Robson 2012-04-13 11:23:13 +10:00
parent 86b5abde08
commit fe77bf9d7d
2 changed files with 15 additions and 7 deletions

View File

@ -282,32 +282,32 @@ json::write (const json::node &node, std::ostream &os)
const json::object& const json::object&
json::node::as_object (void) const 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& const json::array&
json::node::as_array (void) const 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& const json::string&
json::node::as_string (void) const 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& const json::number&
json::node::as_number (void) const 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& const json::boolean&
json::node::as_boolean (void) const 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& const json::null&
json::node::as_null (void) const json::node::as_null (void) const
{ throw parse_error ("node is not a null"); } { throw type_error ("node is not a null"); }
bool bool
@ -363,7 +363,7 @@ json::object::operator[](const std::string &key) const {
if (value == m_values.end ()) { if (value == m_values.end ()) {
ostringstream ss; ostringstream ss;
ss << "no key: " << key; ss << "no key: " << key;
throw json::parse_error (ss.str()); throw json::error (ss.str());
} }
return *value->second; return *value->second;

View File

@ -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 /// Base class for errors thrown during parsing
class parse_error : public error { class parse_error : public error {
public: public: