From 1b27d5cf016457b7290361d83ba683c5a47e9b2d Mon Sep 17 00:00:00 2001 From: Francesco Biscani Date: Tue, 21 Jun 2022 11:13:25 +0200 Subject: [PATCH] Increase the verbosity of the error message produced when there are undefined references. --- src/json-validator.cpp | 31 ++++++++++++++++++++++++++----- test/issue-98.cpp | 3 ++- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/json-validator.cpp b/src/json-validator.cpp index 34076c2..7f34553 100644 --- a/src/json-validator.cpp +++ b/src/json-validator.cpp @@ -14,6 +14,7 @@ #include #include #include +#include using nlohmann::json; using nlohmann::json_patch; @@ -308,11 +309,31 @@ public: break; } while (1); - for (const auto &file : files_) - if (file.second.unresolved.size() != 0) + for (const auto &file : files_) { + if (file.second.unresolved.size() != 0) { + // Build a representation of the undefined + // references as a list of comma-separated strings. + auto n_urefs = file.second.unresolved.size(); + std::string urefs = "["; + + decltype(n_urefs) counter = 0; + for (const auto &p : file.second.unresolved) { + urefs += p.first; + + if (counter != n_urefs - 1u) { + urefs += ", "; + } + + ++counter; + } + + urefs += "]"; + throw std::invalid_argument("after all files have been parsed, '" + (file.first == "" ? "" : file.first) + - "' has still undefined references."); + "' has still the following undefined references: " + urefs); + } + } } void validate(const json::json_pointer &ptr, @@ -929,8 +950,8 @@ class boolean : public schema { if (!true_) { // false schema // empty array - //switch (instance.type()) { - //case json::value_t::array: + // switch (instance.type()) { + // case json::value_t::array: // if (instance.size() != 0) // valid false-schema // e.error(ptr, instance, "false-schema required empty array"); // return; diff --git a/test/issue-98.cpp b/test/issue-98.cpp index 088b324..63a824c 100644 --- a/test/issue-98.cpp +++ b/test/issue-98.cpp @@ -8,7 +8,8 @@ int main(void) try { validator.set_root_schema(nlBase); // this line will log the caught exception } catch (const std::exception &e) { - if (std::string("after all files have been parsed, '' has still undefined references.") == e.what()) + + if (std::string("after all files have been parsed, '' has still the following undefined references: [/unknown/keywords]") == e.what()) return EXIT_SUCCESS; } return EXIT_FAILURE;