From d1c38d9597e110bef92a548c86a651174bd385dc Mon Sep 17 00:00:00 2001 From: Griffin Smith Date: Thu, 13 Aug 2020 22:06:23 -0400 Subject: refactor(tvix): Make Store::buildPaths return a Status Make Store::buildPaths return a Status with [[nodiscard]] rather than throwing exceptions to signal failure. This is the beginning of a long road to refactor the entire store API to be status/statusor based instead of using exceptions. Change-Id: I2e32371c95a25b87ad129987c217d49c6d6e0c85 Reviewed-on: https://cl.tvl.fyi/c/depot/+/1745 Tested-by: BuildkiteCI Reviewed-by: kanepyork --- third_party/nix/src/libutil/CMakeLists.txt | 1 + third_party/nix/src/libutil/proto.hh | 64 +++++++++++++++++++++++++++++- third_party/nix/src/libutil/status.hh | 17 ++++++++ 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 third_party/nix/src/libutil/status.hh (limited to 'third_party/nix/src/libutil') diff --git a/third_party/nix/src/libutil/CMakeLists.txt b/third_party/nix/src/libutil/CMakeLists.txt index cdf4655c4007..0b36929218e8 100644 --- a/third_party/nix/src/libutil/CMakeLists.txt +++ b/third_party/nix/src/libutil/CMakeLists.txt @@ -21,6 +21,7 @@ set(HEADER_FILES proto.hh ref.hh serialise.hh + status.hh sync.hh thread-pool.hh types.hh diff --git a/third_party/nix/src/libutil/proto.hh b/third_party/nix/src/libutil/proto.hh index 484a10a82ac0..058cb7b7b452 100644 --- a/third_party/nix/src/libutil/proto.hh +++ b/third_party/nix/src/libutil/proto.hh @@ -2,19 +2,20 @@ #include #include +#include #include "libproto/worker.pb.h" #include "libutil/types.hh" namespace nix::util::proto { -::nix::proto::StorePath StorePath(const Path& path) { +inline ::nix::proto::StorePath StorePath(const Path& path) { ::nix::proto::StorePath store_path; store_path.set_path(path); return store_path; } -::nix::proto::StorePaths StorePaths(const PathSet& paths) { +inline ::nix::proto::StorePaths StorePaths(const PathSet& paths) { ::nix::proto::StorePaths result; for (const auto& path : paths) { result.add_paths(path); @@ -70,6 +71,47 @@ constexpr absl::StatusCode GRPCStatusCodeToAbsl(grpc::StatusCode code) { } } +constexpr grpc::StatusCode AbslStatusCodeToGRPC(absl::StatusCode code) { + switch (code) { + case absl::StatusCode::kOk: + return grpc::StatusCode::OK; + case absl::StatusCode::kCancelled: + return grpc::StatusCode::CANCELLED; + case absl::StatusCode::kUnknown: + return grpc::StatusCode::UNKNOWN; + case absl::StatusCode::kInvalidArgument: + return grpc::StatusCode::INVALID_ARGUMENT; + case absl::StatusCode::kDeadlineExceeded: + return grpc::StatusCode::DEADLINE_EXCEEDED; + case absl::StatusCode::kNotFound: + return grpc::StatusCode::NOT_FOUND; + case absl::StatusCode::kAlreadyExists: + return grpc::StatusCode::ALREADY_EXISTS; + case absl::StatusCode::kPermissionDenied: + return grpc::StatusCode::PERMISSION_DENIED; + case absl::StatusCode::kUnauthenticated: + return grpc::StatusCode::UNAUTHENTICATED; + case absl::StatusCode::kResourceExhausted: + return grpc::StatusCode::RESOURCE_EXHAUSTED; + case absl::StatusCode::kFailedPrecondition: + return grpc::StatusCode::FAILED_PRECONDITION; + case absl::StatusCode::kAborted: + return grpc::StatusCode::ABORTED; + case absl::StatusCode::kOutOfRange: + return grpc::StatusCode::OUT_OF_RANGE; + case absl::StatusCode::kUnimplemented: + return grpc::StatusCode::UNIMPLEMENTED; + case absl::StatusCode::kInternal: + return grpc::StatusCode::INTERNAL; + case absl::StatusCode::kUnavailable: + return grpc::StatusCode::UNAVAILABLE; + case absl::StatusCode::kDataLoss: + return grpc::StatusCode::DATA_LOSS; + default: + return grpc::StatusCode::INTERNAL; + } +} + constexpr absl::string_view GRPCStatusCodeDescription(grpc::StatusCode code) { switch (code) { case grpc::StatusCode::OK: @@ -111,4 +153,22 @@ constexpr absl::string_view GRPCStatusCodeDescription(grpc::StatusCode code) { }; } +inline absl::Status GRPCStatusToAbsl(grpc::Status status) { + if (status.ok()) { + return absl::OkStatus(); + } + + return absl::Status(GRPCStatusCodeToAbsl(status.error_code()), + status.error_message()); +} + +inline grpc::Status AbslToGRPCStatus(absl::Status status) { + if (status.ok()) { + return grpc::Status::OK; + } + + return grpc::Status(AbslStatusCodeToGRPC(status.code()), + std::string(status.message())); +} + } // namespace nix::util::proto diff --git a/third_party/nix/src/libutil/status.hh b/third_party/nix/src/libutil/status.hh new file mode 100644 index 000000000000..aeee0f502257 --- /dev/null +++ b/third_party/nix/src/libutil/status.hh @@ -0,0 +1,17 @@ +#pragma once + +#include +#include +#include + +#include "libutil/types.hh" + +namespace nix::util { + +inline void OkOrThrow(absl::Status status) { + if (!status.ok()) { + throw Error(absl::StrFormat("Operation failed: %s", status.ToString())); + } +} + +} // namespace nix::util -- cgit 1.4.1