From 32e428cc9fa3da16f9871ce5f3128a3bbb4734ca Mon Sep 17 00:00:00 2001 From: thurs Date: Thu, 26 Mar 2026 12:05:44 -0700 Subject: [PATCH] fix: use std::expected and std::error_code --- src/config/ConfigManager.cpp | 31 ++++++++++++++++++++----------- src/main.cpp | 22 ++++++++++++++-------- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/config/ConfigManager.cpp b/src/config/ConfigManager.cpp index 2bd4ed2..cc7e8b2 100644 --- a/src/config/ConfigManager.cpp +++ b/src/config/ConfigManager.cpp @@ -5,11 +5,15 @@ #include #include #include +#include + +static std::expected 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); } diff --git a/src/main.cpp b/src/main.cpp index cc1b875..cd7541e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,6 +2,7 @@ #include "config/ConfigManager.hpp" #include "core/Hypridle.hpp" #include "helpers/Log.hpp" +#include 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(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(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(); g_pHypridle->run();