diff --git a/src/google/protobuf/stubs/status.cc b/src/google/protobuf/stubs/status.cc index 731e1084e1a7..e0718521a4d8 100644 --- a/src/google/protobuf/stubs/status.cc +++ b/src/google/protobuf/stubs/status.cc @@ -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) { } @@ -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 diff --git a/src/google/protobuf/stubs/status.h b/src/google/protobuf/stubs/status.h index 0eb0b53f93d6..187bc27078f9 100644 --- a/src/google/protobuf/stubs/status.h +++ b/src/google/protobuf/stubs/status.h @@ -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; @@ -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 diff --git a/src/google/protobuf/stubs/status_test.cc b/src/google/protobuf/stubs/status_test.cc index ce486809db3b..cca6c2bc3dad 100644 --- a/src/google/protobuf/stubs/status_test.cc +++ b/src/google/protobuf/stubs/status_test.cc @@ -37,22 +37,170 @@ namespace google { namespace protobuf { namespace { -TEST(Status, Empty) { - util::Status status; - EXPECT_EQ(util::error::OK, util::Status::OK.code()); - EXPECT_EQ("OK", util::Status::OK.ToString()); -} -TEST(Status, GenericCodes) { - EXPECT_EQ(util::error::OK, util::Status::OK.code()); - EXPECT_EQ(util::error::CANCELLED, util::Status::CANCELLED.code()); - EXPECT_EQ(util::error::UNKNOWN, util::Status::UNKNOWN.code()); +TEST(Status, Constructor) { + EXPECT_EQ(util::StatusCode::kOk, + util::Status(util::StatusCode::kOk, "").code()); + EXPECT_EQ(util::StatusCode::kCancelled, + util::Status(util::StatusCode::kCancelled, "").code()); + EXPECT_EQ(util::StatusCode::kUnknown, + util::Status(util::StatusCode::kUnknown, "").code()); + EXPECT_EQ(util::StatusCode::kInvalidArgument, + util::Status(util::StatusCode::kInvalidArgument, "").code()); + EXPECT_EQ(util::StatusCode::kDeadlineExceeded, + util::Status(util::StatusCode::kDeadlineExceeded, "").code()); + EXPECT_EQ(util::StatusCode::kNotFound, + util::Status(util::StatusCode::kNotFound, "").code()); + EXPECT_EQ(util::StatusCode::kAlreadyExists, + util::Status(util::StatusCode::kAlreadyExists, "").code()); + EXPECT_EQ(util::StatusCode::kPermissionDenied, + util::Status(util::StatusCode::kPermissionDenied, "").code()); + EXPECT_EQ(util::StatusCode::kUnauthenticated, + util::Status(util::StatusCode::kUnauthenticated, "").code()); + EXPECT_EQ(util::StatusCode::kResourceExhausted, + util::Status(util::StatusCode::kResourceExhausted, "").code()); + EXPECT_EQ(util::StatusCode::kFailedPrecondition, + util::Status(util::StatusCode::kFailedPrecondition, "").code()); + EXPECT_EQ(util::StatusCode::kAborted, + util::Status(util::StatusCode::kAborted, "").code()); + EXPECT_EQ(util::StatusCode::kOutOfRange, + util::Status(util::StatusCode::kOutOfRange, "").code()); + EXPECT_EQ(util::StatusCode::kUnimplemented, + util::Status(util::StatusCode::kUnimplemented, "").code()); + EXPECT_EQ(util::StatusCode::kInternal, + util::Status(util::StatusCode::kInternal, "").code()); + EXPECT_EQ(util::StatusCode::kUnavailable, + util::Status(util::StatusCode::kUnavailable, "").code()); + EXPECT_EQ(util::StatusCode::kDataLoss, + util::Status(util::StatusCode::kDataLoss, "").code()); } TEST(Status, ConstructorZero) { - util::Status status(util::error::OK, "msg"); + util::Status status(util::StatusCode::kOk, "msg"); EXPECT_TRUE(status.ok()); EXPECT_EQ("OK", status.ToString()); + EXPECT_EQ(util::OkStatus(), status); +} + +TEST(Status, ConvenienceConstructors) { + EXPECT_EQ(util::StatusCode::kOk, util::OkStatus().code()); + EXPECT_EQ("", util::OkStatus().message()); + + EXPECT_EQ(util::StatusCode::kCancelled, util::CancelledError("").code()); + EXPECT_EQ("", util::CancelledError("").message()); + EXPECT_EQ("foo", util::CancelledError("foo").message()); + EXPECT_EQ("bar", util::CancelledError("bar").message()); + + EXPECT_EQ(util::StatusCode::kUnknown, util::UnknownError("").code()); + EXPECT_EQ("", util::UnknownError("").message()); + EXPECT_EQ("foo", util::UnknownError("foo").message()); + EXPECT_EQ("bar", util::UnknownError("bar").message()); + + EXPECT_EQ(util::StatusCode::kInvalidArgument, + util::InvalidArgumentError("").code()); + EXPECT_EQ("", util::InvalidArgumentError("").message()); + EXPECT_EQ("foo", util::InvalidArgumentError("foo").message()); + EXPECT_EQ("bar", util::InvalidArgumentError("bar").message()); + + EXPECT_EQ(util::StatusCode::kDeadlineExceeded, + util::DeadlineExceededError("").code()); + EXPECT_EQ("", util::DeadlineExceededError("").message()); + EXPECT_EQ("foo", util::DeadlineExceededError("foo").message()); + EXPECT_EQ("bar", util::DeadlineExceededError("bar").message()); + + EXPECT_EQ(util::StatusCode::kNotFound, util::NotFoundError("").code()); + EXPECT_EQ("", util::NotFoundError("").message()); + EXPECT_EQ("foo", util::NotFoundError("foo").message()); + EXPECT_EQ("bar", util::NotFoundError("bar").message()); + + EXPECT_EQ(util::StatusCode::kAlreadyExists, + util::AlreadyExistsError("").code()); + EXPECT_EQ("", util::AlreadyExistsError("").message()); + EXPECT_EQ("foo", util::AlreadyExistsError("foo").message()); + EXPECT_EQ("bar", util::AlreadyExistsError("bar").message()); + + EXPECT_EQ(util::StatusCode::kPermissionDenied, + util::PermissionDeniedError("").code()); + EXPECT_EQ("", util::PermissionDeniedError("").message()); + EXPECT_EQ("foo", util::PermissionDeniedError("foo").message()); + EXPECT_EQ("bar", util::PermissionDeniedError("bar").message()); + + EXPECT_EQ(util::StatusCode::kUnauthenticated, + util::UnauthenticatedError("").code()); + EXPECT_EQ("", util::UnauthenticatedError("").message()); + EXPECT_EQ("foo", util::UnauthenticatedError("foo").message()); + EXPECT_EQ("bar", util::UnauthenticatedError("bar").message()); + + EXPECT_EQ(util::StatusCode::kResourceExhausted, + util::ResourceExhaustedError("").code()); + EXPECT_EQ("", util::ResourceExhaustedError("").message()); + EXPECT_EQ("foo", util::ResourceExhaustedError("foo").message()); + EXPECT_EQ("bar", util::ResourceExhaustedError("bar").message()); + + EXPECT_EQ(util::StatusCode::kFailedPrecondition, + util::FailedPreconditionError("").code()); + EXPECT_EQ("", util::FailedPreconditionError("").message()); + EXPECT_EQ("foo", util::FailedPreconditionError("foo").message()); + EXPECT_EQ("bar", util::FailedPreconditionError("bar").message()); + + EXPECT_EQ(util::StatusCode::kAborted, util::AbortedError("").code()); + EXPECT_EQ("", util::AbortedError("").message()); + EXPECT_EQ("foo", util::AbortedError("foo").message()); + EXPECT_EQ("bar", util::AbortedError("bar").message()); + + EXPECT_EQ(util::StatusCode::kOutOfRange, util::OutOfRangeError("").code()); + EXPECT_EQ("", util::OutOfRangeError("").message()); + EXPECT_EQ("foo", util::OutOfRangeError("foo").message()); + EXPECT_EQ("bar", util::OutOfRangeError("bar").message()); + + EXPECT_EQ(util::StatusCode::kUnimplemented, + util::UnimplementedError("").code()); + EXPECT_EQ("", util::UnimplementedError("").message()); + EXPECT_EQ("foo", util::UnimplementedError("foo").message()); + EXPECT_EQ("bar", util::UnimplementedError("bar").message()); + + EXPECT_EQ(util::StatusCode::kInternal, util::InternalError("").code()); + EXPECT_EQ("", util::InternalError("").message()); + EXPECT_EQ("foo", util::InternalError("foo").message()); + EXPECT_EQ("bar", util::InternalError("bar").message()); + + EXPECT_EQ(util::StatusCode::kUnavailable, util::UnavailableError("").code()); + EXPECT_EQ("", util::UnavailableError("").message()); + EXPECT_EQ("foo", util::UnavailableError("foo").message()); + EXPECT_EQ("bar", util::UnavailableError("bar").message()); + + EXPECT_EQ(util::StatusCode::kDataLoss, util::DataLossError("").code()); + EXPECT_EQ("", util::DataLossError("").message()); + EXPECT_EQ("foo", util::DataLossError("foo").message()); + EXPECT_EQ("bar", util::DataLossError("bar").message()); +} + +TEST(Status, ConvenienceTests) { + EXPECT_TRUE(util::OkStatus().ok()); + EXPECT_TRUE(util::IsCancelled(util::CancelledError(""))); + EXPECT_TRUE(util::IsUnknown(util::UnknownError(""))); + EXPECT_TRUE(util::IsInvalidArgument(util::InvalidArgumentError(""))); + EXPECT_TRUE(util::IsDeadlineExceeded(util::DeadlineExceededError(""))); + EXPECT_TRUE(util::IsNotFound(util::NotFoundError(""))); + EXPECT_TRUE(util::IsAlreadyExists(util::AlreadyExistsError(""))); + EXPECT_TRUE(util::IsPermissionDenied(util::PermissionDeniedError(""))); + EXPECT_TRUE(util::IsUnauthenticated(util::UnauthenticatedError(""))); + EXPECT_TRUE(util::IsResourceExhausted(util::ResourceExhaustedError(""))); + EXPECT_TRUE(util::IsFailedPrecondition(util::FailedPreconditionError(""))); + EXPECT_TRUE(util::IsAborted(util::AbortedError(""))); + EXPECT_TRUE(util::IsOutOfRange(util::OutOfRangeError(""))); + EXPECT_TRUE(util::IsUnimplemented(util::UnimplementedError(""))); + EXPECT_TRUE(util::IsInternal(util::InternalError(""))); + EXPECT_TRUE(util::IsUnavailable(util::UnavailableError(""))); + EXPECT_TRUE(util::IsDataLoss(util::DataLossError(""))); +} + +TEST(Status, Empty) { + util::Status status; + EXPECT_TRUE(status.ok()); + EXPECT_EQ(util::OkStatus(), status); + EXPECT_EQ(util::StatusCode::kOk, status.code()); + EXPECT_EQ("OK", status.ToString()); } TEST(Status, CheckOK) { @@ -63,38 +211,35 @@ TEST(Status, CheckOK) { } TEST(Status, ErrorMessage) { - util::Status status(util::error::INVALID_ARGUMENT, ""); + util::Status status = util::InvalidArgumentError(""); EXPECT_FALSE(status.ok()); EXPECT_EQ("", status.message().ToString()); - EXPECT_EQ("", status.message().ToString()); EXPECT_EQ("INVALID_ARGUMENT", status.ToString()); - status = util::Status(util::error::INVALID_ARGUMENT, "msg"); + status = util::InvalidArgumentError("msg"); EXPECT_FALSE(status.ok()); EXPECT_EQ("msg", status.message().ToString()); - EXPECT_EQ("msg", status.message().ToString()); EXPECT_EQ("INVALID_ARGUMENT:msg", status.ToString()); - status = util::Status(util::error::OK, "msg"); + status = util::Status(util::StatusCode::kOk, "msg"); EXPECT_TRUE(status.ok()); EXPECT_EQ("", status.message().ToString()); - EXPECT_EQ("", status.message().ToString()); EXPECT_EQ("OK", status.ToString()); } TEST(Status, Copy) { - util::Status a(util::error::UNKNOWN, "message"); + util::Status a = util::UnknownError("message"); util::Status b(a); ASSERT_EQ(a.ToString(), b.ToString()); } TEST(Status, Assign) { - util::Status a(util::error::UNKNOWN, "message"); + util::Status a = util::UnknownError("message"); util::Status b; b = a; ASSERT_EQ(a.ToString(), b.ToString()); } TEST(Status, AssignEmpty) { - util::Status a(util::error::UNKNOWN, "message"); + util::Status a = util::UnknownError("message"); util::Status b; a = b; ASSERT_EQ(std::string("OK"), a.ToString()); @@ -103,32 +248,33 @@ TEST(Status, AssignEmpty) { } TEST(Status, EqualsOK) { - ASSERT_EQ(util::Status::OK, util::Status()); + ASSERT_EQ(util::OkStatus(), util::Status()); } TEST(Status, EqualsSame) { - const util::Status a = util::Status(util::error::CANCELLED, "message"); - const util::Status b = util::Status(util::error::CANCELLED, "message"); + const util::Status a = util::CancelledError("message"); + const util::Status b = util::CancelledError("message"); ASSERT_EQ(a, b); } TEST(Status, EqualsCopy) { - const util::Status a = util::Status(util::error::CANCELLED, "message"); + const util::Status a = util::CancelledError("message"); const util::Status b = a; ASSERT_EQ(a, b); } TEST(Status, EqualsDifferentCode) { - const util::Status a = util::Status(util::error::CANCELLED, "message"); - const util::Status b = util::Status(util::error::UNKNOWN, "message"); + const util::Status a = util::CancelledError("message"); + const util::Status b = util::UnknownError("message"); ASSERT_NE(a, b); } TEST(Status, EqualsDifferentMessage) { - const util::Status a = util::Status(util::error::CANCELLED, "message"); - const util::Status b = util::Status(util::error::CANCELLED, "another"); + const util::Status a = util::CancelledError("message"); + const util::Status b = util::CancelledError("another"); ASSERT_NE(a, b); } + } // namespace } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/stubs/statusor.h b/src/google/protobuf/stubs/statusor.h index c3748eacc14a..6d8aea590906 100644 --- a/src/google/protobuf/stubs/statusor.h +++ b/src/google/protobuf/stubs/statusor.h @@ -32,7 +32,7 @@ // object. StatusOr models the concept of an object that is either a // usable value, or an error Status explaining why such a value is // not present. To this end, StatusOr does not allow its Status -// value to be Status::kOk. Further, StatusOr does not allow the +// value to be OkStatus(). Further, StatusOr does not allow the // contained pointer to be nullptr. // // The primary use-case for StatusOr is as the return value of a @@ -72,8 +72,7 @@ // // StatusOr FooFactory::MakeNewFoo(int arg) { // if (arg <= 0) { -// return ::util::Status(::util::error::INVALID_ARGUMENT, -// "Arg must be positive"); +// return InvalidArgumentError("Arg must be positive"); // } else { // return new Foo(arg); // } @@ -100,7 +99,7 @@ class StatusOr { template friend class StatusOr; public: - // Construct a new StatusOr with Status::UNKNOWN status + // Construct a new StatusOr with UnknownError() status. StatusOr(); // Construct a new StatusOr with the given non-ok status. After calling @@ -110,8 +109,8 @@ class StatusOr { // value, so it is convenient and sensible to be able to do 'return // Status()' when the return type is StatusOr. // - // REQUIRES: status != Status::kOk. This requirement is DCHECKed. - // In optimized builds, passing Status::kOk here will have the effect + // REQUIRES: status != OkStatus(). This requirement is DCHECKed. + // In optimized builds, passing OkStatus() here will have the effect // of passing PosixErrorSpace::EINVAL as a fallback. StatusOr(const Status& status); // NOLINT @@ -143,7 +142,7 @@ class StatusOr { StatusOr& operator=(const StatusOr& other); // Returns a reference to our status. If this contains a T, then - // returns Status::kOk. + // returns OkStatus(). const Status& status() const; // Returns this->status().ok() @@ -190,14 +189,13 @@ struct StatusOrHelper::Specialize { template inline StatusOr::StatusOr() - : status_(util::Status::UNKNOWN) { + : status_(util::UnknownError("")) { } template inline StatusOr::StatusOr(const Status& status) { if (status.ok()) { - status_ = - Status(StatusCode::kInternal, "Status::kOk is not a valid argument."); + status_ = util::InternalError("OkStatus() is not a valid argument."); } else { status_ = status; } @@ -206,9 +204,9 @@ inline StatusOr::StatusOr(const Status& status) { template inline StatusOr::StatusOr(const T& value) { if (internal::StatusOrHelper::Specialize::IsValueNull(value)) { - status_ = Status(StatusCode::kInternal, "nullptr is not a valid argument."); + status_ = util::InternalError("nullptr is not a valid argument."); } else { - status_ = Status::OK; + status_ = util::OkStatus(); value_ = value; } } diff --git a/src/google/protobuf/stubs/statusor_test.cc b/src/google/protobuf/stubs/statusor_test.cc index 6e2a9e5545ed..683900c6c359 100644 --- a/src/google/protobuf/stubs/statusor_test.cc +++ b/src/google/protobuf/stubs/statusor_test.cc @@ -71,13 +71,13 @@ class CopyNoAssign { TEST(StatusOr, TestDefaultCtor) { StatusOr thing; EXPECT_FALSE(thing.ok()); - EXPECT_EQ(Status::UNKNOWN, thing.status()); + EXPECT_EQ(util::UnknownError(""), thing.status()); } TEST(StatusOr, TestStatusCtor) { - StatusOr thing(Status::CANCELLED); + StatusOr thing(util::CancelledError("")); EXPECT_FALSE(thing.ok()); - EXPECT_EQ(Status::CANCELLED, thing.status()); + EXPECT_EQ(util::CancelledError(""), thing.status()); } TEST(StatusOr, TestValueCtor) { @@ -96,7 +96,7 @@ TEST(StatusOr, TestCopyCtorStatusOk) { } TEST(StatusOr, TestCopyCtorStatusNotOk) { - StatusOr original(Status::CANCELLED); + StatusOr original(util::CancelledError("")); StatusOr copy(original); EXPECT_EQ(original.status(), copy.status()); } @@ -110,7 +110,7 @@ TEST(StatusOr, TestCopyCtorStatusOKConverting) { } TEST(StatusOr, TestCopyCtorStatusNotOkConverting) { - StatusOr original(Status::CANCELLED); + StatusOr original(util::CancelledError("")); StatusOr copy(original); EXPECT_EQ(original.status(), copy.status()); } @@ -125,7 +125,7 @@ TEST(StatusOr, TestAssignmentStatusOk) { } TEST(StatusOr, TestAssignmentStatusNotOk) { - StatusOr source(Status::CANCELLED); + StatusOr source(util::CancelledError("")); StatusOr target; target = source; EXPECT_EQ(source.status(), target.status()); @@ -141,7 +141,7 @@ TEST(StatusOr, TestAssignmentStatusOKConverting) { } TEST(StatusOr, TestAssignmentStatusNotOkConverting) { - StatusOr source(Status::CANCELLED); + StatusOr source(util::CancelledError("")); StatusOr target; target = source; EXPECT_EQ(source.status(), target.status()); @@ -150,9 +150,9 @@ TEST(StatusOr, TestAssignmentStatusNotOkConverting) { TEST(StatusOr, TestStatus) { StatusOr good(4); EXPECT_TRUE(good.ok()); - StatusOr bad(Status::CANCELLED); + StatusOr bad(util::CancelledError("")); EXPECT_FALSE(bad.ok()); - EXPECT_EQ(Status::CANCELLED, bad.status()); + EXPECT_EQ(util::CancelledError(""), bad.status()); } TEST(StatusOr, TestValue) { @@ -170,13 +170,13 @@ TEST(StatusOr, TestValueConst) { TEST(StatusOr, TestPointerDefaultCtor) { StatusOr thing; EXPECT_FALSE(thing.ok()); - EXPECT_EQ(Status::UNKNOWN, thing.status()); + EXPECT_EQ(util::UnknownError(""), thing.status()); } TEST(StatusOr, TestPointerStatusCtor) { - StatusOr thing(Status::CANCELLED); + StatusOr thing(util::CancelledError("")); EXPECT_FALSE(thing.ok()); - EXPECT_EQ(Status::CANCELLED, thing.status()); + EXPECT_EQ(util::CancelledError(""), thing.status()); } TEST(StatusOr, TestPointerValueCtor) { @@ -195,7 +195,7 @@ TEST(StatusOr, TestPointerCopyCtorStatusOk) { } TEST(StatusOr, TestPointerCopyCtorStatusNotOk) { - StatusOr original(Status::CANCELLED); + StatusOr original(util::CancelledError("")); StatusOr copy(original); EXPECT_EQ(original.status(), copy.status()); } @@ -210,7 +210,7 @@ TEST(StatusOr, TestPointerCopyCtorStatusOKConverting) { } TEST(StatusOr, TestPointerCopyCtorStatusNotOkConverting) { - StatusOr original(Status::CANCELLED); + StatusOr original(util::CancelledError("")); StatusOr copy(original); EXPECT_EQ(original.status(), copy.status()); } @@ -225,7 +225,7 @@ TEST(StatusOr, TestPointerAssignmentStatusOk) { } TEST(StatusOr, TestPointerAssignmentStatusNotOk) { - StatusOr source(Status::CANCELLED); + StatusOr source(util::CancelledError("")); StatusOr target; target = source; EXPECT_EQ(source.status(), target.status()); @@ -242,7 +242,7 @@ TEST(StatusOr, TestPointerAssignmentStatusOKConverting) { } TEST(StatusOr, TestPointerAssignmentStatusNotOkConverting) { - StatusOr source(Status::CANCELLED); + StatusOr source(util::CancelledError("")); StatusOr target; target = source; EXPECT_EQ(source.status(), target.status()); @@ -252,8 +252,8 @@ TEST(StatusOr, TestPointerStatus) { const int kI = 0; StatusOr good(&kI); EXPECT_TRUE(good.ok()); - StatusOr bad(Status::CANCELLED); - EXPECT_EQ(Status::CANCELLED, bad.status()); + StatusOr bad(util::CancelledError("")); + EXPECT_EQ(util::CancelledError(""), bad.status()); } TEST(StatusOr, TestPointerValue) { diff --git a/src/google/protobuf/util/internal/datapiece.cc b/src/google/protobuf/util/internal/datapiece.cc index feb4ab441f6e..4384b79c2438 100644 --- a/src/google/protobuf/util/internal/datapiece.cc +++ b/src/google/protobuf/util/internal/datapiece.cc @@ -51,17 +51,13 @@ using util::Status; namespace { -inline Status InvalidArgument(StringPiece value_str) { - return Status(util::error::INVALID_ARGUMENT, value_str); -} - template util::StatusOr ValidateNumberConversion(To after, From before) { if (after == before && MathUtil::Sign(before) == MathUtil::Sign(after)) { return after; } else { - return InvalidArgument(std::is_integral::value + return util::InvalidArgumentError(std::is_integral::value ? ValueAsString(before) : std::is_same::value ? DoubleAsString(before) @@ -106,7 +102,7 @@ util::StatusOr DoubleToFloat(double before) { } else if (before > std::numeric_limits::max() || before < -std::numeric_limits::max()) { // Double value outside of the range of float. - return InvalidArgument(DoubleAsString(before)); + return util::InvalidArgumentError(DoubleAsString(before)); } else { return static_cast(before); } @@ -178,7 +174,7 @@ util::StatusOr DataPiece::ToDouble() const { if (value.ok() && !std::isfinite(value.value())) { // safe_strtod converts out-of-range values to +inf/-inf, but we want // to report them as errors. - return InvalidArgument(StrCat("\"", str_, "\"")); + return util::InvalidArgumentError(StrCat("\"", str_, "\"")); } else { return value; } @@ -208,7 +204,7 @@ util::StatusOr DataPiece::ToBool() const { case TYPE_STRING: return StringToNumber(safe_strtob); default: - return InvalidArgument( + return util::InvalidArgumentError( ValueAsStringOrDefault("Wrong type. Cannot convert to Bool.")); } } @@ -223,7 +219,7 @@ util::StatusOr DataPiece::ToString() const { return base64; } default: - return InvalidArgument( + return util::InvalidArgumentError( ValueAsStringOrDefault("Cannot convert to string.")); } } @@ -264,11 +260,11 @@ util::StatusOr DataPiece::ToBytes() const { if (type_ == TYPE_STRING) { std::string decoded; if (!DecodeBase64(str_, &decoded)) { - return InvalidArgument(ValueAsStringOrDefault("Invalid data in input.")); + return util::InvalidArgumentError(ValueAsStringOrDefault("Invalid data in input.")); } return decoded; } else { - return InvalidArgument(ValueAsStringOrDefault( + return util::InvalidArgumentError(ValueAsStringOrDefault( "Wrong type. Only String or Bytes can be converted to Bytes.")); } } @@ -328,7 +324,7 @@ util::StatusOr DataPiece::ToEnum(const google::protobuf::Enum* enum_type, // enum because we preserve unknown enum values as well. return ToInt32(); } - return InvalidArgument( + return util::InvalidArgumentError( ValueAsStringOrDefault("Cannot find enum with given value.")); } @@ -348,7 +344,7 @@ util::StatusOr DataPiece::GenericConvert() const { case TYPE_FLOAT: return NumberConvertAndCheck(float_); default: // TYPE_ENUM, TYPE_STRING, TYPE_CORD, TYPE_BOOL - return InvalidArgument(ValueAsStringOrDefault( + return util::InvalidArgumentError(ValueAsStringOrDefault( "Wrong type. Bool, Enum, String and Cord not supported in " "GenericConvert.")); } @@ -358,11 +354,11 @@ template util::StatusOr DataPiece::StringToNumber(bool (*func)(StringPiece, To*)) const { if (str_.size() > 0 && (str_[0] == ' ' || str_[str_.size() - 1] == ' ')) { - return InvalidArgument(StrCat("\"", str_, "\"")); + return util::InvalidArgumentError(StrCat("\"", str_, "\"")); } To result; if (func(str_, &result)) return result; - return InvalidArgument(StrCat("\"", std::string(str_), "\"")); + return util::InvalidArgumentError(StrCat("\"", std::string(str_), "\"")); } bool DataPiece::DecodeBase64(StringPiece src, std::string* dest) const { diff --git a/src/google/protobuf/util/internal/field_mask_utility.cc b/src/google/protobuf/util/internal/field_mask_utility.cc index 0beff996a688..cd194e87d409 100644 --- a/src/google/protobuf/util/internal/field_mask_utility.cc +++ b/src/google/protobuf/util/internal/field_mask_utility.cc @@ -134,8 +134,7 @@ util::Status DecodeCompactFieldMaskPaths(StringPiece paths, } // Un-escaped '"' must be followed with a ']'. if (i >= length - 1 || paths[i + 1] != ']') { - return util::Status( - util::error::INVALID_ARGUMENT, + return util::InvalidArgumentError( StrCat( "Invalid FieldMask '", paths, "'. Map keys should be represented as [\"some_key\"].")); @@ -147,8 +146,7 @@ util::Status DecodeCompactFieldMaskPaths(StringPiece paths, // Checks whether the key ends at the end of a path segment. if (i < length - 1 && paths[i + 1] != '.' && paths[i + 1] != ',' && paths[i + 1] != ')' && paths[i + 1] != '(') { - return util::Status( - util::error::INVALID_ARGUMENT, + return util::InvalidArgumentError( StrCat( "Invalid FieldMask '", paths, "'. Map keys should be at the end of a path segment.")); @@ -160,8 +158,7 @@ util::Status DecodeCompactFieldMaskPaths(StringPiece paths, // We are not in a map key, look for the start of one. if (paths[i] == '[') { if (i >= length - 1 || paths[i + 1] != '\"') { - return util::Status( - util::error::INVALID_ARGUMENT, + return util::InvalidArgumentError( StrCat( "Invalid FieldMask '", paths, "'. Map keys should be represented as [\"some_key\"].")); @@ -197,8 +194,7 @@ util::Status DecodeCompactFieldMaskPaths(StringPiece paths, // Removes the last prefix after seeing a ')'. if (i < length && paths[i] == ')') { if (prefix.empty()) { - return util::Status( - util::error::INVALID_ARGUMENT, + return util::InvalidArgumentError( StrCat("Invalid FieldMask '", paths, "'. Cannot find matching '(' for all ')'.")); } @@ -207,14 +203,12 @@ util::Status DecodeCompactFieldMaskPaths(StringPiece paths, previous_position = i + 1; } if (in_map_key) { - return util::Status( - util::error::INVALID_ARGUMENT, + return util::InvalidArgumentError( StrCat("Invalid FieldMask '", paths, "'. Cannot find matching ']' for all '['.")); } if (!prefix.empty()) { - return util::Status( - util::error::INVALID_ARGUMENT, + return util::InvalidArgumentError( StrCat("Invalid FieldMask '", paths, "'. Cannot find matching ')' for all '('.")); } diff --git a/src/google/protobuf/util/internal/json_stream_parser.cc b/src/google/protobuf/util/internal/json_stream_parser.cc index dc0bacad034c..f38b68a1d2c4 100644 --- a/src/google/protobuf/util/internal/json_stream_parser.cc +++ b/src/google/protobuf/util/internal/json_stream_parser.cc @@ -302,14 +302,12 @@ util::Status JsonStreamParser::RunParser() { break; default: - result = util::Status(util::error::INTERNAL, - StrCat("Unknown parse type: ", type)); + result = util::InternalError(StrCat("Unknown parse type: ", type)); break; } if (!result.ok()) { // If we were cancelled, save our state and try again later. - if (!finishing_ && - result == util::Status(util::error::CANCELLED, "")) { + if (!finishing_ && util::IsCancelled(result)) { stack_.push(type); // If we have a key we still need to render, make sure to save off the // contents in our own storage. @@ -353,7 +351,7 @@ util::Status JsonStreamParser::ParseValue(TokenType type) { // don't know if the next char would be e, completing it, or something // else, making it invalid. if (!finishing_ && p_.length() < kKeywordFalse.length()) { - return util::Status(util::error::CANCELLED, ""); + return util::CancelledError(""); } return ReportFailure("Unexpected token.", ParseErrorType::UNEXPECTED_TOKEN); @@ -392,7 +390,7 @@ util::Status JsonStreamParser::ParseStringHelper() { // depending on if we expect more data later. if (p_.length() == 1) { if (!finishing_) { - return util::Status(util::error::CANCELLED, ""); + return util::CancelledError(""); } return ReportFailure("Closing quote expected in string.", ParseErrorType::EXPECTED_CLOSING_QUOTE); @@ -461,7 +459,7 @@ util::Status JsonStreamParser::ParseStringHelper() { } // If we didn't find the closing quote but we expect more data, cancel for now if (!finishing_) { - return util::Status(util::error::CANCELLED, ""); + return util::CancelledError(""); } // End of string reached without a closing quote, report an error. string_open_ = 0; @@ -479,7 +477,7 @@ util::Status JsonStreamParser::ParseStringHelper() { util::Status JsonStreamParser::ParseUnicodeEscape() { if (p_.length() < kUnicodeEscapedLength) { if (!finishing_) { - return util::Status(util::error::CANCELLED, ""); + return util::CancelledError(""); } return ReportFailure("Illegal hex string.", ParseErrorType::ILLEGAL_HEX_STRING); @@ -498,7 +496,7 @@ util::Status JsonStreamParser::ParseUnicodeEscape() { code <= JsonEscaping::kMaxHighSurrogate) { if (p_.length() < 2 * kUnicodeEscapedLength) { if (!finishing_) { - return util::Status(util::error::CANCELLED, ""); + return util::CancelledError(""); } if (!coerce_to_utf8_) { return ReportFailure("Missing low surrogate.", @@ -612,7 +610,7 @@ util::Status JsonStreamParser::ParseNumberHelper(NumberResult* result) { // If the entire input is a valid number, and we may have more content in the // future, we abort for now and resume when we know more. if (index == length && !finishing_) { - return util::Status(util::error::CANCELLED, ""); + return util::CancelledError(""); } // Create a string containing just the number, so we can use safe_strtoX @@ -799,7 +797,7 @@ util::Status JsonStreamParser::ParseArrayValue(TokenType type) { // empty-null array value is relying on this ARRAY_MID token. stack_.push(ARRAY_MID); util::Status result = ParseValue(type); - if (result == util::Status(util::error::CANCELLED, "")) { + if (util::IsCancelled(result)) { // If we were cancelled, pop back off the ARRAY_MID so we don't try to // push it on again when we try over. stack_.pop(); @@ -874,18 +872,15 @@ util::Status JsonStreamParser::ReportFailure(StringPiece message, StringPiece segment(begin, end - begin); std::string location(p_start - begin, ' '); location.push_back('^'); - auto status = - util::Status(util::error::INVALID_ARGUMENT, - StrCat(message, "\n", segment, "\n", location)); - - return status; + return util::InvalidArgumentError( + StrCat(message, "\n", segment, "\n", location)); } util::Status JsonStreamParser::ReportUnknown(StringPiece message, ParseErrorType parse_code) { // If we aren't finishing the parse, cancel parsing and try later. if (!finishing_) { - return util::Status(util::error::CANCELLED, ""); + return util::CancelledError(""); } if (p_.empty()) { return ReportFailure(StrCat("Unexpected end of string. ", message), @@ -897,8 +892,7 @@ util::Status JsonStreamParser::ReportUnknown(StringPiece message, util::Status JsonStreamParser::IncrementRecursionDepth( StringPiece key) const { if (++recursion_depth_ > max_recursion_depth_) { - return util::Status( - util::error::INVALID_ARGUMENT, + return util::InvalidArgumentError( StrCat("Message too deep. Max recursion depth reached for key '", key, "'")); } @@ -940,7 +934,7 @@ util::Status JsonStreamParser::ParseKey() { // we can't know if the key was complete or not. if (!finishing_ && p_.empty()) { p_ = original; - return util::Status(util::error::CANCELLED, ""); + return util::CancelledError(""); } // Since we aren't using the key storage, clear it out. key_storage_.clear(); diff --git a/src/google/protobuf/util/internal/json_stream_parser.h b/src/google/protobuf/util/internal/json_stream_parser.h index 4c93d77fc52a..47dfe82f4a65 100644 --- a/src/google/protobuf/util/internal/json_stream_parser.h +++ b/src/google/protobuf/util/internal/json_stream_parser.h @@ -244,7 +244,7 @@ class PROTOBUF_EXPORT JsonStreamParser { ParseErrorType parse_code); // Helper function to check recursion depth and increment it. It will return - // Status::OK if the current depth is allowed. Otherwise an error is returned. + // OkStatus() if the current depth is allowed. Otherwise an error is returned. // key is used for error reporting. util::Status IncrementRecursionDepth(StringPiece key) const; diff --git a/src/google/protobuf/util/internal/json_stream_parser_test.cc b/src/google/protobuf/util/internal/json_stream_parser_test.cc index 9d38a4f618be..4bb10251ec7c 100644 --- a/src/google/protobuf/util/internal/json_stream_parser_test.cc +++ b/src/google/protobuf/util/internal/json_stream_parser_test.cc @@ -138,7 +138,7 @@ class JsonStreamParserTest : public ::testing::Test { std::function setup = [](JsonStreamParser* p) { }) { util::Status result = RunTest(json, split, setup); - EXPECT_EQ(util::error::INVALID_ARGUMENT, result.code()); + EXPECT_TRUE(util::IsInvalidArgument(result)); StringPiece error_message(result.message()); EXPECT_EQ(error_prefix, error_message.substr(0, error_prefix.size())); } @@ -149,7 +149,7 @@ class JsonStreamParserTest : public ::testing::Test { std::function setup = [](JsonStreamParser* p) { }) { util::Status result = RunTest(json, split, setup); - EXPECT_EQ(util::error::INVALID_ARGUMENT, result.code()); + EXPECT_TRUE(util::IsInvalidArgument(result)); StringPiece error_message(result.message()); EXPECT_EQ(error_prefix, error_message.substr(0, error_prefix.size())); } diff --git a/src/google/protobuf/util/internal/proto_writer.cc b/src/google/protobuf/util/internal/proto_writer.cc index 8821dbef6b4c..ff4fe54aaab9 100644 --- a/src/google/protobuf/util/internal/proto_writer.cc +++ b/src/google/protobuf/util/internal/proto_writer.cc @@ -55,8 +55,6 @@ namespace converter { using io::CodedOutputStream; using ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite; -using util::error::INVALID_ARGUMENT; - ProtoWriter::ProtoWriter(TypeResolver* type_resolver, const google::protobuf::Type& type, @@ -695,8 +693,7 @@ ProtoWriter* ProtoWriter::RenderPrimitiveField( break; } default: // TYPE_GROUP, TYPE_MESSAGE, TYPE_UNKNOWN. - status = util::Status(util::error::INVALID_ARGUMENT, - data.ValueAsStringOrDefault("")); + status = util::InvalidArgumentError(data.ValueAsStringOrDefault("")); } if (!status.ok()) { diff --git a/src/google/protobuf/util/internal/protostream_objectsource.cc b/src/google/protobuf/util/internal/protostream_objectsource.cc index 8ad51df28beb..b98a14cdb0b7 100644 --- a/src/google/protobuf/util/internal/protostream_objectsource.cc +++ b/src/google/protobuf/util/internal/protostream_objectsource.cc @@ -102,7 +102,7 @@ util::StatusOr MapKeyDefaultValueAsString( case google::protobuf::Field::TYPE_STRING: return std::string(); default: - return util::Status(util::error::INTERNAL, "Invalid map key type."); + return util::InternalError("Invalid map key type."); } } } // namespace @@ -282,8 +282,7 @@ util::StatusOr ProtoStreamObjectSource::RenderMap( if (key_field == nullptr) { // The Type info for this map entry is incorrect. It should always // have a field named "key" and with field number 1. - return util::Status(util::error::INTERNAL, - "Invalid map entry."); + return util::InternalError("Invalid map entry."); } ASSIGN_OR_RETURN(map_key, MapKeyDefaultValueAsString(*key_field)); } @@ -291,7 +290,7 @@ util::StatusOr ProtoStreamObjectSource::RenderMap( } else { // The Type info for this map entry is incorrect. It should contain // exactly two fields with field number 1 and 2. - return util::Status(util::error::INTERNAL, "Invalid map entry."); + return util::InternalError("Invalid map entry."); } } stream_->PopLimit(old_limit); @@ -318,15 +317,12 @@ util::Status ProtoStreamObjectSource::RenderTimestamp( int64_t seconds = p.first; int32_t nanos = p.second; if (seconds > kTimestampMaxSeconds || seconds < kTimestampMinSeconds) { - return util::Status( - util::error::INTERNAL, - StrCat("Timestamp seconds exceeds limit for field: ", - field_name)); + return util::InternalError( + StrCat("Timestamp seconds exceeds limit for field: ", field_name)); } if (nanos < 0 || nanos >= kNanosPerSecond) { - return util::Status( - util::error::INTERNAL, + return util::InternalError( StrCat("Timestamp nanos exceeds limit for field: ", field_name)); } @@ -343,22 +339,19 @@ util::Status ProtoStreamObjectSource::RenderDuration( int64_t seconds = p.first; int32_t nanos = p.second; if (seconds > kDurationMaxSeconds || seconds < kDurationMinSeconds) { - return util::Status( - util::error::INTERNAL, + return util::InternalError( StrCat("Duration seconds exceeds limit for field: ", field_name)); } if (nanos <= -kNanosPerSecond || nanos >= kNanosPerSecond) { - return util::Status( - util::error::INTERNAL, + return util::InternalError( StrCat("Duration nanos exceeds limit for field: ", field_name)); } std::string sign = ""; if (seconds < 0) { if (nanos > 0) { - return util::Status( - util::error::INTERNAL, + return util::InternalError( StrCat("Duration nanos is non-negative, but seconds is " "negative for field: ", field_name)); @@ -611,8 +604,7 @@ util::Status ProtoStreamObjectSource::RenderAny( // If there is a value but no type, we cannot render it, so report an error. if (type_url.empty()) { // TODO(sven): Add an external message once those are ready. - return util::Status(util::error::INTERNAL, - "Invalid Any, the type_url is missing."); + return util::InternalError("Invalid Any, the type_url is missing."); } util::StatusOr resolved_type = @@ -621,8 +613,7 @@ util::Status ProtoStreamObjectSource::RenderAny( if (!resolved_type.ok()) { // Convert into an internal error, since this means the backend gave us // an invalid response (missing or invalid type information). - return util::Status(util::error::INTERNAL, - resolved_type.status().message()); + return util::InternalError(resolved_type.status().message()); } // nested_type cannot be null at this time. const google::protobuf::Type* nested_type = resolved_type.value(); @@ -659,8 +650,7 @@ util::Status ProtoStreamObjectSource::RenderFieldMask( } } if (paths_field_tag != tag) { - return util::Status(util::error::INTERNAL, - "Invalid FieldMask, unexpected field."); + return util::InternalError("Invalid FieldMask, unexpected field."); } std::string str; os->stream_->ReadVarint32(&buffer32); // string size. @@ -744,8 +734,7 @@ util::Status ProtoStreamObjectSource::RenderField( const google::protobuf::Type* type = typeinfo_->GetTypeByTypeUrl(field->type_url()); if (type == nullptr) { - return util::Status( - util::error::INTERNAL, + return util::InternalError( StrCat("Invalid configuration. Could not find the type: ", field->type_url())); } @@ -762,8 +751,7 @@ util::Status ProtoStreamObjectSource::RenderField( --recursion_depth_; if (!stream_->ConsumedEntireMessage()) { - return util::Status( - util::error::INVALID_ARGUMENT, + return util::InvalidArgumentError( "Nested protocol message not parsed in its entirety."); } stream_->PopLimit(old_limit); @@ -1062,8 +1050,7 @@ std::pair ProtoStreamObjectSource::ReadSecondsAndNanos( util::Status ProtoStreamObjectSource::IncrementRecursionDepth( StringPiece type_name, StringPiece field_name) const { if (++recursion_depth_ > max_recursion_depth_) { - return util::Status( - util::error::INVALID_ARGUMENT, + return util::InvalidArgumentError( StrCat("Message too deep. Max recursion depth reached for type '", type_name, "', field '", field_name, "'")); } diff --git a/src/google/protobuf/util/internal/protostream_objectsource.h b/src/google/protobuf/util/internal/protostream_objectsource.h index 441e4fd162db..60eaf4e1d8e3 100644 --- a/src/google/protobuf/util/internal/protostream_objectsource.h +++ b/src/google/protobuf/util/internal/protostream_objectsource.h @@ -287,7 +287,7 @@ class PROTOBUF_EXPORT ProtoStreamObjectSource : public ObjectSource { const google::protobuf::Type& type) const; // Helper function to check recursion depth and increment it. It will return - // Status::OK if the current depth is allowed. Otherwise an error is returned. + // OkStatus() if the current depth is allowed. Otherwise an error is returned. // type_name and field_name are used for error reporting. util::Status IncrementRecursionDepth(StringPiece type_name, StringPiece field_name) const; diff --git a/src/google/protobuf/util/internal/protostream_objectsource_test.cc b/src/google/protobuf/util/internal/protostream_objectsource_test.cc index 5bd5a91020ad..6ea8b29fd779 100644 --- a/src/google/protobuf/util/internal/protostream_objectsource_test.cc +++ b/src/google/protobuf/util/internal/protostream_objectsource_test.cc @@ -597,7 +597,7 @@ TEST_P(ProtostreamObjectSourceTest, CyclicMessageDepthTest) { } util::Status status = ExecuteTest(cyclic, Cyclic::descriptor()); - EXPECT_EQ(util::error::INVALID_ARGUMENT, status.code()); + EXPECT_TRUE(util::IsInvalidArgument(status)); } class ProtostreamObjectSourceMapsTest : public ProtostreamObjectSourceTest { @@ -943,7 +943,7 @@ TEST_P(ProtostreamObjectSourceAnysTest, MissingTypeUrlError) { ow_.StartObject(""); util::Status status = ExecuteTest(out, AnyOut::descriptor()); - EXPECT_EQ(util::error::INTERNAL, status.code()); + EXPECT_TRUE(util::IsInternal(status)); } TEST_P(ProtostreamObjectSourceAnysTest, UnknownTypeServiceError) { @@ -959,7 +959,7 @@ TEST_P(ProtostreamObjectSourceAnysTest, UnknownTypeServiceError) { ow_.StartObject(""); util::Status status = ExecuteTest(out, AnyOut::descriptor()); - EXPECT_EQ(util::error::INTERNAL, status.code()); + EXPECT_TRUE(util::IsInternal(status)); } TEST_P(ProtostreamObjectSourceAnysTest, UnknownTypeError) { @@ -975,7 +975,7 @@ TEST_P(ProtostreamObjectSourceAnysTest, UnknownTypeError) { ow_.StartObject(""); util::Status status = ExecuteTest(out, AnyOut::descriptor()); - EXPECT_EQ(util::error::INTERNAL, status.code()); + EXPECT_TRUE(util::IsInternal(status)); } class ProtostreamObjectSourceStructTest : public ProtostreamObjectSourceTest { @@ -1108,7 +1108,7 @@ TEST_P(ProtostreamObjectSourceTimestampTest, InvalidTimestampBelowMinTest) { ow_.StartObject(""); util::Status status = ExecuteTest(out, TimestampDuration::descriptor()); - EXPECT_EQ(util::error::INTERNAL, status.code()); + EXPECT_TRUE(util::IsInternal(status)); } TEST_P(ProtostreamObjectSourceTimestampTest, InvalidTimestampAboveMaxTest) { @@ -1119,7 +1119,7 @@ TEST_P(ProtostreamObjectSourceTimestampTest, InvalidTimestampAboveMaxTest) { ow_.StartObject(""); util::Status status = ExecuteTest(out, TimestampDuration::descriptor()); - EXPECT_EQ(util::error::INTERNAL, status.code()); + EXPECT_TRUE(util::IsInternal(status)); } TEST_P(ProtostreamObjectSourceTimestampTest, InvalidDurationBelowMinTest) { @@ -1130,7 +1130,7 @@ TEST_P(ProtostreamObjectSourceTimestampTest, InvalidDurationBelowMinTest) { ow_.StartObject(""); util::Status status = ExecuteTest(out, TimestampDuration::descriptor()); - EXPECT_EQ(util::error::INTERNAL, status.code()); + EXPECT_TRUE(util::IsInternal(status)); } TEST_P(ProtostreamObjectSourceTimestampTest, InvalidDurationAboveMaxTest) { @@ -1141,7 +1141,7 @@ TEST_P(ProtostreamObjectSourceTimestampTest, InvalidDurationAboveMaxTest) { ow_.StartObject(""); util::Status status = ExecuteTest(out, TimestampDuration::descriptor()); - EXPECT_EQ(util::error::INTERNAL, status.code()); + EXPECT_TRUE(util::IsInternal(status)); } TEST_P(ProtostreamObjectSourceTimestampTest, TimestampDurationDefaultValue) { diff --git a/src/google/protobuf/util/internal/protostream_objectwriter.cc b/src/google/protobuf/util/internal/protostream_objectwriter.cc index 56ca3caed7f3..772e6985d930 100644 --- a/src/google/protobuf/util/internal/protostream_objectwriter.cc +++ b/src/google/protobuf/util/internal/protostream_objectwriter.cc @@ -59,7 +59,6 @@ namespace converter { using ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite; using std::placeholders::_1; using util::Status; -using util::error::INVALID_ARGUMENT; ProtoStreamObjectWriter::ProtoStreamObjectWriter( @@ -144,14 +143,14 @@ Status GetNanosFromStringPiece(StringPiece s_nanos, // conversion to 'nanos', rather than a double, so that there is no // loss of precision. if (!s_nanos.empty() && !safe_strto32(s_nanos, &i_nanos)) { - return Status(util::error::INVALID_ARGUMENT, parse_failure_message); + return util::InvalidArgumentError(parse_failure_message); } if (i_nanos > kNanosPerSecond || i_nanos < 0) { - return Status(util::error::INVALID_ARGUMENT, exceeded_limit_message); + return util::InvalidArgumentError(exceeded_limit_message); } // s_nanos should only have digits. No whitespace. if (s_nanos.find_first_not_of("0123456789") != StringPiece::npos) { - return Status(util::error::INVALID_ARGUMENT, parse_failure_message); + return util::InvalidArgumentError(parse_failure_message); } if (i_nanos > 0) { @@ -191,8 +190,7 @@ Status GetNanosFromStringPiece(StringPiece s_nanos, conversion = 1; break; default: - return Status(util::error::INVALID_ARGUMENT, - exceeded_limit_message); + return util::InvalidArgumentError(exceeded_limit_message); } *nanos = i_nanos * conversion; } @@ -1013,9 +1011,9 @@ Status ProtoStreamObjectWriter::RenderStructValue(ProtoStreamObjectWriter* ow, break; } default: { - return Status(util::error::INVALID_ARGUMENT, - "Invalid struct data type. Only number, string, boolean or " - "null values are supported."); + return util::InvalidArgumentError( + "Invalid struct data type. Only number, string, boolean or null " + "values are supported."); } } ow->ProtoWriter::RenderDataPiece(struct_field_name, data); @@ -1026,9 +1024,10 @@ Status ProtoStreamObjectWriter::RenderTimestamp(ProtoStreamObjectWriter* ow, const DataPiece& data) { if (data.type() == DataPiece::TYPE_NULL) return Status(); if (data.type() != DataPiece::TYPE_STRING) { - return Status(util::error::INVALID_ARGUMENT, - StrCat("Invalid data type for timestamp, value is ", - data.ValueAsStringOrDefault(""))); + return util::InvalidArgumentError( + StrCat( + "Invalid data type for timestamp, value is ", + data.ValueAsStringOrDefault(""))); } StringPiece value(data.str()); @@ -1037,7 +1036,7 @@ Status ProtoStreamObjectWriter::RenderTimestamp(ProtoStreamObjectWriter* ow, int32 nanos; if (!::google::protobuf::internal::ParseTime(value.ToString(), &seconds, &nanos)) { - return Status(INVALID_ARGUMENT, StrCat("Invalid time format: ", value)); + return util::InvalidArgumentError(StrCat("Invalid time format: ", value)); } @@ -1057,9 +1056,10 @@ Status ProtoStreamObjectWriter::RenderFieldMask(ProtoStreamObjectWriter* ow, const DataPiece& data) { if (data.type() == DataPiece::TYPE_NULL) return Status(); if (data.type() != DataPiece::TYPE_STRING) { - return Status(util::error::INVALID_ARGUMENT, - StrCat("Invalid data type for field mask, value is ", - data.ValueAsStringOrDefault(""))); + return util::InvalidArgumentError( + StrCat( + "Invalid data type for field mask, value is ", + data.ValueAsStringOrDefault(""))); } // TODO(tsun): figure out how to do proto descriptor based snake case @@ -1073,16 +1073,17 @@ Status ProtoStreamObjectWriter::RenderDuration(ProtoStreamObjectWriter* ow, const DataPiece& data) { if (data.type() == DataPiece::TYPE_NULL) return Status(); if (data.type() != DataPiece::TYPE_STRING) { - return Status(util::error::INVALID_ARGUMENT, - StrCat("Invalid data type for duration, value is ", - data.ValueAsStringOrDefault(""))); + return util::InvalidArgumentError( + StrCat( + "Invalid data type for duration, value is ", + data.ValueAsStringOrDefault(""))); } StringPiece value(data.str()); if (!HasSuffixString(value, "s")) { - return Status(util::error::INVALID_ARGUMENT, - "Illegal duration format; duration must end with 's'"); + return util::InvalidArgumentError( + "Illegal duration format; duration must end with 's'"); } value = value.substr(0, value.size() - 1); int sign = 1; @@ -1095,8 +1096,8 @@ Status ProtoStreamObjectWriter::RenderDuration(ProtoStreamObjectWriter* ow, SplitSecondsAndNanos(value, &s_secs, &s_nanos); uint64_t unsigned_seconds; if (!safe_strtou64(s_secs, &unsigned_seconds)) { - return Status(util::error::INVALID_ARGUMENT, - "Invalid duration format, failed to parse seconds"); + return util::InvalidArgumentError( + "Invalid duration format, failed to parse seconds"); } int32_t nanos = 0; @@ -1111,8 +1112,7 @@ Status ProtoStreamObjectWriter::RenderDuration(ProtoStreamObjectWriter* ow, int64_t seconds = sign * unsigned_seconds; if (seconds > kDurationMaxSeconds || seconds < kDurationMinSeconds || nanos <= -kNanosPerSecond || nanos >= kNanosPerSecond) { - return Status(util::error::INVALID_ARGUMENT, - "Duration value exceeds limits"); + return util::InvalidArgumentError("Duration value exceeds limits"); } ow->ProtoWriter::RenderDataPiece("seconds", DataPiece(seconds)); diff --git a/src/google/protobuf/util/json_util.cc b/src/google/protobuf/util/json_util.cc index eb81b65e1ae7..2e7b78d039f4 100644 --- a/src/google/protobuf/util/json_util.cc +++ b/src/google/protobuf/util/json_util.cc @@ -140,25 +140,23 @@ class StatusErrorListener : public converter::ErrorListener { if (!loc_string.empty()) { loc_string.append(" "); } - status_ = - util::Status(util::error::INVALID_ARGUMENT, - StrCat(loc_string, unknown_name, ": ", message)); + status_ = util::InvalidArgumentError( + StrCat(loc_string, unknown_name, ": ", message)); } void InvalidValue(const converter::LocationTrackerInterface& loc, StringPiece type_name, StringPiece value) override { - status_ = util::Status( - util::error::INVALID_ARGUMENT, + status_ = util::InvalidArgumentError( StrCat(GetLocString(loc), ": invalid value ", std::string(value), " for type ", std::string(type_name))); } void MissingField(const converter::LocationTrackerInterface& loc, StringPiece missing_name) override { - status_ = util::Status(util::error::INVALID_ARGUMENT, - StrCat(GetLocString(loc), ": missing field ", - std::string(missing_name))); + status_ = util::InvalidArgumentError( + StrCat( + GetLocString(loc), ": missing field ", std::string(missing_name))); } private: @@ -271,8 +269,8 @@ util::Status JsonStringToMessage(StringPiece input, Message* message, util::Status result = JsonToBinaryString(resolver, GetTypeUrl(*message), input, &binary, options); if (result.ok() && !message->ParseFromString(binary)) { - result = util::Status(util::error::INVALID_ARGUMENT, - "JSON transcoder produced invalid protobuf output."); + result = util::InvalidArgumentError( + "JSON transcoder produced invalid protobuf output."); } if (pool != DescriptorPool::generated_pool()) { delete resolver; diff --git a/src/google/protobuf/util/json_util_test.cc b/src/google/protobuf/util/json_util_test.cc index 9851cc5d7f9f..3ffe73ef6ba6 100644 --- a/src/google/protobuf/util/json_util_test.cc +++ b/src/google/protobuf/util/json_util_test.cc @@ -632,8 +632,7 @@ TEST_F(JsonUtilTest, TestWrongJsonInput) { delete resolver; EXPECT_FALSE(result_status.ok()); - EXPECT_EQ(result_status.code(), - util::error::INVALID_ARGUMENT); + EXPECT_TRUE(util::IsInvalidArgument(result_status)); } TEST_F(JsonUtilTest, HtmlEscape) { diff --git a/src/google/protobuf/util/type_resolver_util.cc b/src/google/protobuf/util/type_resolver_util.cc index addb33499a3f..80fe3411d241 100644 --- a/src/google/protobuf/util/type_resolver_util.cc +++ b/src/google/protobuf/util/type_resolver_util.cc @@ -64,9 +64,6 @@ using google::protobuf::Type; using google::protobuf::UInt32Value; using google::protobuf::UInt64Value; -using util::error::INVALID_ARGUMENT; -using util::error::NOT_FOUND; - class DescriptorPoolTypeResolver : public TypeResolver { public: DescriptorPoolTypeResolver(const std::string& url_prefix, @@ -83,8 +80,8 @@ class DescriptorPoolTypeResolver : public TypeResolver { const Descriptor* descriptor = pool_->FindMessageTypeByName(type_name); if (descriptor == NULL) { - return util::Status(util::error::NOT_FOUND, - "Invalid type URL, unknown type: " + type_name); + return util::NotFoundError( + "Invalid type URL, unknown type: " + type_name); } ConvertDescriptor(descriptor, type); return util::Status(); @@ -100,8 +97,8 @@ class DescriptorPoolTypeResolver : public TypeResolver { const EnumDescriptor* descriptor = pool_->FindEnumTypeByName(type_name); if (descriptor == NULL) { - return util::Status(util::error::NOT_FOUND, - "Invalid type URL, unknown type: " + type_name); + return util::InvalidArgumentError( + "Invalid type URL, unknown type: " + type_name); } ConvertEnumDescriptor(descriptor, enum_type); return util::Status(); @@ -309,8 +306,7 @@ class DescriptorPoolTypeResolver : public TypeResolver { util::Status ParseTypeUrl(const std::string& type_url, std::string* type_name) { if (type_url.substr(0, url_prefix_.size() + 1) != url_prefix_ + "/") { - return util::Status( - util::error::INVALID_ARGUMENT, + return util::InvalidArgumentError( StrCat("Invalid type URL, type URLs must be of the form '", url_prefix_, "/', got: ", type_url)); }