0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-25 16:22:35 +01:00

ircd::resource: Cleanup JSON iov related; Add exception handlers.

This commit is contained in:
Jason Volk 2017-09-15 12:07:07 -07:00
parent ceb172a958
commit 65802d3e47
2 changed files with 40 additions and 12 deletions

View file

@ -91,9 +91,9 @@ struct ircd::resource::request::body
struct ircd::resource::response
{
response(client &, const string_view &str, const string_view &ct = "text/plain; charset=utf8", const http::code & = http::OK);
response(client &, const json::object & = "{}", const http::code & = http::OK);
response(client &, const json::object &str, const http::code & = http::OK);
response(client &, const json::members & = {}, const http::code & = http::OK);
response(client &, const json::iov &, const http::code & = http::OK);
response(client &, const json::members &, const http::code & = http::OK);
response(client &, const http::code &, const json::members &);
response(client &, const http::code &, const json::iov &);
response(client &, const http::code &);

View file

@ -179,13 +179,27 @@ try
method(client, request)
};
}
catch(const json::error &e)
catch(const json::not_found &e)
{
throw m::error
{
http::BAD_REQUEST, "M_BAD_JSON", "Required JSON field: %s", e.what()
};
}
catch(const json::print_error &e)
{
throw m::error
{
http::INTERNAL_SERVER_ERROR, "M_NOT_JSON", "Generator Protection: %s", e.what()
};
}
catch(const json::error &e)
{
throw m::error
{
http::BAD_REQUEST, "M_NOT_JSON", "%s", e.what()
};
}
ircd::resource::method::method(struct resource &resource,
const string_view &name,
@ -258,13 +272,18 @@ ircd::resource::response::response(client &client,
const http::code &code,
const json::members &members)
{
size_t i(0);
json::iov iov;
json::iov::push nodes[members.size()];
for(const auto &member : members)
new (nodes + i++) json::iov::push(iov, member);
const auto size
{
serialized(members)
};
response(client, iov, code);
char buffer[size];
const json::object object
{
stringify(mutable_buffer{buffer, buffer + size}, members)
};
response(client, object, code);
}
ircd::resource::response::response(client &client,
@ -278,9 +297,18 @@ ircd::resource::response::response(client &client,
const http::code &code)
try
{
char buf[8192];
const auto sv(stringify(buf, members));
response(client, json::object{sv}, code);
const auto size
{
serialized(members)
};
char buffer[size];
const json::object object
{
stringify(mutable_buffer{buffer, buffer + size}, members)
};
response(client, object, code);
}
catch(const json::error &e)
{