Update inspector to enable and handle exceptions

This commit is contained in:
Tristan Penman 2021-11-05 08:57:42 +11:00
parent af071f0198
commit 4990e352a7
3 changed files with 24 additions and 12 deletions

View File

@ -31,6 +31,8 @@ add_project_meta(META_FILES_TO_INCLUDE)
find_package(QT NAMES Qt6 Qt5 COMPONENTS Core Widgets REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Widgets REQUIRED)
add_definitions(-DVALIJSON_USE_EXCEPTIONS=1)
add_executable(${PROJECT_NAME} ${OS_BUNDLE} # Expands to WIN32 or MACOS_BUNDLE depending on OS
${SOURCE_FILES} ${META_FILES_TO_INCLUDE} ${RESOURCE_FILES}
)

View File

@ -116,10 +116,16 @@ QToolBar * Window::createToolBar()
void Window::refreshDocumentJson()
{
const auto doc = m_documentEditor->toPlainText().toUtf8();
if (doc.isEmpty()) {
m_errors->setText("");
return;
}
QJsonParseError error;
m_document = QJsonDocument::fromJson(m_documentEditor->toPlainText().toUtf8(), &error);
m_document = QJsonDocument::fromJson(doc, &error);
if (m_document.isNull()) {
m_errors->setText(error.errorString());
m_errors->setText(QString("Document error: ") + error.errorString());
return;
}
@ -132,10 +138,16 @@ void Window::refreshDocumentJson()
void Window::refreshSchemaJson()
{
const auto schema = m_schemaEditor->toPlainText().toUtf8();
if (schema.isEmpty()) {
m_errors->setText("");
return;
}
QJsonParseError error;
auto schemaDoc = QJsonDocument::fromJson(m_schemaEditor->toPlainText().toUtf8(), &error);
if (schemaDoc.isNull()) {
m_errors->setText(error.errorString());
m_errors->setText(QString("Schema error: ") + error.errorString());
return;
}
@ -146,14 +158,12 @@ void Window::refreshSchemaJson()
m_schema = new valijson::Schema();
parser.populateSchema(adapter, *m_schema);
m_errors->setText("");
} catch (std::runtime_error error) {
validate();
} catch (std::runtime_error & error) {
delete m_schema;
m_schema = nullptr;
m_errors->setText(error.what());
m_errors->setText(QString("Schema error: ") + error.what());
}
validate();
}
void Window::showOpenDocumentDialog()
@ -192,11 +202,11 @@ void Window::validate()
std::stringstream ss;
while (results.popError(error)) {
std::string context;
for (auto itr = error.context.begin(); itr != error.context.end(); itr++) {
context += *itr;
for (auto & itr : error.context) {
context += itr;
}
ss << "Error #" << errorNum << std::endl
ss << "Validation error #" << errorNum << std::endl
<< " context: " << context << std::endl
<< " desc: " << error.description << std::endl;
++errorNum;

View File

@ -19,7 +19,7 @@ class Window : public QMainWindow
Q_OBJECT
public:
Window(QWidget * parent = 0);
explicit Window(QWidget * parent = 0);
public slots:
void refreshDocumentJson();