Skip to content

Commit 64b0daf

Browse files
authored
[BUG] Cannot parse coverage.json as it contains warnings #345 (#380)
- parse leading lines as warning with logging those lines
1 parent e42fdde commit 64b0daf

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

server/src/utils/JsonUtils.cpp

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,53 @@
11
#include "JsonUtils.h"
2-
2+
#include "StringUtils.h"
33
#include "utils/FileSystemUtils.h"
4+
#include "utils/path/FileSystemPath.h"
45

56
#include "loguru.h"
67

7-
#include "utils/path/FileSystemPath.h"
8-
#include <fstream>
98
#include <exception>
9+
#include <fstream>
1010

1111
namespace JsonUtils {
1212
nlohmann::json getJsonFromFile(const fs::path &path) {
1313
std::ifstream stream(path.string());
1414
std::stringstream buffer;
1515
buffer << stream.rdbuf();
16+
std::string contentToParse = buffer.str();
1617
try {
17-
nlohmann::json coverageJson = nlohmann::json::parse(buffer.str());
18+
// skip warning header lines from input stream like
19+
// warning: 1 functions have mismatched data
20+
// {JSON}
21+
std::string::size_type pos = 0;
22+
while (pos < contentToParse.size()) {
23+
const char ch = contentToParse[pos];
24+
if (!std::isspace(ch)) {
25+
if (ch == '{' || ch == '[') {
26+
break;
27+
}
28+
pos = contentToParse.find('\n', pos);
29+
if (pos == std::string::npos) {
30+
const std::invalid_argument e("empty json file");
31+
LOG_S(ERROR) << e.what();
32+
throw e;
33+
}
34+
}
35+
++pos;
36+
}
37+
if (pos > 0) {
38+
std::string warningHeader = contentToParse.substr(0, pos);
39+
StringUtils::trim(warningHeader);
40+
if (!warningHeader.empty()) {
41+
LOG_S(WARNING) << "JSON header: [" << warningHeader << "] in: " << path.string();
42+
}
43+
}
44+
nlohmann::json coverageJson = nlohmann::json::parse(contentToParse.substr(pos));
1845
return coverageJson;
1946
} catch (const std::exception &e) {
20-
LOG_S(ERROR) << e.what() << ": " << buffer.str() << " in: " << path.string();
47+
LOG_S(ERROR) << e.what() << ": " << contentToParse << " in: " << path.string();
2148
throw e;
2249
} catch (...) {
23-
LOG_S(ERROR) << buffer.str();
50+
LOG_S(ERROR) << "Crash: " << contentToParse << " in: " << path.string();
2451
throw;
2552
}
2653
}

0 commit comments

Comments
 (0)