Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

Commit

Permalink
Better exception handling, build JSON report first as that's less lik…
Browse files Browse the repository at this point in the history
…ely to error
  • Loading branch information
Stephen Foulds committed Aug 18, 2023
1 parent 5489cd5 commit 2877e45
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,16 @@ int main(int argc, char *argv[])
return ordered;
});

// Write the JSON first - this is safer and is the report automation need, so if we crash
// after this point we'll still get some data
if (gJson) {
std::filesystem::path jsonFilepath = gOutputDirectory / "report.json";
std::ofstream outputJson(jsonFilepath, std::ios::trunc | std::ios::binary);
outputJson << reportGenerator->getJson().dump(4);

LOG_INFO("Saved JSON data to %s", jsonFilepath.string().c_str());
}

try {
auto htmlTemplateString = std::string(g_templateHtml_data, g_templateHtml_data + g_templateHtml_size);
std::string result = env.render(htmlTemplateString, reportGenerator->getJson());
Expand All @@ -293,16 +303,9 @@ int main(int argc, char *argv[])
outputHtml << result;

LOG_INFO("Saved report to %s", htmlFilepath.string().c_str());
} catch (std::exception& e) {
} catch (const std::exception& e) {
LOG_ERROR("Failed to save HTML report with exception %s", e.what());
}

if (gJson) {
std::filesystem::path jsonFilepath = gOutputDirectory / "report.json";
std::ofstream outputJson(jsonFilepath, std::ios::trunc | std::ios::binary);
outputJson << reportGenerator->getJson().dump(4);

LOG_INFO("Saved JSON data to %s", jsonFilepath.string().c_str());
throw;
}

return EXIT_SUCCESS;
Expand Down

0 comments on commit 2877e45

Please sign in to comment.