0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-29 02:02:38 +01:00

ircd::json: Clean up expectation failure catching.

This commit is contained in:
Jason Volk 2017-11-06 12:07:57 -08:00
parent fecf2afff9
commit d09a3096be

View file

@ -54,6 +54,8 @@ namespace ircd::json
using karma::eps; using karma::eps;
using karma::attr_cast; using karma::attr_cast;
struct expectation_failure;
template<class it> struct input; template<class it> struct input;
template<class it> struct output; template<class it> struct output;
@ -204,6 +206,22 @@ struct ircd::json::output
{} {}
}; };
struct ircd::json::expectation_failure
:parse_error
{
expectation_failure(const char *const &start,
const qi::expectation_failure<const char *> &e,
const ssize_t &show_max = 64)
:parse_error
{
"Expected %s. You input %zd invalid characters at position %zd: %s",
ircd::string(e.what_),
std::distance(e.first, e.last),
std::distance(start, e.first),
string_view{e.first, e.first + std::min(std::distance(e.first, e.last), show_max)}
}{}
};
struct ircd::json::parser struct ircd::json::parser
:input<const char *> :input<const char *>
{ {
@ -637,14 +655,7 @@ try
} }
catch(const qi::expectation_failure<const char *> &e) catch(const qi::expectation_failure<const char *> &e)
{ {
const auto rule(ircd::string(e.what_)); throw expectation_failure(start, e);
const long size(std::distance(e.first, e.last));
const long cat(std::distance(start, e.first));
throw parse_error("Expected %s. You input %zd invalid characters at position %zd: %s",
between(rule, "<", ">"),
size,
cat,
string_view(e.first, e.first + std::min(size, 64L)));
} }
ircd::json::object::operator std::string() ircd::json::object::operator std::string()
@ -674,21 +685,14 @@ const try
string_view::begin(), string_view::end() string_view::begin(), string_view::end()
}; };
if(!empty()) if(!string_view{*this}.empty())
qi::parse(ret.start, ret.stop, eps > parse_begin, ret.state); qi::parse(ret.start, ret.stop, eps > parse_begin, ret.state);
return ret; return ret;
} }
catch(const qi::expectation_failure<const char *> &e) catch(const qi::expectation_failure<const char *> &e)
{ {
const auto rule(ircd::string(e.what_)); throw expectation_failure(string_view::begin(), e);
const long size(std::distance(e.first, e.last));
const long cat(std::distance(string_view::data(), e.first));
throw parse_error("Expected %s. You input %zd invalid characters at position %zd: %s.",
between(rule, "<", ">"),
size,
cat,
string_view(e.first, e.first + std::min(size, 64L)));
} }
ircd::json::object::const_iterator ircd::json::object::const_iterator
@ -794,12 +798,7 @@ try
} }
catch(const qi::expectation_failure<const char *> &e) catch(const qi::expectation_failure<const char *> &e)
{ {
const auto rule(ircd::string(e.what_)); throw expectation_failure(start, e);
const long size(std::distance(e.first, e.last));
throw parse_error("Expected JSON %s. You input %zd invalid characters starting with `%s`.",
between(rule, "<", ">"),
size,
string_view(e.first, e.first + std::min(size, 64L)));
} }
ircd::json::array::operator std::string() ircd::json::array::operator std::string()
@ -823,19 +822,14 @@ const try
string_view::begin(), string_view::end() string_view::begin(), string_view::end()
}; };
if(!empty()) if(!string_view{*this}.empty())
qi::parse(ret.start, ret.stop, eps > parse_begin, ret.state); qi::parse(ret.start, ret.stop, eps > parse_begin, ret.state);
return ret; return ret;
} }
catch(const qi::expectation_failure<const char *> &e) catch(const qi::expectation_failure<const char *> &e)
{ {
const auto rule(ircd::string(e.what_)); throw expectation_failure(string_view::begin(), e);
const long size(std::distance(e.first, e.last));
throw parse_error("Expected JSON %s. You input %zd invalid characters starting with `%s`.",
between(rule, "<", ">"),
size,
string_view(e.first, e.first + std::min(size, 64L)));
} }
ircd::json::array::const_iterator ircd::json::array::const_iterator