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:
parent
738eb0782e
commit
7ad99ab387
3 changed files with 25 additions and 3 deletions
|
@ -138,7 +138,10 @@ ircd::json::print(const mutable_buffer &buf,
|
||||||
if(unlikely(!valid(sv, std::nothrow))) //note: false alarm when T=json::member
|
if(unlikely(!valid(sv, std::nothrow))) //note: false alarm when T=json::member
|
||||||
throw print_error
|
throw print_error
|
||||||
{
|
{
|
||||||
"print (%zu): %s", sv.size(), sv
|
"print %zu bytes: %s: %s",
|
||||||
|
sv.size(),
|
||||||
|
why(sv),
|
||||||
|
sv
|
||||||
};
|
};
|
||||||
|
|
||||||
buf[sv.size()] = '\0';
|
buf[sv.size()] = '\0';
|
||||||
|
@ -161,13 +164,19 @@ ircd::json::strung::strung(T&&... t)
|
||||||
if(unlikely(size(sv) != size(out)))
|
if(unlikely(size(sv) != size(out)))
|
||||||
throw print_error
|
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
|
if(unlikely(!valid(sv, std::nothrow))) //note: false alarm when T=json::member
|
||||||
throw print_error
|
throw print_error
|
||||||
{
|
{
|
||||||
"strung (%zu): %s", size(sv), sv
|
"strung %zu bytes: %s: %s",
|
||||||
|
size(sv),
|
||||||
|
why(sv),
|
||||||
|
sv
|
||||||
};
|
};
|
||||||
|
|
||||||
return sv;
|
return sv;
|
||||||
|
|
|
@ -37,6 +37,7 @@ namespace ircd::json
|
||||||
// Validate JSON - checks if valid JSON (not canonical).
|
// Validate JSON - checks if valid JSON (not canonical).
|
||||||
bool valid(const string_view &, std::nothrow_t) noexcept;
|
bool valid(const string_view &, std::nothrow_t) noexcept;
|
||||||
void valid(const string_view &);
|
void valid(const string_view &);
|
||||||
|
std::string why(const string_view &);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::ostream &
|
inline std::ostream &
|
||||||
|
|
12
ircd/json.cc
12
ircd/json.cc
|
@ -2175,6 +2175,18 @@ ircd::json::operator==(const value &a, const value &b)
|
||||||
// json.h
|
// json.h
|
||||||
//
|
//
|
||||||
|
|
||||||
|
std::string
|
||||||
|
ircd::json::why(const string_view &s)
|
||||||
|
try
|
||||||
|
{
|
||||||
|
valid(s);
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
catch(const expectation_failure &e)
|
||||||
|
{
|
||||||
|
return e.what();
|
||||||
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
ircd::json::valid(const string_view &s,
|
ircd::json::valid(const string_view &s,
|
||||||
std::nothrow_t)
|
std::nothrow_t)
|
||||||
|
|
Loading…
Reference in a new issue