Switched most of message-based error handling to std::expected.

Refactored enum class naming to use ThisCase.
Decorated most getters with [[nodiscard]].
This commit is contained in:
Alexander Shaduri
2024-03-22 18:21:08 +04:00
parent 6edcf19694
commit 351f5edaa9
53 changed files with 1043 additions and 921 deletions
+6 -6
View File
@@ -80,7 +80,7 @@ hz::fs::path get_smartctl_binary()
std::string execute_smartctl(const std::string& device, const std::string& device_opts,
hz::ExpectedVoid<SmartctlExecutorError> execute_smartctl(const std::string& device, const std::string& device_opts,
const std::string& command_options,
std::shared_ptr<CommandExecutor> smartctl_ex, std::string& smartctl_output)
{
@@ -89,7 +89,7 @@ std::string execute_smartctl(const std::string& device, const std::string& devic
const std::string::size_type pos = device.rfind('/'); // find basename
if (pos == std::string::npos) {
debug_out_error("app", DBG_FUNC_MSG << "Invalid device name \"" << device << "\".\n");
return _("Invalid device name specified.");
return hz::Unexpected(SmartctlExecutorError::InvalidDevice, _("Invalid device name specified."));
}
}
@@ -101,7 +101,7 @@ std::string execute_smartctl(const std::string& device, const std::string& devic
if (smartctl_binary.empty()) {
debug_out_error("app", DBG_FUNC_MSG << "Smartctl binary is not set in config.\n");
return _("Smartctl binary is not specified in configuration.");
return hz::Unexpected(SmartctlExecutorError::NoBinary, _("Smartctl binary is not specified in configuration."));
}
auto smartctl_def_options = rconfig::get_data<std::string>("system/smartctl_options");
@@ -127,18 +127,18 @@ std::string execute_smartctl(const std::string& device, const std::string& devic
// check if it's a device permission error.
// Smartctl open device: /dev/sdb failed: Permission denied
if (app_pcre_match("/Smartctl open device.+Permission denied/mi", smartctl_output)) {
return _("Permission denied while opening device.");
return hz::Unexpected(SmartctlExecutorError::PermissionDenied, _("Permission denied while opening device."));
}
// smartctl_output = smartctl_ex->get_stdout_str();
return smartctl_ex->get_error_msg();
return hz::Unexpected(SmartctlExecutorError::ExecutionError, smartctl_ex->get_error_msg());
}
// any_to_unix is needed for windows
smartctl_output = hz::string_trim_copy(hz::string_any_to_unix_copy(smartctl_ex->get_stdout_str()));
if (smartctl_output.empty()) {
debug_out_error("app", DBG_FUNC_MSG << "Smartctl returned an empty output.\n");
return _("Smartctl returned an empty output.");
return hz::Unexpected(SmartctlExecutorError::EmptyOutput, _("Smartctl returned an empty output."));
}
return {};