feat: implement MoloApp plans 01-15 with integration
Full C++23 framework tree: runtime, observability, security, data, app, realtime, supervisor, clients, CLI, example plus enterprise tenancy, audit, operability, governance, supplychain. Postgres via docker compose for tests.
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
#include "clients/rest/errors.h"
|
||||
|
||||
#include "clients/headers.h"
|
||||
|
||||
namespace molo::clients::rest {
|
||||
namespace {
|
||||
|
||||
std::string str_field(const body::Json& value, const std::string& key) {
|
||||
const body::Json* found = value.find(key);
|
||||
if (found && found->is_string()) return found->as_string();
|
||||
return "";
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
ProblemDetails ProblemDetails::parse(const body::Json& value) {
|
||||
ProblemDetails problem;
|
||||
if (!value.is_object()) return problem;
|
||||
std::string type = str_field(value, "type");
|
||||
if (!type.empty()) problem.type = type;
|
||||
problem.title = str_field(value, "title");
|
||||
const body::Json* status = value.find("status");
|
||||
if (status && status->is_number())
|
||||
problem.status = (int)status->as_number();
|
||||
problem.detail = str_field(value, "detail");
|
||||
problem.instance = str_field(value, "instance");
|
||||
const body::Json* errors = value.find("errors");
|
||||
if (errors && errors->is_array()) {
|
||||
for (const auto& item : errors->as_array()) {
|
||||
if (!item.is_object()) continue;
|
||||
FieldFailure failure;
|
||||
failure.field = str_field(item, "field");
|
||||
if (failure.field.empty()) failure.field = str_field(item, "loc");
|
||||
failure.message = str_field(item, "message");
|
||||
if (failure.message.empty()) failure.message = str_field(item, "msg");
|
||||
failure.type = str_field(item, "type");
|
||||
problem.errors.push_back(failure);
|
||||
}
|
||||
}
|
||||
return problem;
|
||||
}
|
||||
|
||||
ProblemDetails ProblemDetails::from_response(const ClientResponse& response) {
|
||||
ProblemDetails problem;
|
||||
problem.status = response.status();
|
||||
problem.title = "HTTP " + std::to_string(response.status());
|
||||
if (response.content_type() == content_type() ||
|
||||
response.content_type() == "application/json") {
|
||||
try {
|
||||
body::Json parsed = response.json();
|
||||
if (parsed.is_object() &&
|
||||
(parsed.has("title") || parsed.has("detail"))) {
|
||||
problem = ProblemDetails::parse(parsed);
|
||||
if (problem.status == 0) problem.status = response.status();
|
||||
return problem;
|
||||
}
|
||||
} catch (...) {
|
||||
// Fall through to the generic shape below.
|
||||
}
|
||||
}
|
||||
if (!response.body().empty() && response.body().size() < 4096)
|
||||
problem.detail = response.body();
|
||||
return problem;
|
||||
}
|
||||
|
||||
body::Json ProblemDetails::to_json() const {
|
||||
body::Json value = body::Json::object();
|
||||
value.set("type", body::Json(type));
|
||||
value.set("title", body::Json(title));
|
||||
value.set("status", body::Json(status));
|
||||
if (!detail.empty()) value.set("detail", body::Json(detail));
|
||||
if (!instance.empty()) value.set("instance", body::Json(instance));
|
||||
if (!errors.empty()) {
|
||||
body::Json list = body::Json::array();
|
||||
for (const auto& item : errors) {
|
||||
body::Json entry = body::Json::object();
|
||||
entry.set("field", body::Json(item.field));
|
||||
entry.set("message", body::Json(item.message));
|
||||
if (!item.type.empty()) entry.set("type", body::Json(item.type));
|
||||
list.push(std::move(entry));
|
||||
}
|
||||
value.set("errors", std::move(list));
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
std::string ProblemDetails::summary() const {
|
||||
std::string out = std::to_string(status);
|
||||
if (!title.empty()) {
|
||||
out += " ";
|
||||
out += title;
|
||||
}
|
||||
if (!detail.empty()) {
|
||||
out += ": ";
|
||||
out += detail;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
RestError::RestError(ProblemDetails problem)
|
||||
: std::runtime_error("molo::clients::rest: " + problem.summary()),
|
||||
problem_(std::move(problem)) {}
|
||||
|
||||
void raise_for_status(const ClientResponse& response) {
|
||||
if (response.status() >= 400) throw RestError(ProblemDetails::from_response(response));
|
||||
}
|
||||
|
||||
ClientResponse problem_response(int status, const std::string& title,
|
||||
const std::string& detail,
|
||||
const std::string& instance) {
|
||||
ProblemDetails problem;
|
||||
problem.type = "https://molo.app/problems/" + std::to_string(status);
|
||||
problem.title = title;
|
||||
problem.status = status;
|
||||
problem.detail = detail;
|
||||
problem.instance = instance;
|
||||
HeaderMap headers;
|
||||
headers.set("Content-Type", ProblemDetails::content_type());
|
||||
std::string body = problem.to_json().dump();
|
||||
headers.set("Content-Length", std::to_string(body.size()));
|
||||
return ClientResponse(status, headers, body);
|
||||
}
|
||||
|
||||
} // namespace molo::clients::rest
|
||||
Reference in New Issue
Block a user