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 <rikingcoding@gmail.com>
This commit is contained in:
parent
aef3607bd3
commit
d1c38d9597
18 changed files with 137 additions and 32 deletions
1
third_party/nix/src/libutil/CMakeLists.txt
vendored
1
third_party/nix/src/libutil/CMakeLists.txt
vendored
|
|
@ -21,6 +21,7 @@ set(HEADER_FILES
|
|||
proto.hh
|
||||
ref.hh
|
||||
serialise.hh
|
||||
status.hh
|
||||
sync.hh
|
||||
thread-pool.hh
|
||||
types.hh
|
||||
|
|
|
|||
64
third_party/nix/src/libutil/proto.hh
vendored
64
third_party/nix/src/libutil/proto.hh
vendored
|
|
@ -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
|
||||
|
|
|
|||
17
third_party/nix/src/libutil/status.hh
vendored
Normal file
17
third_party/nix/src/libutil/status.hh
vendored
Normal file
|
|
@ -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
|
||||
Loading…
Add table
Add a link
Reference in a new issue