diff options
author | Griffin Smith <grfn@gws.fyi> | 2020-08-14T02·06-0400 |
---|---|---|
committer | glittershark <grfn@gws.fyi> | 2020-08-14T03·06+0000 |
commit | d1c38d9597e110bef92a548c86a651174bd385dc (patch) | |
tree | 98d64ac81a4f7ca91ad21f732c84bdc3f73df9cb /third_party/nix/src/libutil | |
parent | aef3607bd332a9a6adbb150abe63250745972532 (diff) |
refactor(tvix): Make Store::buildPaths return a Status r/1649
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 <rikingcoding@gmail.com>
Diffstat (limited to 'third_party/nix/src/libutil')
-rw-r--r-- | third_party/nix/src/libutil/CMakeLists.txt | 1 | ||||
-rw-r--r-- | third_party/nix/src/libutil/proto.hh | 64 | ||||
-rw-r--r-- | third_party/nix/src/libutil/status.hh | 17 |
3 files changed, 80 insertions, 2 deletions
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 <absl/status/status.h> #include <grpcpp/impl/codegen/status.h> +#include <grpcpp/impl/codegen/status_code_enum.h> #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 <absl/status/status.h> +#include <absl/strings/str_format.h> +#include <absl/strings/string_view.h> + +#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 |