diff options
Diffstat (limited to 'src/mongo/db/commands.cpp')
-rw-r--r-- | src/mongo/db/commands.cpp | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/mongo/db/commands.cpp b/src/mongo/db/commands.cpp index b431751dc9f..2e4b0116afd 100644 --- a/src/mongo/db/commands.cpp +++ b/src/mongo/db/commands.cpp @@ -50,6 +50,7 @@ #include "mongo/db/namespace_string.h" #include "mongo/db/server_parameters.h" #include "mongo/rpc/write_concern_error_detail.h" +#include "mongo/s/stale_exception.h" #include "mongo/util/log.h" namespace mongo { @@ -182,6 +183,26 @@ Status Command::explain(OperationContext* opCtx, return {ErrorCodes::IllegalOperation, str::stream() << "Cannot explain cmd: " << getName()}; } +BSONObj Command::runCommandDirectly(OperationContext* opCtx, const OpMsgRequest& request) { + auto command = Command::findCommand(request.getCommandName()); + invariant(command); + + BSONObjBuilder out; + try { + std::string errmsg; + bool ok = command->enhancedRun(opCtx, request, errmsg, out); + appendCommandStatus(out, ok, errmsg); + } catch (const StaleConfigException& ex) { + // These exceptions are intended to be handled at a higher level and cannot losslessly + // round-trip through Status. + throw; + } catch (const DBException& ex) { + out.resetToEmpty(); + appendCommandStatus(out, ex.toStatus()); + } + return out.obj(); +} + Command* Command::findCommand(StringData name) { CommandMap::const_iterator i = _commands->find(name); if (i == _commands->end()) |