diff options
author | Klaus Aehlig <klaus.aehlig@huawei.com> | 2023-03-29 18:45:57 +0200 |
---|---|---|
committer | Klaus Aehlig <klaus.aehlig@huawei.com> | 2023-03-30 14:00:44 +0200 |
commit | 78340c3d2336c2d5ea5365120a8fbd2319764f58 (patch) | |
tree | e3086d7802c3a58e65628e0b9b5154a1fefe494d /src | |
parent | 1d8321976c7634f80b7f878b4219899fe0748d70 (diff) | |
download | justbuild-78340c3d2336c2d5ea5365120a8fbd2319764f58.tar.gz |
Improve error messages on reading invalid target files
... by including the details of the parse error.
Diffstat (limited to 'src')
-rw-r--r-- | src/buildtool/build_engine/base_maps/json_file_map.hpp | 14 | ||||
-rw-r--r-- | src/buildtool/main/main.cpp | 12 |
2 files changed, 20 insertions, 6 deletions
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); |