core: don't crash on errors

fixes #19
This commit is contained in:
Vaxry
2025-04-20 23:41:54 +01:00
parent 9d3922d1d3
commit c60abae9bb
7 changed files with 35 additions and 11 deletions

View File

@@ -31,7 +31,7 @@ CConfig::CConfig() {
NFsUtils::readFileAsString(NFsUtils::isAbsolute(g_pGlobalState->configPath) ? g_pGlobalState->configPath : g_pGlobalState->cwd + "/" + g_pGlobalState->configPath).value());
if (!json.has_value())
throw std::runtime_error("No config / bad config format");
Debug::die("No config or config has bad format");
m_config = json.value();
@@ -46,7 +46,7 @@ CConfig::CConfig() {
parsed.exclude_regex = std::make_unique<re2::RE2>(ic.exclude_regex);
if (parsed.exclude_regex->error_code() != RE2::NoError) {
Debug::log(CRIT, "Regex \"{}\" failed to parse", ic.exclude_regex);
throw std::runtime_error("Failed to parse regex");
Debug::die("Failed to parse regex");
}
}

View File

@@ -3,13 +3,15 @@
#include <algorithm>
#include <stdexcept>
#include "../debug/log.hpp"
CIP::CIP(const std::string& ip) {
if (std::count(ip.begin(), ip.end(), '.') == 3)
parseV4(ip);
else if (std::count(ip.begin(), ip.end(), ':') >= 2)
parseV6(ip);
else
throw std::runtime_error("IP not valid");
Debug::die("Invalid IP: {}", ip);
}
void CIP::parseV4(const std::string& ip) {
@@ -32,7 +34,7 @@ void CIP::parseV4(const std::string& ip) {
m_blocks.push_back(std::stoul(std::string{curr}));
if (m_blocks.back() > 0xFF)
throw std::runtime_error("Invalid IPv4 byte");
Debug::die("Invalid ipv4 byte: {}", curr);
}
}
@@ -74,13 +76,13 @@ void CIP::parseV6(const std::string& ip) {
m_blocks.push_back(std::stoul(std::string{curr}, nullptr, 16));
if (m_blocks.back() > 0xFFFF)
throw std::runtime_error("Invalid IPv6 byte");
Debug::die("Invalid ipv6 byte: {}", curr);
}
}
CIPRange::CIPRange(const std::string& range) {
if (!range.contains('/'))
throw std::runtime_error("Range has no subnet");
Debug::die("IP range {} has no subnet", range);
m_subnet = std::stoul(range.substr(range.find('/') + 1));