0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-26 15:33:54 +01:00

ircd::json: Add why() to specifically return err str from valid().

This commit is contained in:
Jason Volk 2018-03-28 20:47:26 -07:00
parent 738eb0782e
commit 7ad99ab387
3 changed files with 25 additions and 3 deletions

View file

@ -138,7 +138,10 @@ ircd::json::print(const mutable_buffer &buf,
if(unlikely(!valid(sv, std::nothrow))) //note: false alarm when T=json::member
throw print_error
{
"print (%zu): %s", sv.size(), sv
"print %zu bytes: %s: %s",
sv.size(),
why(sv),
sv
};
buf[sv.size()] = '\0';
@ -161,13 +164,19 @@ ircd::json::strung::strung(T&&... t)
if(unlikely(size(sv) != size(out)))
throw print_error
{
"stringified:%zu != serialized:%zu: %s", size(sv), size(out), sv
"stringified:%zu != serialized:%zu: %s",
size(sv),
size(out),
sv
};
if(unlikely(!valid(sv, std::nothrow))) //note: false alarm when T=json::member
throw print_error
{
"strung (%zu): %s", size(sv), sv
"strung %zu bytes: %s: %s",
size(sv),
why(sv),
sv
};
return sv;

View file

@ -37,6 +37,7 @@ namespace ircd::json
// Validate JSON - checks if valid JSON (not canonical).
bool valid(const string_view &, std::nothrow_t) noexcept;
void valid(const string_view &);
std::string why(const string_view &);
}
inline std::ostream &

View file

@ -2175,6 +2175,18 @@ ircd::json::operator==(const value &a, const value &b)
// json.h
//
std::string
ircd::json::why(const string_view &s)
try
{
valid(s);
return {};
}
catch(const expectation_failure &e)
{
return e.what();
}
bool
ircd::json::valid(const string_view &s,
std::nothrow_t)