|
|
@@ -131,6 +131,33 @@ std::string describeDbFailure(const char* what,
|
|
|
return detail;
|
|
|
}
|
|
|
|
|
|
+// One message shape for every write that reports through FailureDetail.
|
|
|
+//
|
|
|
+// increment() and remove() each grew their own copy of this, and update(),
|
|
|
+// patch() and patchWithTtl() had none at all - they said "Patch failed:
|
|
|
+// collection/id" and dropped the reason the server gave. The reason matters
|
|
|
+// most on the two 2.13.1 refusals: "outcome_unknown" is the difference between
|
|
|
+// a write that definitely did not happen and one that may have, and
|
|
|
+// "document_too_large" is the difference between a transient failure and a
|
|
|
+// permanent one.
|
|
|
+std::string describeWriteFailure(const char* what,
|
|
|
+ const std::string& collection,
|
|
|
+ const std::string& id,
|
|
|
+ const database::Client::FailureDetail& failure) {
|
|
|
+ std::string detail = std::string(what) + " failed: " + collection + "/" + id;
|
|
|
+ if (!failure.message.empty()) detail += ": " + failure.message;
|
|
|
+ if (!failure.reason.empty()) detail += " [" + failure.reason + "]";
|
|
|
+ if (!failure.fields.empty()) {
|
|
|
+ detail += " field(s): ";
|
|
|
+ for (size_t i = 0; i < failure.fields.size(); ++i) {
|
|
|
+ if (i) detail += ", ";
|
|
|
+ detail += failure.fields[i];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!failure.relation.empty()) detail += " blocked by relation " + failure.relation;
|
|
|
+ return detail;
|
|
|
+}
|
|
|
+
|
|
|
// From client 2.12.4 a failed READ throws instead of answering "nothing there".
|
|
|
// The file calls (upload, download, getFileInfo, listFiles, setFileTtl) have
|
|
|
// thrown since well before that, which is why this is not named for reads.
|
|
|
@@ -152,7 +179,10 @@ Error dbCallError(const char* what,
|
|
|
const std::string& collection,
|
|
|
const std::string& id,
|
|
|
const database::Client::Error& e) {
|
|
|
- return Error(mapDbError(e.code()), describeDbFailure(what, collection, id, e));
|
|
|
+ // Reason first, code second: from 2.13.1 two refusals mean something the
|
|
|
+ // code alone cannot say. See mapDbError's reason overload.
|
|
|
+ return Error(mapDbError(e.code(), dbFailureReason(e)),
|
|
|
+ describeDbFailure(what, collection, id, e));
|
|
|
}
|
|
|
|
|
|
} // namespace
|
|
|
@@ -189,7 +219,8 @@ Result<std::string> StorageClient::insert(const std::string& collection,
|
|
|
}
|
|
|
return new_id;
|
|
|
} catch (const Client::Error& e) {
|
|
|
- return Error(mapDbError(e.code()), describeDbFailure("Insert", collection, "", e));
|
|
|
+ return Error(mapDbError(e.code(), dbFailureReason(e)),
|
|
|
+ describeDbFailure("Insert", collection, "", e));
|
|
|
} catch (const std::exception& e) {
|
|
|
return Error(ErrorCode::DatabaseError,
|
|
|
"Insert failed: " + collection + ": " + e.what());
|
|
|
@@ -209,7 +240,8 @@ Result<std::string> StorageClient::upsert(const std::string& collection,
|
|
|
}
|
|
|
return new_id;
|
|
|
} catch (const Client::Error& e) {
|
|
|
- return Error(mapDbError(e.code()), describeDbFailure("Upsert", collection, id, e));
|
|
|
+ return Error(mapDbError(e.code(), dbFailureReason(e)),
|
|
|
+ describeDbFailure("Upsert", collection, id, e));
|
|
|
} catch (const std::exception& e) {
|
|
|
return Error(ErrorCode::DatabaseError,
|
|
|
"Upsert failed: " + collection + ": " + e.what());
|
|
|
@@ -223,17 +255,8 @@ Result<nlohmann::json> StorageClient::increment(const std::string& collection,
|
|
|
database::Client::FailureDetail failure;
|
|
|
auto values = impl_->client_->increment(collection, id, deltas, failure, actor);
|
|
|
if (!values) {
|
|
|
- std::string detail = "Increment failed: " + collection + "/" + id;
|
|
|
- if (!failure.message.empty()) detail += ": " + failure.message;
|
|
|
- if (!failure.reason.empty()) detail += " [" + failure.reason + "]";
|
|
|
- if (!failure.fields.empty()) {
|
|
|
- detail += " field(s): ";
|
|
|
- for (size_t i = 0; i < failure.fields.size(); ++i) {
|
|
|
- if (i) detail += ", ";
|
|
|
- detail += failure.fields[i];
|
|
|
- }
|
|
|
- }
|
|
|
- return Error(mapDbError(failure.code), detail);
|
|
|
+ return Error(mapDbError(failure.code, failure.reason),
|
|
|
+ describeWriteFailure("Increment", collection, id, failure));
|
|
|
}
|
|
|
return *values;
|
|
|
}
|
|
|
@@ -244,19 +267,42 @@ Result<int64_t> StorageClient::update(const std::string& collection,
|
|
|
int64_t expected_version,
|
|
|
bool partial,
|
|
|
const std::string& actor) {
|
|
|
+ // All three branches take the FailureDetail overload (2.11.4/2.12.0). They
|
|
|
+ // used to take the bare forms and flatten every failure to DatabaseError -
|
|
|
+ // or, worse, to a specific code that was often wrong: a failed
|
|
|
+ // updateIfVersion was reported as a version conflict even when the
|
|
|
+ // document was simply missing or the database was unreachable.
|
|
|
+ //
|
|
|
+ // That also kept reason strings out of reach, and from 2.13.1 the reason is
|
|
|
+ // the only thing that distinguishes a write which may have been applied
|
|
|
+ // (outcome_unknown) from one that definitely was not. patch() is the write
|
|
|
+ // path most callers here use, so without this the new classification could
|
|
|
+ // never surface.
|
|
|
if (partial) {
|
|
|
- uint64_t v = impl_->client_->patch(collection, id, data, actor);
|
|
|
- if (v == 0) return Error(ErrorCode::DatabaseError, "Patch failed: " + collection + "/" + id);
|
|
|
+ database::Client::FailureDetail failure;
|
|
|
+ const uint64_t v = impl_->client_->patch(collection, id, data, failure, actor);
|
|
|
+ if (v == 0) {
|
|
|
+ return Error(mapDbError(failure.code, failure.reason),
|
|
|
+ describeWriteFailure("Patch", collection, id, failure));
|
|
|
+ }
|
|
|
return static_cast<int64_t>(v);
|
|
|
}
|
|
|
if (expected_version != 0) {
|
|
|
- bool ok = impl_->client_->updateIfVersion(
|
|
|
- collection, id, data, static_cast<uint64_t>(expected_version), actor);
|
|
|
- if (!ok) return Error(ErrorCode::VersionConflict, "Version conflict: " + collection + "/" + id);
|
|
|
+ database::Client::FailureDetail failure;
|
|
|
+ const bool ok = impl_->client_->updateIfVersion(
|
|
|
+ collection, id, data, static_cast<uint64_t>(expected_version), failure, actor);
|
|
|
+ if (!ok) {
|
|
|
+ return Error(mapDbError(failure.code, failure.reason),
|
|
|
+ describeWriteFailure("Conditional update", collection, id, failure));
|
|
|
+ }
|
|
|
return expected_version + 1;
|
|
|
}
|
|
|
- bool ok = impl_->client_->update(collection, id, data, actor);
|
|
|
- if (!ok) return Error(ErrorCode::DatabaseError, "Update failed: " + collection + "/" + id);
|
|
|
+ database::Client::FailureDetail failure;
|
|
|
+ const bool ok = impl_->client_->update(collection, id, data, failure, actor);
|
|
|
+ if (!ok) {
|
|
|
+ return Error(mapDbError(failure.code, failure.reason),
|
|
|
+ describeWriteFailure("Update", collection, id, failure));
|
|
|
+ }
|
|
|
// Upstream plain update() returns bool only; the new version is not exposed.
|
|
|
// Use -1 to signal "version unknown" so callers cannot mistake it for version 0.
|
|
|
return -1;
|
|
|
@@ -266,9 +312,12 @@ Result<int64_t> StorageClient::patchWithTtl(const std::string& collection,
|
|
|
const std::string& id,
|
|
|
const nlohmann::json& fields,
|
|
|
int64_t ttl_ms) {
|
|
|
- uint64_t version = impl_->client_->patchWithTtl(collection, id, fields, msToSec(ttl_ms));
|
|
|
+ database::Client::FailureDetail failure;
|
|
|
+ const uint64_t version =
|
|
|
+ impl_->client_->patchWithTtl(collection, id, fields, msToSec(ttl_ms), failure);
|
|
|
if (version == 0) {
|
|
|
- return Error(ErrorCode::DatabaseError, "Patch with TTL failed: " + collection + "/" + id);
|
|
|
+ return Error(mapDbError(failure.code, failure.reason),
|
|
|
+ describeWriteFailure("Patch with TTL", collection, id, failure));
|
|
|
}
|
|
|
return static_cast<int64_t>(version);
|
|
|
}
|
|
|
@@ -290,11 +339,8 @@ Result<void> StorageClient::remove(const std::string& collection,
|
|
|
database::Client::FailureDetail failure;
|
|
|
const bool ok = impl_->client_->removeAs(collection, id, actor, failure);
|
|
|
if (!ok) {
|
|
|
- std::string detail = "Remove failed: " + collection + "/" + id;
|
|
|
- if (!failure.message.empty()) detail += ": " + failure.message;
|
|
|
- if (!failure.reason.empty()) detail += " [" + failure.reason + "]";
|
|
|
- if (!failure.relation.empty()) detail += " blocked by relation " + failure.relation;
|
|
|
- return Error(mapDbError(failure.code), detail);
|
|
|
+ return Error(mapDbError(failure.code, failure.reason),
|
|
|
+ describeWriteFailure("Remove", collection, id, failure));
|
|
|
}
|
|
|
return {};
|
|
|
}
|
|
|
@@ -846,35 +892,61 @@ Result<std::vector<FileInfo>> StorageClient::listFilesForWorkflow(const std::str
|
|
|
}
|
|
|
|
|
|
Result<void> StorageClient::setFileTtl(const std::string& id, int64_t ttl_ms) {
|
|
|
- // The one call in the file API that still cannot tell an outage from an
|
|
|
- // absence. Upstream's setFileTtl returns plain false on ANY failed status
|
|
|
- // and never throws, and unlike the other writes it has no Failure&
|
|
|
- // overload, so there is no channel to report a reason through - the 2.12.4
|
|
|
- // sweep could not reach it. The catch below is therefore belt-and-braces
|
|
|
- // rather than live today.
|
|
|
+ // From client 2.13.1 this call can finally say WHY it failed.
|
|
|
//
|
|
|
- // So this does not claim the file is gone. It used to answer
|
|
|
- // DocumentNotFound, which during a blip asserted something it had no way
|
|
|
- // of knowing; the retention worker only counts successes, and a file that
|
|
|
- // keeps its existing expiry is the harmless direction either way.
|
|
|
+ // It used to return a bare false for every failed status with no channel to
|
|
|
+ // report a reason through, so this returned a message naming all three
|
|
|
+ // possibilities at once - "no such file, already expired, or the database
|
|
|
+ // could not be reached; upstream does not distinguish these". That hedge was
|
|
|
+ // honest but useless: the retention worker could not tell a file it should
|
|
|
+ // stop restamping from a database it should wait for. The FailureDetail
|
|
|
+ // overload separates them, so the hedge is gone.
|
|
|
+ //
|
|
|
+ // NotFound still covers two cases upstream treats as one - the file is not
|
|
|
+ // in this project, or its TTL already elapsed - but both mean the same
|
|
|
+ // thing to a caller: there is nothing left to restamp.
|
|
|
+ database::Client::FailureDetail failure;
|
|
|
bool ok = false;
|
|
|
try {
|
|
|
- ok = impl_->client_->setFileTtl(id, msToSec(ttl_ms));
|
|
|
+ ok = impl_->client_->setFileTtl(id, msToSec(ttl_ms), failure);
|
|
|
} catch (const Client::Error& e) {
|
|
|
return dbCallError("setFileTtl", "files", id, e);
|
|
|
+ } catch (const std::exception& e) {
|
|
|
+ return Error(ErrorCode::DatabaseError,
|
|
|
+ "setFileTtl failed: files/" + id + ": " + e.what());
|
|
|
}
|
|
|
if (!ok) {
|
|
|
- return Error(ErrorCode::DatabaseError,
|
|
|
- "Could not set the expiry of " + id +
|
|
|
- " - no such file in this project, it has already expired, or the "
|
|
|
- "database could not be reached; upstream does not distinguish these");
|
|
|
+ if (failure.code == Client::ErrorCode::NotFound) {
|
|
|
+ return Error(ErrorCode::DocumentNotFound,
|
|
|
+ "No file " + id + " to set an expiry on - it is not in this project, "
|
|
|
+ "or it has already expired");
|
|
|
+ }
|
|
|
+ return Error(mapDbError(failure.code, failure.reason),
|
|
|
+ describeWriteFailure("setFileTtl", "files", id, failure));
|
|
|
}
|
|
|
return {};
|
|
|
}
|
|
|
|
|
|
Result<void> StorageClient::deleteFile(const std::string& id) {
|
|
|
- if (!impl_->client_->deleteFile(id)) {
|
|
|
- return Error(ErrorCode::DatabaseError, "Failed to delete file: " + id);
|
|
|
+ // Had no error channel at all before 2.13.1: a bare false became
|
|
|
+ // DatabaseError "Failed to delete file", so an unreachable database and a
|
|
|
+ // file that was never there read identically.
|
|
|
+ database::Client::FailureDetail failure;
|
|
|
+ bool ok = false;
|
|
|
+ try {
|
|
|
+ ok = impl_->client_->deleteFile(id, failure);
|
|
|
+ } catch (const Client::Error& e) {
|
|
|
+ return dbCallError("deleteFile", "files", id, e);
|
|
|
+ } catch (const std::exception& e) {
|
|
|
+ return Error(ErrorCode::DatabaseError,
|
|
|
+ "deleteFile failed: files/" + id + ": " + e.what());
|
|
|
+ }
|
|
|
+ if (!ok) {
|
|
|
+ if (failure.code == Client::ErrorCode::NotFound) {
|
|
|
+ return Error(ErrorCode::DocumentNotFound, "No file " + id + " to delete");
|
|
|
+ }
|
|
|
+ return Error(mapDbError(failure.code, failure.reason),
|
|
|
+ describeWriteFailure("deleteFile", "files", id, failure));
|
|
|
}
|
|
|
return {};
|
|
|
}
|