Split json::error into parse and schema errors

This commit is contained in:
Danny Robson 2012-04-12 14:36:05 +10:00
parent 937d9522e0
commit 040e565a18
2 changed files with 26 additions and 10 deletions

View File

@ -84,7 +84,7 @@ struct parse_context {
assert (nodestack.back ().value); assert (nodestack.back ().value);
if (!nodestack.back ().key->is_string ()) if (!nodestack.back ().key->is_string ())
throw error ("object keys must be strings"); throw parse_error ("object keys must be strings");
json::object *object = (json::object *)nodestack.back ().root; json::object *object = (json::object *)nodestack.back ().root;
object->insert (nodestack.back ().key->to_string (), object->insert (nodestack.back ().key->to_string (),
@ -115,7 +115,7 @@ struct parse_context {
assert (!nodestack.empty ()); assert (!nodestack.empty ());
assert (!nodestack.back ().value); assert (!nodestack.back ().value);
throw error ("unable to parse boolean"); throw parse_error ("unable to parse boolean");
} }
action new_number { action new_number {
@ -125,7 +125,7 @@ struct parse_context {
errno = 0; errno = 0;
double value = strtod (nodestack.back ().start, NULL); double value = strtod (nodestack.back ().start, NULL);
if (errno) if (errno)
throw error ("unable to parse number"); throw parse_error ("unable to parse number");
nodestack.back ().value = new json::number (value); nodestack.back ().value = new json::number (value);
} }
@ -261,7 +261,7 @@ json::parse (const char *start,
%%write exec; %%write exec;
if (!__success) if (!__success)
throw error ("unable to parse json"); throw parse_error ("unable to parse json");
//__root->print (cout) << endl; //__root->print (cout) << endl;
assert (*__root == *__root); assert (*__root == *__root);
@ -281,27 +281,27 @@ json::write (const json::node &node, std::ostream &os)
const json::object& const json::object&
json::node::to_object (void) const json::node::to_object (void) const
{ throw error ("node is not an object"); } { throw parse_error ("node is not an object"); }
const json::array& const json::array&
json::node::to_array (void) const json::node::to_array (void) const
{ throw error ("node is not an array"); } { throw parse_error ("node is not an array"); }
const json::string& const json::string&
json::node::to_string (void) const json::node::to_string (void) const
{ throw error ("node is not a string"); } { throw parse_error ("node is not a string"); }
const json::number& const json::number&
json::node::to_number (void) const json::node::to_number (void) const
{ throw error ("node is not a number"); } { throw parse_error ("node is not a number"); }
const json::boolean& const json::boolean&
json::node::to_boolean (void) const json::node::to_boolean (void) const
{ throw error ("node is not a boolean"); } { throw parse_error ("node is not a boolean"); }
bool bool
@ -359,7 +359,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::error (ss.str()); throw json::parse_error (ss.str());
} }
return *value->second; return *value->second;

View File

@ -222,6 +222,22 @@ namespace json {
std::runtime_error (_what) std::runtime_error (_what)
{ ; } { ; }
}; };
/// Base class for errors thrown during parsing
class parse_error : public error {
public:
parse_error (const std::string &_what):
error (_what)
{ ; }
};
/// Base class for errors thrown during schema validation
class schema_error : public error {
public:
schema_error (const std::string &_what):
error (_what)
{ ; }
};
} }