Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make util::Status more similar to absl::Status #8405

Merged
merged 1 commit into from Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
136 changes: 132 additions & 4 deletions src/google/protobuf/stubs/status.cc
Expand Up @@ -85,10 +85,6 @@ inline std::string StatusCodeToString(StatusCode code) {

} // namespace

const Status Status::OK = Status();
const Status Status::CANCELLED = Status(StatusCode::kCancelled, "");
const Status Status::UNKNOWN = Status(StatusCode::kUnknown, "");

Status::Status() : error_code_(StatusCode::kOk) {
}

Expand Down Expand Up @@ -127,11 +123,143 @@ std::string Status::ToString() const {
}
}

Status OkStatus() {
return Status();
}

std::ostream& operator<<(std::ostream& os, const Status& x) {
os << x.ToString();
return os;
}

bool IsAborted(const Status& status) {
return status.code() == StatusCode::kAborted;
}

bool IsAlreadyExists(const Status& status) {
return status.code() == StatusCode::kAlreadyExists;
}

bool IsCancelled(const Status& status) {
return status.code() == StatusCode::kCancelled;
}

bool IsDataLoss(const Status& status) {
return status.code() == StatusCode::kDataLoss;
}

bool IsDeadlineExceeded(const Status& status) {
return status.code() == StatusCode::kDeadlineExceeded;
}

bool IsFailedPrecondition(const Status& status) {
return status.code() == StatusCode::kFailedPrecondition;
}

bool IsInternal(const Status& status) {
return status.code() == StatusCode::kInternal;
}

bool IsInvalidArgument(const Status& status) {
return status.code() == StatusCode::kInvalidArgument;
}

bool IsNotFound(const Status& status) {
return status.code() == StatusCode::kNotFound;
}

bool IsOutOfRange(const Status& status) {
return status.code() == StatusCode::kOutOfRange;
}

bool IsPermissionDenied(const Status& status) {
return status.code() == StatusCode::kPermissionDenied;
}

bool IsResourceExhausted(const Status& status) {
return status.code() == StatusCode::kResourceExhausted;
}

bool IsUnauthenticated(const Status& status) {
return status.code() == StatusCode::kUnauthenticated;
}

bool IsUnavailable(const Status& status) {
return status.code() == StatusCode::kUnavailable;
}

bool IsUnimplemented(const Status& status) {
return status.code() == StatusCode::kUnimplemented;
}

bool IsUnknown(const Status& status) {
return status.code() == StatusCode::kUnknown;
}

Status AbortedError(StringPiece message) {
return Status(StatusCode::kAborted, message);
}

Status AlreadyExistsError(StringPiece message) {
return Status(StatusCode::kAlreadyExists, message);
}

Status CancelledError(StringPiece message) {
return Status(StatusCode::kCancelled, message);
}

Status DataLossError(StringPiece message) {
return Status(StatusCode::kDataLoss, message);
}

Status DeadlineExceededError(StringPiece message) {
return Status(StatusCode::kDeadlineExceeded, message);
}

Status FailedPreconditionError(StringPiece message) {
return Status(StatusCode::kFailedPrecondition, message);
}

Status InternalError(StringPiece message) {
return Status(StatusCode::kInternal, message);
}

Status InvalidArgumentError(StringPiece message) {
return Status(StatusCode::kInvalidArgument, message);
}

Status NotFoundError(StringPiece message) {
return Status(StatusCode::kNotFound, message);
}

Status OutOfRangeError(StringPiece message) {
return Status(StatusCode::kOutOfRange, message);
}

Status PermissionDeniedError(StringPiece message) {
return Status(StatusCode::kPermissionDenied, message);
}

Status ResourceExhaustedError(StringPiece message) {
return Status(StatusCode::kResourceExhausted, message);
}

Status UnauthenticatedError(StringPiece message) {
return Status(StatusCode::kUnauthenticated, message);
}

Status UnavailableError(StringPiece message) {
return Status(StatusCode::kUnavailable, message);
}

Status UnimplementedError(StringPiece message) {
return Status(StatusCode::kUnimplemented, message);
}

Status UnknownError(StringPiece message) {
return Status(StatusCode::kUnknown, message);
}

} // namespace status_internal
} // namespace util
} // namespace protobuf
Expand Down
96 changes: 81 additions & 15 deletions src/google/protobuf/stubs/status.h
Expand Up @@ -77,11 +77,6 @@ class PROTOBUF_EXPORT Status {
Status& operator=(const Status& x);
~Status() {}

// Some pre-defined Status objects
static const Status OK; // Identical to 0-arg constructor
static const Status CANCELLED;
static const Status UNKNOWN;

// Accessor
bool ok() const {
return error_code_ == StatusCode::kOk;
Expand All @@ -106,25 +101,96 @@ class PROTOBUF_EXPORT Status {
std::string error_message_;
};

// Returns an OK status, equivalent to a default constructed instance. Prefer
// usage of `OkStatus()` when constructing such an OK status.
PROTOBUF_EXPORT Status OkStatus();

// Prints a human-readable representation of 'x' to 'os'.
PROTOBUF_EXPORT std::ostream& operator<<(std::ostream& os, const Status& x);

// These convenience functions return `true` if a given status matches the
// `StatusCode` error code of its associated function.
PROTOBUF_EXPORT bool IsAborted(const Status& status);
PROTOBUF_EXPORT bool IsAlreadyExists(const Status& status);
PROTOBUF_EXPORT bool IsCancelled(const Status& status);
PROTOBUF_EXPORT bool IsDataLoss(const Status& status);
PROTOBUF_EXPORT bool IsDeadlineExceeded(const Status& status);
PROTOBUF_EXPORT bool IsFailedPrecondition(const Status& status);
PROTOBUF_EXPORT bool IsInternal(const Status& status);
PROTOBUF_EXPORT bool IsInvalidArgument(const Status& status);
PROTOBUF_EXPORT bool IsNotFound(const Status& status);
PROTOBUF_EXPORT bool IsOutOfRange(const Status& status);
PROTOBUF_EXPORT bool IsPermissionDenied(const Status& status);
PROTOBUF_EXPORT bool IsResourceExhausted(const Status& status);
PROTOBUF_EXPORT bool IsUnauthenticated(const Status& status);
PROTOBUF_EXPORT bool IsUnavailable(const Status& status);
PROTOBUF_EXPORT bool IsUnimplemented(const Status& status);
PROTOBUF_EXPORT bool IsUnknown(const Status& status);

// These convenience functions create an `Status` object with an error code as
// indicated by the associated function name, using the error message passed in
// `message`.
//
// These functions are intentionally named `*Error` rather than `*Status` to
// match the names from Abseil:
// https://github.com/abseil/abseil-cpp/blob/2e9532cc6c701a8323d0cffb468999ab804095ab/absl/status/status.h#L716
PROTOBUF_EXPORT Status AbortedError(StringPiece message);
PROTOBUF_EXPORT Status AlreadyExistsError(StringPiece message);
PROTOBUF_EXPORT Status CancelledError(StringPiece message);
PROTOBUF_EXPORT Status DataLossError(StringPiece message);
PROTOBUF_EXPORT Status DeadlineExceededError(StringPiece message);
PROTOBUF_EXPORT Status FailedPreconditionError(StringPiece message);
PROTOBUF_EXPORT Status InternalError(StringPiece message);
PROTOBUF_EXPORT Status InvalidArgumentError(StringPiece message);
PROTOBUF_EXPORT Status NotFoundError(StringPiece message);
PROTOBUF_EXPORT Status OutOfRangeError(StringPiece message);
PROTOBUF_EXPORT Status PermissionDeniedError(StringPiece message);
PROTOBUF_EXPORT Status ResourceExhaustedError(StringPiece message);
PROTOBUF_EXPORT Status UnauthenticatedError(StringPiece message);
PROTOBUF_EXPORT Status UnavailableError(StringPiece message);
PROTOBUF_EXPORT Status UnimplementedError(StringPiece message);
PROTOBUF_EXPORT Status UnknownError(StringPiece message);

} // namespace status_internal

using ::google::protobuf::util::status_internal::Status;
using ::google::protobuf::util::status_internal::StatusCode;

namespace error {

// TODO(yannic): Remove these.
constexpr StatusCode OK = StatusCode::kOk;
constexpr StatusCode CANCELLED = StatusCode::kCancelled;
constexpr StatusCode UNKNOWN = StatusCode::kUnknown;
constexpr StatusCode INVALID_ARGUMENT = StatusCode::kInvalidArgument;
constexpr StatusCode NOT_FOUND = StatusCode::kNotFound;
constexpr StatusCode INTERNAL = StatusCode::kInternal;
using ::google::protobuf::util::status_internal::IsAborted;
using ::google::protobuf::util::status_internal::IsAlreadyExists;
using ::google::protobuf::util::status_internal::IsCancelled;
using ::google::protobuf::util::status_internal::IsDataLoss;
using ::google::protobuf::util::status_internal::IsDeadlineExceeded;
using ::google::protobuf::util::status_internal::IsFailedPrecondition;
using ::google::protobuf::util::status_internal::IsInternal;
using ::google::protobuf::util::status_internal::IsInvalidArgument;
using ::google::protobuf::util::status_internal::IsNotFound;
using ::google::protobuf::util::status_internal::IsOutOfRange;
using ::google::protobuf::util::status_internal::IsPermissionDenied;
using ::google::protobuf::util::status_internal::IsResourceExhausted;
using ::google::protobuf::util::status_internal::IsUnauthenticated;
using ::google::protobuf::util::status_internal::IsUnavailable;
using ::google::protobuf::util::status_internal::IsUnimplemented;
using ::google::protobuf::util::status_internal::IsUnknown;

using ::google::protobuf::util::status_internal::OkStatus;
using ::google::protobuf::util::status_internal::AbortedError;
using ::google::protobuf::util::status_internal::AlreadyExistsError;
using ::google::protobuf::util::status_internal::CancelledError;
using ::google::protobuf::util::status_internal::DataLossError;
using ::google::protobuf::util::status_internal::DeadlineExceededError;
using ::google::protobuf::util::status_internal::FailedPreconditionError;
using ::google::protobuf::util::status_internal::InternalError;
using ::google::protobuf::util::status_internal::InvalidArgumentError;
using ::google::protobuf::util::status_internal::NotFoundError;
using ::google::protobuf::util::status_internal::OutOfRangeError;
using ::google::protobuf::util::status_internal::PermissionDeniedError;
using ::google::protobuf::util::status_internal::ResourceExhaustedError;
using ::google::protobuf::util::status_internal::UnauthenticatedError;
using ::google::protobuf::util::status_internal::UnavailableError;
using ::google::protobuf::util::status_internal::UnimplementedError;
using ::google::protobuf::util::status_internal::UnknownError;

} // namespace error
} // namespace util
} // namespace protobuf
} // namespace google
Expand Down