Fix comment, add a new function for reading special keys' error message

This commit is contained in:
Chaoguang Lin 2021-08-24 14:18:43 -07:00
parent 5f924c8952
commit 8de3417f24
4 changed files with 26 additions and 10 deletions

View File

@ -10,7 +10,7 @@ set(FDBCLI_SRCS
SetClassCommand.actor.cpp
SnapshotCommand.actor.cpp
ThrottleCommand.actor.cpp
Util.cpp
Util.actor.cpp
linenoise/linenoise.h)
if(NOT WIN32)

View File

@ -23,8 +23,6 @@
#include "fdbclient/FDBOptions.g.h"
#include "fdbclient/IClientApi.h"
#include "fdbclient/Knobs.h"
#include "fdbclient/Schemas.h"
#include "fdbclient/Status.h"
#include "flow/Arena.h"
#include "flow/FastRef.h"
@ -83,13 +81,9 @@ ACTOR Future<bool> setProcessClass(Reference<IDatabase> db, KeyRef network_addre
} catch (Error& e) {
state Error err(e);
if (e.code() == error_code_special_keys_api_failure) {
Optional<Value> errorMsg = wait(safeThreadFutureToFuture(tr->get(fdb_cli::errorMsgSpecialKey)));
ASSERT(errorMsg.present());
std::string errorMsgStr;
auto valueObj = readJSONStrictly(errorMsg.get().toString()).get_obj();
auto schema = readJSONStrictly(JSONSchemas::managementApiErrorSchema.toString()).get_obj();
std::string errorMsgStr = wait(fdb_cli::getSpecialKeysFailureErrorMessage(tr));
// error message already has \n at the end
fprintf(stderr, "%s", valueObj["message"].get_str().c_str());
fprintf(stderr, "%s", errorMsgStr.c_str());
return false;
}
wait(safeThreadFutureToFuture(tr->onError(err)));

View File

@ -1,5 +1,5 @@
/*
* Util.cpp
* Util.actor.cpp
*
* This source file is part of the FoundationDB open source project
*
@ -19,9 +19,14 @@
*/
#include "fdbcli/fdbcli.actor.h"
#include "fdbclient/ManagementAPI.actor.h"
#include "fdbclient/Schemas.h"
#include "fdbclient/Status.h"
#include "flow/Arena.h"
#include "flow/actorcompiler.h" // This must be the last #include.
namespace fdb_cli {
bool tokencmp(StringRef token, const char* command) {
@ -40,4 +45,18 @@ void printUsage(StringRef command) {
fprintf(stderr, "ERROR: Unknown command `%s'\n", command.toString().c_str());
}
ACTOR Future<std::string> getSpecialKeysFailureErrorMessage(Reference<ITransaction> tr) {
Optional<Value> errorMsg = wait(safeThreadFutureToFuture(tr->get(fdb_cli::errorMsgSpecialKey)));
// Error message should be present
ASSERT(errorMsg.present());
// Read the json string
auto valueObj = readJSONStrictly(errorMsg.get().toString()).get_obj();
// verify schema
auto schema = readJSONStrictly(JSONSchemas::managementApiErrorSchema.toString()).get_obj();
std::string errorStr;
ASSERT(schemaMatch(schema, valueObj, errorStr, SevError, true));
// return the error message
return valueObj["message"].get_str();
}
} // namespace fdb_cli

View File

@ -75,6 +75,9 @@ inline const KeyRef errorMsgSpecialKey = LiteralStringRef("\xff\xff/error_messag
bool tokencmp(StringRef token, const char* command);
// print the usage of the specified command
void printUsage(StringRef command);
// Pre: tr failed with special_keys_api_failure error
// Read the error message special key and return the message
ACTOR Future<std::string> getSpecialKeysFailureErrorMessage(Reference<ITransaction> tr);
// All fdbcli commands (alphabetically)
// advanceversion command