From 6a43bf048b2bbc672ac3dd57c4e2cf1b765c9e8f Mon Sep 17 00:00:00 2001 From: Alexey Utkin Date: Wed, 10 Aug 2022 11:39:25 +0300 Subject: [PATCH] [BUG] Cannot parse coverage.json as it contains warnings #345 - parse leading lines as warning with logging those lines --- server/src/utils/JsonUtils.cpp | 39 ++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/server/src/utils/JsonUtils.cpp b/server/src/utils/JsonUtils.cpp index 8ace4364..0d212886 100644 --- a/server/src/utils/JsonUtils.cpp +++ b/server/src/utils/JsonUtils.cpp @@ -1,26 +1,53 @@ #include "JsonUtils.h" - +#include "StringUtils.h" #include "utils/FileSystemUtils.h" +#include "utils/path/FileSystemPath.h" #include "loguru.h" -#include "utils/path/FileSystemPath.h" -#include #include +#include namespace JsonUtils { nlohmann::json getJsonFromFile(const fs::path &path) { std::ifstream stream(path.string()); std::stringstream buffer; buffer << stream.rdbuf(); + std::string contentToParse = buffer.str(); try { - nlohmann::json coverageJson = nlohmann::json::parse(buffer.str()); + // skip warning header lines from input stream like + // warning: 1 functions have mismatched data + // {JSON} + std::string::size_type pos = 0; + while (pos < contentToParse.size()) { + const char ch = contentToParse[pos]; + if (!std::isspace(ch)) { + if (ch == '{' || ch == '[') { + break; + } + pos = contentToParse.find('\n', pos); + if (pos == std::string::npos) { + const std::invalid_argument e("empty json file"); + LOG_S(ERROR) << e.what(); + throw e; + } + } + ++pos; + } + if (pos > 0) { + std::string warningHeader = contentToParse.substr(0, pos); + StringUtils::trim(warningHeader); + if (!warningHeader.empty()) { + LOG_S(WARNING) << "JSON header: [" << warningHeader << "] in: " << path.string(); + } + } + nlohmann::json coverageJson = nlohmann::json::parse(contentToParse.substr(pos)); return coverageJson; } catch (const std::exception &e) { - LOG_S(ERROR) << e.what() << ": " << buffer.str() << " in: " << path.string(); + LOG_S(ERROR) << e.what() << ": " << contentToParse << " in: " << path.string(); throw e; } catch (...) { - LOG_S(ERROR) << buffer.str(); + LOG_S(ERROR) << "Crash: " << contentToParse << " in: " << path.string(); throw; } }