|
@@ -128,4 +128,68 @@ TEST(DbErrors, TheDatabaseMessageIsCarriedThroughWhereItIsTheUsefulOne) {
|
|
|
EXPECT_EQ(denied.what(), std::string("collection is read-protected"));
|
|
EXPECT_EQ(denied.what(), std::string("collection is read-protected"));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// --- the THROWN path, which the global exception handler now maps -----------
|
|
|
|
|
+//
|
|
|
|
|
+// Everything above drives the FailureDetail overload, which is plain data. But
|
|
|
|
|
+// the arm added to the global exception handler catches a thrown Client::Error,
|
|
|
|
|
+// and a caught-by-reference exception may or may not carry a FailureDetail
|
|
|
|
|
+// alongside it. These assert the exception entry point directly, because that
|
|
|
|
|
+// is the one an uncaught database failure actually travels through.
|
|
|
|
|
+
|
|
|
|
|
+TEST(DbErrors, AThrownTransportFailureIs503NotA500) {
|
|
|
|
|
+ // The reason this arm exists. Client 2.12.4 makes about twenty reads THROW
|
|
|
|
|
+ // on a failed gRPC status instead of answering empty, so during an outage
|
|
|
|
|
+ // these reach the global handler. With no Client::Error arm they would land
|
|
|
|
|
+ // on the std::exception arm as 500 "internal" plus a gRPC sentence - which
|
|
|
|
|
+ // tells a caller we are broken and it should not retry, when the truth is
|
|
|
|
|
+ // the opposite.
|
|
|
|
|
+ for (const Code c : {Code::Unavailable, Code::DeadlineExceeded}) {
|
|
|
|
|
+ const smartbotic::database::Client::Error e(c, "failed to connect to all addresses");
|
|
|
|
|
+ const svapi::ApiError a = svapi::apiErrorFor(e);
|
|
|
|
|
+ EXPECT_EQ(svapi::httpStatus(a.code), 503) << static_cast<int>(c);
|
|
|
|
|
+ EXPECT_EQ(a.slug, "db_unavailable") << static_cast<int>(c);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(DbErrors, AThrownErrorWithNoDetailStillUsesItsCode) {
|
|
|
|
|
+ // errorDetail() returns null when the exception was sliced (caught by
|
|
|
|
|
+ // value) or raised somewhere that attached none. The mapping must fall back
|
|
|
|
|
+ // to code() rather than to the catch-all.
|
|
|
|
|
+ const smartbotic::database::Client::Error denied(Code::PermissionDenied,
|
|
|
|
|
+ "collection is read-protected");
|
|
|
|
|
+ const svapi::ApiError a = svapi::apiErrorFor(denied);
|
|
|
|
|
+ EXPECT_EQ(svapi::httpStatus(a.code), 403);
|
|
|
|
|
+ EXPECT_EQ(a.what(), std::string("collection is read-protected"));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(DbErrors, AThrownErrorNeverLandsOnTheUnclassified500ByAccident) {
|
|
|
|
|
+ // A guard on the whole table: every code the database can raise must map to
|
|
|
|
|
+ // something deliberate. Internal and DataLoss ARE 500 - that is correct,
|
|
|
|
|
+ // they are genuine faults - but nothing should reach 500 merely because the
|
|
|
|
|
+ // switch forgot it.
|
|
|
|
|
+ const struct { Code c; int status; } expected[] = {
|
|
|
|
|
+ {Code::Ok, 404}, {Code::NotFound, 404},
|
|
|
|
|
+ {Code::AlreadyExists, 409}, {Code::Aborted, 409},
|
|
|
|
|
+ {Code::PermissionDenied, 403}, {Code::InvalidArgument, 422},
|
|
|
|
|
+ {Code::OutOfRange, 422}, {Code::ResourceExhausted, 429},
|
|
|
|
|
+ {Code::Unavailable, 503}, {Code::DeadlineExceeded, 503},
|
|
|
|
|
+ {Code::Unknown, 503},
|
|
|
|
|
+ };
|
|
|
|
|
+ for (const auto& e : expected) {
|
|
|
|
|
+ const smartbotic::database::Client::Error err(e.c, "boom");
|
|
|
|
|
+ EXPECT_EQ(svapi::httpStatus(svapi::apiErrorFor(err).code), e.status)
|
|
|
|
|
+ << "code " << static_cast<int>(e.c);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(DbErrors, ARateLimitFromTheDatabaseIsA429SoItCanCarryRetryAfter) {
|
|
|
|
|
+ // The global handler sets Retry-After for TooManyRequests, and it now does
|
|
|
|
|
+ // so for a database-raised one as well as for our own. That only works if
|
|
|
|
|
+ // ResourceExhausted really maps to the same ErrCode.
|
|
|
|
|
+ const smartbotic::database::Client::Error e(Code::ResourceExhausted, "too many requests");
|
|
|
|
|
+ const svapi::ApiError a = svapi::apiErrorFor(e);
|
|
|
|
|
+ EXPECT_EQ(a.code, svapi::ErrCode::TooManyRequests);
|
|
|
|
|
+ EXPECT_EQ(svapi::httpStatus(a.code), 429);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
} // namespace
|
|
} // namespace
|