diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/buildtool/common/remote/TARGETS | 7 | ||||
-rw-r--r-- | src/buildtool/common/remote/remote_common.hpp | 67 | ||||
-rw-r--r-- | src/buildtool/execution_api/remote/config.cpp | 6 | ||||
-rw-r--r-- | src/buildtool/serve_api/serve_service/target.cpp | 20 |
4 files changed, 47 insertions, 53 deletions
diff --git a/src/buildtool/common/remote/TARGETS b/src/buildtool/common/remote/TARGETS index ae4c9b5b..06604234 100644 --- a/src/buildtool/common/remote/TARGETS +++ b/src/buildtool/common/remote/TARGETS @@ -29,7 +29,12 @@ { "type": ["@", "rules", "CC", "library"] , "name": ["remote_common"] , "hdrs": ["remote_common.hpp"] - , "deps": [["@", "fmt", "", "fmt"], ["@", "json", "", "json"], "port"] + , "deps": + [ ["@", "fmt", "", "fmt"] + , ["@", "json", "", "json"] + , ["src/utils/cpp", "expected"] + , "port" + ] , "stage": ["src", "buildtool", "common", "remote"] } , "retry_parameters": diff --git a/src/buildtool/common/remote/remote_common.hpp b/src/buildtool/common/remote/remote_common.hpp index 70c1b6cb..0f995dd5 100644 --- a/src/buildtool/common/remote/remote_common.hpp +++ b/src/buildtool/common/remote/remote_common.hpp @@ -20,12 +20,12 @@ #include <sstream> #include <string> #include <utility> -#include <variant> #include <vector> #include "fmt/core.h" #include "nlohmann/json.hpp" #include "src/buildtool/common/remote/port.hpp" +#include "src/utils/cpp/expected.hpp" struct ServerAddress { std::string host{}; @@ -64,69 +64,66 @@ struct ServerAddress { } [[nodiscard]] static auto -ParseDispatch(std::string const& dispatch_info) noexcept -> std::variant< - std::string, - std::vector<std::pair<std::map<std::string, std::string>, ServerAddress>>> { - using result_t = - std::variant<std::string, - std::vector<std::pair<std::map<std::string, std::string>, - ServerAddress>>>; +ParseDispatch(std::string const& dispatch_info) noexcept -> expected< + std::vector<std::pair<std::map<std::string, std::string>, ServerAddress>>, + std::string> { nlohmann::json dispatch; try { dispatch = nlohmann::json::parse(dispatch_info); } catch (std::exception const& e) { - return fmt::format("Failed to parse endpoint configuration: {}", - e.what()); + return unexpected{fmt::format( + "Failed to parse endpoint configuration: {}", e.what())}; } std::vector<std::pair<std::map<std::string, std::string>, ServerAddress>> parsed{}; try { if (not dispatch.is_array()) { - return fmt::format( - "Endpoint configuration has to be a " - "list of pairs, but found {}", - dispatch.dump()); + return unexpected{ + fmt::format("Endpoint configuration has to be a " + "list of pairs, but found {}", + dispatch.dump())}; } for (auto const& entry : dispatch) { if (not(entry.is_array() and entry.size() == 2)) { - return fmt::format( - "Endpoint configuration has to be a list of " - "pairs, but found entry {}", - entry.dump()); + return unexpected{ + fmt::format("Endpoint configuration has to be a list of " + "pairs, but found entry {}", + entry.dump())}; } if (not entry[0].is_object()) { - return fmt::format( - "Property condition has to be " - "given as an object, but found {}", - entry[0].dump()); + return unexpected{ + fmt::format("Property condition has to be " + "given as an object, but found {}", + entry[0].dump())}; } std::map<std::string, std::string> props{}; for (auto const& [k, v] : entry[0].items()) { if (not v.is_string()) { - return fmt::format( - "Property condition has to be given as an " - "object of strings but found {}", - entry[0].dump()); + return unexpected{ + fmt::format("Property condition has to be given as an " + "object of strings but found {}", + entry[0].dump())}; } props.emplace(k, v.template get<std::string>()); } if (not entry[1].is_string()) { - return fmt::format( - "Endpoint has to be specified as string (in " - "the form host:port), but found {}", - entry[1].dump()); + return unexpected{ + fmt::format("Endpoint has to be specified as string (in " + "the form host:port), but found {}", + entry[1].dump())}; } auto endpoint = ParseAddress(entry[1].template get<std::string>()); if (not endpoint) { - return fmt::format("Failed to parse {} as endpoint.", - entry[1].dump()); + return unexpected{fmt::format("Failed to parse {} as endpoint.", + entry[1].dump())}; } parsed.emplace_back(props, *endpoint); } } catch (std::exception const& e) { - return fmt::format("Failure analysing endpoint configuration {}: {}", - dispatch.dump(), - e.what()); + return unexpected{ + fmt::format("Failure analysing endpoint configuration {}: {}", + dispatch.dump(), + e.what())}; } // success! return parsed; diff --git a/src/buildtool/execution_api/remote/config.cpp b/src/buildtool/execution_api/remote/config.cpp index d878b93b..2f166f8c 100644 --- a/src/buildtool/execution_api/remote/config.cpp +++ b/src/buildtool/execution_api/remote/config.cpp @@ -28,11 +28,11 @@ try { if (auto dispatch_info = FileSystemManager::ReadFile(filename)) { auto parsed = ParseDispatch(*dispatch_info); - if (parsed.index() == 0) { - Logger::Log(LogLevel::Warning, std::get<0>(parsed)); + if (not parsed) { + Logger::Log(LogLevel::Warning, std::move(parsed).error()); return false; } - Instance().dispatch_ = std::move(std::get<1>(parsed)); + Instance().dispatch_ = *std::move(parsed); return true; } Logger::Log(LogLevel::Warning, diff --git a/src/buildtool/serve_api/serve_service/target.cpp b/src/buildtool/serve_api/serve_service/target.cpp index d7e48402..341b326e 100644 --- a/src/buildtool/serve_api/serve_service/target.cpp +++ b/src/buildtool/serve_api/serve_service/target.cpp @@ -67,21 +67,13 @@ auto TargetService::GetDispatchList( dispatch_info.ToString())}; } // parse content - try { - auto parsed = ParseDispatch(*dispatch_str); - if (parsed.index() == 0) { - // pass the parsing error forward - return ::grpc::Status{::grpc::StatusCode::FAILED_PRECONDITION, - std::get<0>(parsed)}; - } - return std::get<1>(parsed); - } catch (std::exception const& e) { - return ::grpc::Status{::grpc::StatusCode::INTERNAL, - fmt::format("Parsing dispatch blob {} " - "unexpectedly failed with:\n{}", - dispatch_digest.hash(), - e.what())}; + auto parsed = ParseDispatch(*dispatch_str); + if (not parsed) { + // pass the parsing error forward + return ::grpc::Status{::grpc::StatusCode::FAILED_PRECONDITION, + std::move(parsed).error()}; } + return *std::move(parsed); } auto TargetService::HandleFailureLog( |