diff options
author | Moustafa Maher <m.maher@10gen.com> | 2021-03-25 19:22:59 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-04-01 23:30:27 +0000 |
commit | d4840264c7df06f969267806fdf2623d1bb6f6d6 (patch) | |
tree | d1d9dfe838ba61d3c65d3e61458bb22504994c39 /src/mongo/s | |
parent | a1cb32b607ffff4014a203419cc60598f206b7e0 (diff) | |
download | mongo-d4840264c7df06f969267806fdf2623d1bb6f6d6.tar.gz |
SERVER-54436 Refactor validateResult method from cluster_create_indexes to use checkIsErrorStatus
Diffstat (limited to 'src/mongo/s')
-rw-r--r-- | src/mongo/s/commands/cluster_collection_mod_cmd.cpp | 42 | ||||
-rw-r--r-- | src/mongo/s/commands/cluster_create_indexes_cmd.cpp | 80 |
2 files changed, 53 insertions, 69 deletions
diff --git a/src/mongo/s/commands/cluster_collection_mod_cmd.cpp b/src/mongo/s/commands/cluster_collection_mod_cmd.cpp index c8ee99af54d..0d895756b91 100644 --- a/src/mongo/s/commands/cluster_collection_mod_cmd.cpp +++ b/src/mongo/s/commands/cluster_collection_mod_cmd.cpp @@ -117,26 +117,36 @@ public: void validateResult(const BSONObj& resultObj) final { auto ctx = IDLParserErrorContext("CollModReply"); + if (checkIsErrorStatus(resultObj, ctx)) { + return; + } + StringDataSet ignorableFields({kWriteConcernErrorFieldName, ErrorReply::kOkFieldName, kTopologyVersionFieldName, kRawFieldName}); - if (!checkIsErrorStatus(resultObj, ctx)) { - auto reply = Reply::parse(ctx, resultObj.removeFields(ignorableFields)); - coll_mod_reply_validation::validateReply(reply); - - if (resultObj.hasField(kRawFieldName)) { - const auto& rawData = resultObj[kRawFieldName]; - if (ctx.checkAndAssertType(rawData, Object)) { - for (const auto& element : rawData.Obj()) { - const auto& shardReply = element.Obj(); - if (!checkIsErrorStatus(shardReply, ctx)) { - auto reply = - Reply::parse(ctx, shardReply.removeFields(ignorableFields)); - coll_mod_reply_validation::validateReply(reply); - } - } - } + auto reply = Reply::parse(ctx, resultObj.removeFields(ignorableFields)); + coll_mod_reply_validation::validateReply(reply); + + if (!resultObj.hasField(kRawFieldName)) { + return; + } + + const auto& rawData = resultObj[kRawFieldName]; + if (!ctx.checkAndAssertType(rawData, Object)) { + return; + } + + auto rawCtx = IDLParserErrorContext(kRawFieldName, &ctx); + for (const auto& element : rawData.Obj()) { + if (!rawCtx.checkAndAssertType(element, Object)) { + return; + } + + const auto& shardReply = element.Obj(); + if (!checkIsErrorStatus(shardReply, ctx)) { + auto reply = Reply::parse(ctx, shardReply.removeFields(ignorableFields)); + coll_mod_reply_validation::validateReply(reply); } } } diff --git a/src/mongo/s/commands/cluster_create_indexes_cmd.cpp b/src/mongo/s/commands/cluster_create_indexes_cmd.cpp index 1c6f9e68f5d..0f656dc3291 100644 --- a/src/mongo/s/commands/cluster_create_indexes_cmd.cpp +++ b/src/mongo/s/commands/cluster_create_indexes_cmd.cpp @@ -50,6 +50,7 @@ constexpr auto kTopologyVersionFieldName = "topologyVersion"_sd; class CreateIndexesCmd : public BasicCommandWithRequestParser<CreateIndexesCmd> { public: using Request = CreateIndexesCommand; + using Reply = CreateIndexesReply; const std::set<std::string>& apiVersions() const final { return kApiVersions1; @@ -118,62 +119,35 @@ public: * 'code' & 'codeName' are permitted in either scenario, but non-zero 'code' indicates "not ok". */ void validateResult(const BSONObj& result) final { - BSONElement rawElem; - bool ok = true, hasErrMsg = false; - - for (auto elem : result) { - const auto fieldName = elem.fieldNameStringData(); - if (fieldName == kRawFieldName) { - rawElem = elem; - uassert(ErrorCodes::BadValue, - str::stream() - << "'raw' field must be an object, got: " << typeName(elem.type()), - elem.type() == Object); - } else if (fieldName == ErrorReply::kCodeFieldName) { - uassert(ErrorCodes::BadValue, - str::stream() << "Reply contained non-numeric status code: " << elem, - elem.isNumber()); - ok = ok & (elem.numberInt() != 0); - } else if (fieldName == ErrorReply::kOkFieldName) { - ok = ok & elem.trueValue(); - } else if (fieldName == ErrorReply::kErrmsgFieldName) { - hasErrMsg = true; - } else if ((fieldName == ErrorReply::kCodeNameFieldName) || - (fieldName == kWriteConcernErrorFieldName)) { - // Ignorable field. - } else { - uasserted(ErrorCodes::BadValue, - str::stream() << "Invalid field in reply: " << fieldName); - } + auto ctx = IDLParserErrorContext("createIndexesReply"); + if (checkIsErrorStatus(result, ctx)) { + return; + } + + StringDataSet ignorableFields({kWriteConcernErrorFieldName, + ErrorReply::kOkFieldName, + kTopologyVersionFieldName, + kRawFieldName}); + Reply::parse(ctx, result.removeFields(ignorableFields)); + if (!result.hasField(kRawFieldName)) { + return; } - if (ok) { - uassert( - ErrorCodes::BadValue, "Error message field present for 'ok' result", !hasErrMsg); - uassert(ErrorCodes::BadValue, "Missing field in reply: raw", !rawElem.eoo()); - - invariant(rawElem.type() == Object); // Validated in field loop above. - IDLParserErrorContext ctx("createIndexesReply"); - StringDataSet ignorableFields( - {kWriteConcernErrorFieldName, ErrorReply::kOkFieldName, kTopologyVersionFieldName}); - for (auto elem : rawElem.Obj()) { - uassert(ErrorCodes::FailedToParse, - str::stream() << "Response from shard must be an object, found: " - << typeName(elem.type()), - elem.type() == Object); - try { - // 'ok' is a permissable part of an reply even though it's not - // a formal part of the command reply. - CreateIndexesReply::parse(ctx, elem.Obj().removeFields(ignorableFields)); - } catch (const DBException& ex) { - uasserted(ex.code(), - str::stream() - << "Failed parsing response from shard: " << ex.reason()); - } + const auto& rawData = result[kRawFieldName]; + if (!ctx.checkAndAssertType(rawData, Object)) { + return; + } + + auto rawCtx = IDLParserErrorContext(kRawFieldName, &ctx); + for (const auto& element : rawData.Obj()) { + if (!rawCtx.checkAndAssertType(element, Object)) { + return; + } + + const auto& shardReply = element.Obj(); + if (!checkIsErrorStatus(shardReply, ctx)) { + Reply::parse(ctx, shardReply.removeFields(ignorableFields)); } - } else { - uassert( - ErrorCodes::BadValue, "Error message field missing for 'not ok' result", hasErrMsg); } } |