forked from qt-creator/qt-creator
Python projects: Add error handling to the JSON parser
Display parse errors in case the file is invalid.
Amends f7e1354ae5
.
Task-number: QTCREATORBUG-21824
Change-Id: I0d357597257fcbc49719fae781c61f251a5392fe
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
@@ -33,6 +33,7 @@
|
|||||||
#include <coreplugin/documentmanager.h>
|
#include <coreplugin/documentmanager.h>
|
||||||
#include <coreplugin/fileiconprovider.h>
|
#include <coreplugin/fileiconprovider.h>
|
||||||
#include <coreplugin/id.h>
|
#include <coreplugin/id.h>
|
||||||
|
#include <coreplugin/messagemanager.h>
|
||||||
#include <coreplugin/editormanager/editormanager.h>
|
#include <coreplugin/editormanager/editormanager.h>
|
||||||
|
|
||||||
#include <projectexplorer/buildtargetinfo.h>
|
#include <projectexplorer/buildtargetinfo.h>
|
||||||
@@ -61,6 +62,7 @@
|
|||||||
#include <QTextCursor>
|
#include <QTextCursor>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
|
#include <QJsonParseError>
|
||||||
#include <QJsonValue>
|
#include <QJsonValue>
|
||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
|
|
||||||
@@ -344,30 +346,48 @@ static QStringList readLines(const Utils::FileName &projectFile)
|
|||||||
return lines;
|
return lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
static QStringList readLinesJson(const Utils::FileName &projectFile)
|
static QStringList readLinesJson(const Utils::FileName &projectFile,
|
||||||
|
QString *errorMessage)
|
||||||
{
|
{
|
||||||
const QString projectFileName = projectFile.fileName();
|
const QString projectFileName = projectFile.fileName();
|
||||||
QStringList lines = { projectFileName };
|
QStringList lines = { projectFileName };
|
||||||
|
|
||||||
QFile file(projectFile.toString());
|
QFile file(projectFile.toString());
|
||||||
if (!file.open(QFile::ReadOnly))
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||||
|
*errorMessage = PythonProject::tr("Unable to open \"%1\" for reading: %2")
|
||||||
|
.arg(projectFile.toUserOutput(), file.errorString());
|
||||||
return lines;
|
return lines;
|
||||||
|
}
|
||||||
|
|
||||||
const QByteArray content = file.readAll();
|
const QByteArray content = file.readAll();
|
||||||
|
|
||||||
// This assumes te project file is formed with only one field called
|
// This assumes te project file is formed with only one field called
|
||||||
// 'files' that has a list associated of the files to include in the project.
|
// 'files' that has a list associated of the files to include in the project.
|
||||||
if (!content.isEmpty()) {
|
if (content.isEmpty()) {
|
||||||
const QJsonDocument doc = QJsonDocument::fromJson(content);
|
*errorMessage = PythonProject::tr("Unable read \"%1\": The file is empty.")
|
||||||
const QJsonObject obj = doc.object();
|
.arg(projectFile.toUserOutput());
|
||||||
if (obj.contains("files")) {
|
return lines;
|
||||||
QJsonValue files = obj.value("files");
|
}
|
||||||
QJsonArray files_array = files.toArray();
|
|
||||||
QSet<QString> visited;
|
|
||||||
for (const auto &file : files_array)
|
|
||||||
visited.insert(file.toString());
|
|
||||||
|
|
||||||
lines.append(visited.toList());
|
QJsonParseError error;
|
||||||
}
|
const QJsonDocument doc = QJsonDocument::fromJson(content, &error);
|
||||||
|
if (doc.isNull()) {
|
||||||
|
const int line = content.left(error.offset).count('\n') + 1;
|
||||||
|
*errorMessage = PythonProject::tr("Unable parse %1:%2: %3")
|
||||||
|
.arg(projectFile.toUserOutput()).arg(line)
|
||||||
|
.arg(error.errorString());
|
||||||
|
return lines;
|
||||||
|
}
|
||||||
|
|
||||||
|
const QJsonObject obj = doc.object();
|
||||||
|
if (obj.contains("files")) {
|
||||||
|
QJsonValue files = obj.value("files");
|
||||||
|
QJsonArray files_array = files.toArray();
|
||||||
|
QSet<QString> visited;
|
||||||
|
for (const auto &file : files_array)
|
||||||
|
visited.insert(file.toString());
|
||||||
|
|
||||||
|
lines.append(visited.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
return lines;
|
return lines;
|
||||||
@@ -453,8 +473,12 @@ void PythonProject::parseProject()
|
|||||||
m_rawListEntries.clear();
|
m_rawListEntries.clear();
|
||||||
const Utils::FileName filePath = projectFilePath();
|
const Utils::FileName filePath = projectFilePath();
|
||||||
// The PySide project file is JSON based
|
// The PySide project file is JSON based
|
||||||
if (filePath.endsWith(".pyproject"))
|
if (filePath.endsWith(".pyproject")) {
|
||||||
m_rawFileList = readLinesJson(filePath);
|
QString errorMessage;
|
||||||
|
m_rawFileList = readLinesJson(filePath, &errorMessage);
|
||||||
|
if (!errorMessage.isEmpty())
|
||||||
|
Core::MessageManager::write(errorMessage);
|
||||||
|
}
|
||||||
// To keep compatibility with PyQt we keep the compatibility with plain
|
// To keep compatibility with PyQt we keep the compatibility with plain
|
||||||
// text files as project files.
|
// text files as project files.
|
||||||
else if (filePath.endsWith(".pyqtc"))
|
else if (filePath.endsWith(".pyqtc"))
|
||||||
|
Reference in New Issue
Block a user