check config when writing (#405)

* check config when writing

* integrate #403
This commit is contained in:
redPanther 2017-02-26 15:30:10 +01:00 committed by GitHub
parent 26154befa4
commit 72e2f0bf18
3 changed files with 30 additions and 5 deletions

View File

@ -107,8 +107,8 @@ To generate make files on OS X:
After which you can run cmake with the correct qt5 path:
```
cmake -DCMAKE_PREFIX_PATH=/usr/local/Cellar/qt5/5.7.0 -DCMAKE_BUILD_TYPE=Release ..
```
export QVER=$(find /usr/local/Cellar/qt5 -type d -name "5.*" | sort -n | head -n1)
cmake -DCMAKE_PREFIX_PATH=$QVER -DCMAKE_BUILD_TYPE=Release ..```
### Run make to build Hyperion
The `-j $(nproc)` specifies the amount of CPU cores to use.

View File

@ -1028,7 +1028,7 @@ void JsonClientConnection::handleSchemaGetCommand(const QJsonObject& message, co
// read the hyperion json schema from the resource
QFile schemaData(":/hyperion-schema-"+QString::number(_hyperion->getConfigVersionId()));
if (!schemaData.open(QIODevice::ReadOnly))
{
std::stringstream error;
@ -1039,7 +1039,7 @@ void JsonClientConnection::handleSchemaGetCommand(const QJsonObject& message, co
QByteArray schema = schemaData.readAll();
QJsonDocument doc = QJsonDocument::fromJson(schema, &error);
schemaData.close();
if (error.error != QJsonParseError::NoError)
{
// report to the user the failure and their locations in the document.

View File

@ -101,7 +101,32 @@ void CgiHandler::cmd_cfg_set()
if (error.error == QJsonParseError::NoError)
{
QJsonObject hyperionConfigJsonObj = hyperionConfig.object();
QJsonFactory::writeJson(QString::fromStdString(_hyperion->getConfigFileName()), hyperionConfigJsonObj);
try
{
// make sure the resources are loaded (they may be left out after static linking)
Q_INIT_RESOURCE(resource);
QJsonObject schemaJson = QJsonFactory::readSchema(":/hyperion-schema-"+QString::number(_hyperion->getConfigVersionId()));
QJsonSchemaChecker schemaChecker;
schemaChecker.setSchema(schemaJson);
if ( schemaChecker.validate(hyperionConfigJsonObj) )
{
QJsonFactory::writeJson(QString::fromStdString(_hyperion->getConfigFileName()), hyperionConfigJsonObj);
}
else
{
std::string errorMsg = "ERROR: Json validation failed: \n";
for (std::list<std::string>::const_iterator i = schemaChecker.getMessages().begin(); i != schemaChecker.getMessages().end(); ++i)
{
Error(_log, "config write validation: %s", (*i).c_str());
errorMsg += *i + "\n";
}
throw std::runtime_error(errorMsg.c_str());
}
}
catch(const std::runtime_error& validate_error)
{
_reply->appendRawData (QString(validate_error.what()).toUtf8());
}
}
else
{