Catch exception on erroneous rules

std::regex and std::regex_replace may throw an std::regex_error if the
expression or replacement contain errors.

Log this error and carry on with the next rule, so that the title is
shown even if the config contains errors.
pull/1055/head
Matthias Richter 2021-04-21 12:15:25 +02:00
parent b16c8972c7
commit af3c868a5b
1 changed files with 9 additions and 3 deletions

View File

@ -141,9 +141,15 @@ std::string Window::rewriteTitle(const std::string& title)
for (auto it = rules.begin(); it != rules.end(); ++it) { for (auto it = rules.begin(); it != rules.end(); ++it) {
if (it.key().isString() && it->isString()) { if (it.key().isString() && it->isString()) {
const std::regex rule{it.key().asString()}; try {
if (std::regex_match(title, rule)) { // malformated regexes will cause an exception.
return std::regex_replace(title, rule, it->asString()); // in this case, log error and try the next rule.
const std::regex rule{it.key().asString()};
if (std::regex_match(title, rule)) {
return std::regex_replace(title, rule, it->asString());
}
} catch (const std::regex_error& e) {
spdlog::error("Invalid rule {}: {}", it.key().asString(), e.what());
} }
} }
} }