fix: use std::expected and std::error_code

This commit is contained in:
thurs 2026-03-26 12:05:44 -07:00
parent 7fa3b062b5
commit 32e428cc9f
2 changed files with 34 additions and 19 deletions

View file

@ -5,11 +5,15 @@
#include <filesystem>
#include <glob.h>
#include <cstring>
#include <expected>
static std::expected<std::string, std::error_code> getMainConfigPath(const std::string& overridePath = "") {
namespace fs = std::filesystem;
static std::string getMainConfigPath(const std::string& overridePath = "") {
if (!overridePath.empty()) {
if (!std::filesystem::exists(overridePath)) {
throw std::runtime_error("Provided config path does not exist: " + overridePath);
std::error_code ec;
if (!fs::exists(overridePath, ec)) {
return std::unexpected(std::make_error_code(std::errc::no_such_file_or_directory));
}
return overridePath;
}
@ -20,18 +24,23 @@ static std::string getMainConfigPath(const std::string& overridePath = "") {
return paths.first.value();
}
throw std::runtime_error(
"[ERR] Could not find hypridle.conf in:\n"
" $HOME/.config/hypr, /etc/xdg/hypr, $XDG_CONFIG_HOME/hypr or any $XDG_CONFIG_DIRS/hypr directories\n"
);
return std::unexpected(std::make_error_code(std::errc::no_such_file_or_directory));
}
CConfigManager::CConfigManager(std::string configPath) :
m_config(getMainConfigPath(configPath).c_str(),
Hyprlang::SConfigOptions{.throwAllErrors = true, .allowMissingConfig = false})
/* allowMissingConfig = 'true' to avoid library-level exceptions during initialization.
Existence is guaranteed by the path check below. */
m_config(getMainConfigPath(configPath).value_or("").c_str(),
Hyprlang::SConfigOptions{.throwAllErrors = false, .allowMissingConfig = true})
{
configCurrentPath = getMainConfigPath(configPath);
configHeadPath = configCurrentPath;
auto pathResult = getMainConfigPath(configPath);
if (!pathResult) {
return;
}
configCurrentPath = *pathResult;
configHeadPath = configCurrentPath;
Debug::log(LOG, "Using config file: {}", configCurrentPath);
}

View file

@ -2,6 +2,7 @@
#include "config/ConfigManager.hpp"
#include "core/Hypridle.hpp"
#include "helpers/Log.hpp"
#include <memory>
int main(int argc, char** argv, char** envp) {
std::string configPath;
@ -51,17 +52,22 @@ int main(int argc, char** argv, char** envp) {
}
}
try {
g_pConfigManager = std::make_unique<CConfigManager>(configPath);
g_pConfigManager->init();
} catch (const char* err) {
Debug::log(CRIT, "ConfigManager threw: {}", err);
std::string strerr = err;
if (strerr.contains("File does not exist"))
Debug::log(NONE, " Make sure you have a config.");
g_pConfigManager = std::make_unique<CConfigManager>(configPath);
if (g_pConfigManager->configCurrentPath.empty()) {
if (!configPath.empty()) {
Debug::log(CRIT, "ConfigManager: Specified file not found: {}\n", configPath);
} else {
Debug::log(CRIT, "ConfigManager: No hypridle.conf file found in:");
Debug::log(NONE, " $XDG_CONFIG_HOME/hypr/, ~/.config/hypr/, [XDG_CONFIG_DIRS]/hypr/, /etc/xdg/hypr/\n");
Debug::log(NONE, "Create a config or specify one manually:");
Debug::log(NONE, " hypridle -c /path/to/conf");
}
return 1;
}
g_pConfigManager->init();
g_pHypridle = std::make_unique<CHypridle>();
g_pHypridle->run();