From 4990e352a710eb2eb6dc43e288e59441297c7b2f Mon Sep 17 00:00:00 2001 From: Tristan Penman Date: Fri, 5 Nov 2021 08:57:42 +1100 Subject: [PATCH] Update inspector to enable and handle exceptions --- inspector/CMakeLists.txt | 2 ++ inspector/src/window.cpp | 32 +++++++++++++++++++++----------- inspector/src/window.h | 2 +- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/inspector/CMakeLists.txt b/inspector/CMakeLists.txt index 264637e..009f748 100644 --- a/inspector/CMakeLists.txt +++ b/inspector/CMakeLists.txt @@ -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} ) diff --git a/inspector/src/window.cpp b/inspector/src/window.cpp index c59db24..9d649ac 100644 --- a/inspector/src/window.cpp +++ b/inspector/src/window.cpp @@ -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; diff --git a/inspector/src/window.h b/inspector/src/window.h index 5315126..0e71628 100644 --- a/inspector/src/window.h +++ b/inspector/src/window.h @@ -19,7 +19,7 @@ class Window : public QMainWindow Q_OBJECT public: - Window(QWidget * parent = 0); + explicit Window(QWidget * parent = 0); public slots: void refreshDocumentJson();