From 78340c3d2336c2d5ea5365120a8fbd2319764f58 Mon Sep 17 00:00:00 2001 From: Klaus Aehlig Date: Wed, 29 Mar 2023 18:45:57 +0200 Subject: Improve error messages on reading invalid target files ... by including the details of the parse error. --- src/buildtool/build_engine/base_maps/json_file_map.hpp | 14 +++++++++----- src/buildtool/main/main.cpp | 12 +++++++++++- 2 files changed, 20 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/buildtool/build_engine/base_maps/json_file_map.hpp b/src/buildtool/build_engine/base_maps/json_file_map.hpp index 15aa6234..412631ca 100644 --- a/src/buildtool/build_engine/base_maps/json_file_map.hpp +++ b/src/buildtool/build_engine/base_maps/json_file_map.hpp @@ -84,11 +84,15 @@ auto CreateJsonFileMap(std::size_t jobs) -> JsonFileMap { true); return; } - auto json = nlohmann::json::parse(*file_content, nullptr, false); - if (json.is_discarded()) { - (*logger)(fmt::format("JSON file {} does not contain valid JSON.", - json_file_path.string()), - true); + auto json = nlohmann::json(); + try { + json = nlohmann::json::parse(*file_content); + } catch (std::exception const& e) { + (*logger)( + fmt::format("JSON file {} does not contain valid JSON:\n{}", + json_file_path.string(), + e.what()), + true); return; } if (!json.is_object()) { diff --git a/src/buildtool/main/main.cpp b/src/buildtool/main/main.cpp index 35137d77..1c93d7c2 100644 --- a/src/buildtool/main/main.cpp +++ b/src/buildtool/main/main.cpp @@ -575,7 +575,17 @@ void SetupHashFunction() { Logger::Log(LogLevel::Error, "Cannot read file {}.", target_file); std::exit(kExitFailure); } - auto const json = nlohmann::json::parse(*file_content); + auto json = nlohmann::json(); + try { + json = nlohmann::json::parse(*file_content); + } catch (std::exception const& e) { + Logger::Log(LogLevel::Error, + "While searching for the default target in {}:\n" + "Failed to parse json with error {}", + target_file, + e.what()); + std::exit(kExitFailure); + } if (not json.is_object()) { Logger::Log( LogLevel::Error, "Invalid content in target file {}.", target_file); -- cgit v1.2.3