|
@@ -1,4 +1,5 @@
|
|
|
#include "collection_registry.hpp"
|
|
#include "collection_registry.hpp"
|
|
|
|
|
+#include "db_errors.hpp"
|
|
|
#include "errors.hpp"
|
|
#include "errors.hpp"
|
|
|
#include "filters.hpp"
|
|
#include "filters.hpp"
|
|
|
#include "json_http.hpp"
|
|
#include "json_http.hpp"
|
|
@@ -81,7 +82,12 @@ void registerDocumentRoutes(ApiServer& s) {
|
|
|
std::string id = body.value("id", "");
|
|
std::string id = body.value("id", "");
|
|
|
nlohmann::json data = documentBody(body);
|
|
nlohmann::json data = documentBody(body);
|
|
|
const uint32_t ttl = ttlParam(req).value_or(0);
|
|
const uint32_t ttl = ttlParam(req).value_or(0);
|
|
|
- std::string newId = d->db.client().insert(c, data, id, ttl);
|
|
|
|
|
|
|
+ std::string newId;
|
|
|
|
|
+ try {
|
|
|
|
|
+ newId = d->db.client().insert(c, data, id, ttl);
|
|
|
|
|
+ } catch (const smartbotic::database::Client::Error& e) {
|
|
|
|
|
+ throw apiErrorFor(e);
|
|
|
|
|
+ }
|
|
|
if (newId.empty()) throw ApiError(ErrCode::Unavailable, "db_error", "insert failed");
|
|
if (newId.empty()) throw ApiError(ErrCode::Unavailable, "db_error", "insert failed");
|
|
|
sendJson(res, 201, {{"id", newId}});
|
|
sendJson(res, 201, {{"id", newId}});
|
|
|
});
|
|
});
|
|
@@ -115,33 +121,40 @@ void registerDocumentRoutes(ApiServer& s) {
|
|
|
if (!d->db.client().exists(c, id)) throw ApiError(ErrCode::NotFound, "not_found", "no such document");
|
|
if (!d->db.client().exists(c, id)) throw ApiError(ErrCode::NotFound, "not_found", "no such document");
|
|
|
nlohmann::json patchBody = documentBody(bodyJson(req));
|
|
nlohmann::json patchBody = documentBody(bodyJson(req));
|
|
|
const auto ttl = ttlParam(req);
|
|
const auto ttl = ttlParam(req);
|
|
|
- uint64_t v = ttl ? d->db.client().patchWithTtl(c, id, patchBody, *ttl)
|
|
|
|
|
- : d->db.client().patch(c, id, patchBody);
|
|
|
|
|
- if (v == 0) throw ApiError(ErrCode::Unavailable, "db_error", "patch failed");
|
|
|
|
|
|
|
+ smartbotic::database::Client::Failure failure;
|
|
|
|
|
+ uint64_t v = ttl ? d->db.client().patchWithTtl(c, id, patchBody, *ttl, failure)
|
|
|
|
|
+ : d->db.client().patch(c, id, patchBody, failure);
|
|
|
|
|
+ if (v == 0) throw apiErrorFor(failure, "no such document");
|
|
|
sendJson(res, 200, {{"id", id}, {"version", v}});
|
|
sendJson(res, 200, {{"id", id}, {"version", v}});
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
svr.Put(R"(/api/v1/projects/([^/]+)/collections/([^/]+)/documents/([^/]+))", [d](const httplib::Request& req, httplib::Response& res) {
|
|
svr.Put(R"(/api/v1/projects/([^/]+)/collections/([^/]+)/documents/([^/]+))", [d](const httplib::Request& req, httplib::Response& res) {
|
|
|
std::string c = scoped(d, req, 1, 2, KeyOp::Update); std::string id = req.matches[3];
|
|
std::string c = scoped(d, req, 1, 2, KeyOp::Update); std::string id = req.matches[3];
|
|
|
if (!d->db.client().exists(c, id)) throw ApiError(ErrCode::NotFound, "not_found", "no such document");
|
|
if (!d->db.client().exists(c, id)) throw ApiError(ErrCode::NotFound, "not_found", "no such document");
|
|
|
- if (!d->db.client().update(c, id, documentBody(bodyJson(req))))
|
|
|
|
|
- throw ApiError(ErrCode::Unavailable, "db_error", "update failed");
|
|
|
|
|
|
|
+ smartbotic::database::Client::Failure failure;
|
|
|
|
|
+ if (!d->db.client().update(c, id, documentBody(bodyJson(req)), failure))
|
|
|
|
|
+ throw apiErrorFor(failure, "no such document");
|
|
|
sendJson(res, 200, {{"id", id}});
|
|
sendJson(res, 200, {{"id", id}});
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
svr.Delete(R"(/api/v1/projects/([^/]+)/collections/([^/]+)/documents/([^/]+))", [d](const httplib::Request& req, httplib::Response& res) {
|
|
svr.Delete(R"(/api/v1/projects/([^/]+)/collections/([^/]+)/documents/([^/]+))", [d](const httplib::Request& req, httplib::Response& res) {
|
|
|
std::string c = scoped(d, req, 1, 2, KeyOp::Delete); std::string id = req.matches[3];
|
|
std::string c = scoped(d, req, 1, 2, KeyOp::Delete); std::string id = req.matches[3];
|
|
|
std::string project = req.matches[1];
|
|
std::string project = req.matches[1];
|
|
|
- std::string err;
|
|
|
|
|
- if (!d->db.client().remove(c, id, err)) {
|
|
|
|
|
|
|
+ smartbotic::database::Client::Failure failure;
|
|
|
|
|
+ if (!d->db.client().remove(c, id, failure)) {
|
|
|
// A restrict relation is the one refusal a caller can act on, so it
|
|
// A restrict relation is the one refusal a caller can act on, so it
|
|
|
- // gets 409 plus the impact list rather than a generic failure.
|
|
|
|
|
- auto why = d->db.client().describeDelete(c, id);
|
|
|
|
|
- if (why.success && why.wouldBeBlocked)
|
|
|
|
|
- throw ApiError(ErrCode::Conflict, "relation_restricted",
|
|
|
|
|
- "delete blocked by a relation",
|
|
|
|
|
- {{"impacts", impactsJson(project, why)}});
|
|
|
|
|
- throw ApiError(ErrCode::NotFound, "not_found", "no such document");
|
|
|
|
|
|
|
+ // gets 409 plus the impact list rather than a generic failure. Only
|
|
|
|
|
+ // ask for the impacts when the database says a precondition failed;
|
|
|
|
|
+ // previously every failure took this path and then answered 404,
|
|
|
|
|
+ // which was wrong for a transport error.
|
|
|
|
|
+ if (failure.code == smartbotic::database::Client::ErrorCode::FailedPrecondition) {
|
|
|
|
|
+ auto why = d->db.client().describeDelete(c, id);
|
|
|
|
|
+ if (why.success && why.wouldBeBlocked)
|
|
|
|
|
+ throw ApiError(ErrCode::Conflict, "relation_restricted",
|
|
|
|
|
+ "delete blocked by a relation",
|
|
|
|
|
+ {{"impacts", impactsJson(project, why)}});
|
|
|
|
|
+ }
|
|
|
|
|
+ throw apiErrorFor(failure, "no such document");
|
|
|
}
|
|
}
|
|
|
sendJson(res, 200, {{"deleted", id}});
|
|
sendJson(res, 200, {{"deleted", id}});
|
|
|
});
|
|
});
|