Update error handling

This commit is contained in:
Uwe Seimet 2023-11-15 17:58:53 +01:00
parent 7fe4e1c9f5
commit 81f25dc180
3 changed files with 23 additions and 13 deletions

View File

@ -23,14 +23,15 @@ using namespace spdlog;
using namespace scsi_defs;
using namespace piscsi_interface;
string ScsiExecutor::Execute(const string& filename, bool binary)
bool ScsiExecutor::Execute(const string& filename, bool binary, string& result)
{
int size = 0;
if (!binary) {
ifstream in(filename);
if (in.fail()) {
return "Error opening JSON input file '" + filename + "'";
result = "Error opening JSON input file '" + filename + "'";
return false;
}
stringstream buf;
@ -42,7 +43,8 @@ string ScsiExecutor::Execute(const string& filename, bool binary)
else {
ifstream in(filename, ios::binary);
if (in.fail()) {
return "Error opening binary input file '" + filename + "'";
result = "Error opening binary input file '" + filename + "'";
return false;
}
vector<char> b(file_size(filename));
@ -63,15 +65,16 @@ string ScsiExecutor::Execute(const string& filename, bool binary)
if (!binary) {
const string json((const char*) buffer.data(), length);
return json;
result = json;
}
else {
PbResult result;
if (!result.ParseFromArray(buffer.data(), length)) {
assert(false);
PbResult r;
if (!r.ParseFromArray(buffer.data(), length)) {
result = "Error parsing binary protobuf data";
return false;
}
string json;
google::protobuf::util::MessageToJsonString(result, &json);
return json;
google::protobuf::util::MessageToJsonString(r, &result);
}
return true;
}

View File

@ -28,7 +28,7 @@ public:
}
~ScsiExecutor() = default;
string Execute(const string&, bool);
bool Execute(const string&, bool, string&);
void SetTarget(int id, int lun)
{

View File

@ -156,11 +156,18 @@ int ScsiExec::run(span<char*> args, bool in_process)
return EXIT_FAILURE;
}
cout << scsi_executor->Execute(filename, binary) << "\n" << flush;
string result;
const bool status = scsi_executor->Execute(filename, binary, result);
if (status) {
cout << result << '\n' << flush;
}
else {
cerr << result << endl;
}
CleanUp();
return EXIT_SUCCESS;
return status ? EXIT_SUCCESS : EXIT_FAILURE;
}
bool ScsiExec::SetLogLevel() const