Skip to content

Commit ca17166

Browse files
committed
Added exception handling to handle incorrect keyword. Related to #28
1 parent 29e0742 commit ca17166

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/Syntax.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,28 @@ void Syntax::loadSyntaxRules(const YAML::Node &config)
4646
// Iterate through each rule in the category
4747
for (const auto &rule : rules)
4848
{
49-
QString regex = QString::fromStdString(rule["regex"].as<std::string>());
50-
QColor color(QString::fromStdString(rule["color"].as<std::string>()));
49+
50+
QString regex;
51+
try{
52+
std::string regexStr = rule["regex"].as<std::string>(); //will throw exception if the key does not exist
53+
regex = QString::fromStdString(regexStr);
54+
}catch(const YAML::Exception e){
55+
qWarning() << " YAML exception when parsion the regex in syntax file" << e.what();
56+
continue;
57+
}
58+
5159
qDebug() << "regex: " << regex;
5260

61+
QColor color;
62+
try{
63+
std::string colorStr = rule["color"].as<std::string>();
64+
color = QColor(QString::fromStdString(colorStr));
65+
}catch(const YAML::Exception e){
66+
qWarning() << " YAML exception when parsion the color in syntax file" << e.what();
67+
continue;
68+
}
69+
70+
5371
// Create a QTextCharFormat for the rule
5472
QTextCharFormat format;
5573
format.setForeground(color);

0 commit comments

Comments
 (0)