Skip to content

Commit

Permalink
Update error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
uweseimet committed Oct 30, 2023
1 parent e03bb50 commit d58c881
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
13 changes: 11 additions & 2 deletions cpp/scsictl/scsictl_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,17 @@ int ScsiCtl::run(const vector<char *>& args) const

case 's':
command.set_operation(SERVER_INFO);
if (optarg) {
SetCommandParams(command, optarg);
if (optarg) {
const string params = optarg;
if (params.find(KEY_VALUE_SEPARATOR) != string::npos) {
if (const string error = SetFromGenericParams(command, params); !error.empty()) {
cerr << "Error: " << error << endl;
exit(EXIT_FAILURE);
}
}
else {
SetCommandParams(command, params);
}
}
break;

Expand Down
11 changes: 4 additions & 7 deletions cpp/shared/protobuf_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ void protobuf_util::ParseParameters(PbDeviceDefinition& device, const string& pa

void protobuf_util::SetCommandParams(PbCommand& command, const string& params)
{
if (params.find(KEY_VALUE_SEPARATOR) != string::npos) {
SetFromGenericParams(command, params);
return;
}

string folder_pattern;
string file_pattern;
string operations;
Expand Down Expand Up @@ -76,17 +71,19 @@ void protobuf_util::SetCommandParams(PbCommand& command, const string& params)
SetParam(command, "operations", operations);
}

void protobuf_util::SetFromGenericParams(PbCommand& command, const string& params)
string protobuf_util::SetFromGenericParams(PbCommand& command, const string& params)
{
for (const string& key_value : Split(params, COMPONENT_SEPARATOR)) {
const auto& param = Split(key_value, KEY_VALUE_SEPARATOR, 2);
if (param.size() > 1 && !param[0].empty()) {
SetParam(command, param[0], param[1]);
}
else {
throw parser_exception("Parameter '" + key_value + "' has to be a key/value pair");
return "Parameter '" + key_value + "' has to be a key/value pair";
}
}

return "";
}

void protobuf_util::SetProductData(PbDeviceDefinition& device, const string& data)
Expand Down
2 changes: 1 addition & 1 deletion cpp/shared/protobuf_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace protobuf_util

void ParseParameters(PbDeviceDefinition&, const string&);
void SetCommandParams(PbCommand&, const string&);
void SetFromGenericParams(PbCommand&, const string&);
string SetFromGenericParams(PbCommand&, const string&);
void SetProductData(PbDeviceDefinition&, const string&);
string SetIdAndLun(PbDeviceDefinition&, const string&);
string ListDevices(const vector<PbDevice>&);
Expand Down
11 changes: 3 additions & 8 deletions cpp/test/protobuf_util_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,25 +85,20 @@ TEST(ProtobufUtil, SetCommandParams)
EXPECT_EQ("folder", GetParam(command6, "folder_pattern"));
EXPECT_EQ("file", GetParam(command6, "file_pattern"));
EXPECT_EQ("operations", GetParam(command6, "operations"));

PbCommand command7;
SetCommandParams(command7, "operations=mapping_info:folder_pattern=pattern");
EXPECT_EQ("mapping_info", GetParam(command7, "operations"));
EXPECT_EQ("pattern", GetParam(command7, "folder_pattern"));
}

TEST(ProtobufUtil, SetFromGenericParams)
{
PbCommand command1;
SetFromGenericParams(command1, "operations=mapping_info:folder_pattern=pattern");
EXPECT_TRUE(SetFromGenericParams(command1, "operations=mapping_info:folder_pattern=pattern").empty());
EXPECT_EQ("mapping_info", GetParam(command1, "operations"));
EXPECT_EQ("pattern", GetParam(command1, "folder_pattern"));

PbCommand command2;
EXPECT_THROW(SetFromGenericParams(command2, "=mapping_info"), parser_exception);
EXPECT_FALSE(SetFromGenericParams(command2, "=mapping_info").empty());

PbCommand command3;
EXPECT_THROW(SetFromGenericParams(command3, "="), parser_exception);
EXPECT_FALSE(SetFromGenericParams(command3, "=").empty());
}

TEST(ProtobufUtil, ListDevices)
Expand Down

0 comments on commit d58c881

Please sign in to comment.