Merge pull request #8405 from Yannic/status-del-error

Make util::Status more similar to absl::Status
This commit is contained in:
Adam Cozzette 2021-03-15 13:37:06 -07:00 committed by GitHub
commit 146d579738
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 511 additions and 212 deletions

View File

@ -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

View File

@ -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 {
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;
// 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::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

View File

@ -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

View File

@ -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<T> does not allow its Status
// value to be Status::kOk. Further, StatusOr<T*> does not allow the
// value to be OkStatus(). Further, StatusOr<T*> does not allow the
// contained pointer to be nullptr.
//
// The primary use-case for StatusOr<T> is as the return value of a
@ -72,8 +72,7 @@
//
// StatusOr<Foo*> 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<typename U> 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<T>.
//
// 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<U>& 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<T*> {
template<typename T>
inline StatusOr<T>::StatusOr()
: status_(util::Status::UNKNOWN) {
: status_(util::UnknownError("")) {
}
template<typename T>
inline StatusOr<T>::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<T>::StatusOr(const Status& status) {
template<typename T>
inline StatusOr<T>::StatusOr(const T& value) {
if (internal::StatusOrHelper::Specialize<T>::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;
}
}

View File

@ -71,13 +71,13 @@ class CopyNoAssign {
TEST(StatusOr, TestDefaultCtor) {
StatusOr<int> thing;
EXPECT_FALSE(thing.ok());
EXPECT_EQ(Status::UNKNOWN, thing.status());
EXPECT_EQ(util::UnknownError(""), thing.status());
}
TEST(StatusOr, TestStatusCtor) {
StatusOr<int> thing(Status::CANCELLED);
StatusOr<int> 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<int> original(Status::CANCELLED);
StatusOr<int> original(util::CancelledError(""));
StatusOr<int> copy(original);
EXPECT_EQ(original.status(), copy.status());
}
@ -110,7 +110,7 @@ TEST(StatusOr, TestCopyCtorStatusOKConverting) {
}
TEST(StatusOr, TestCopyCtorStatusNotOkConverting) {
StatusOr<int> original(Status::CANCELLED);
StatusOr<int> original(util::CancelledError(""));
StatusOr<double> copy(original);
EXPECT_EQ(original.status(), copy.status());
}
@ -125,7 +125,7 @@ TEST(StatusOr, TestAssignmentStatusOk) {
}
TEST(StatusOr, TestAssignmentStatusNotOk) {
StatusOr<int> source(Status::CANCELLED);
StatusOr<int> source(util::CancelledError(""));
StatusOr<int> target;
target = source;
EXPECT_EQ(source.status(), target.status());
@ -141,7 +141,7 @@ TEST(StatusOr, TestAssignmentStatusOKConverting) {
}
TEST(StatusOr, TestAssignmentStatusNotOkConverting) {
StatusOr<int> source(Status::CANCELLED);
StatusOr<int> source(util::CancelledError(""));
StatusOr<double> target;
target = source;
EXPECT_EQ(source.status(), target.status());
@ -150,9 +150,9 @@ TEST(StatusOr, TestAssignmentStatusNotOkConverting) {
TEST(StatusOr, TestStatus) {
StatusOr<int> good(4);
EXPECT_TRUE(good.ok());
StatusOr<int> bad(Status::CANCELLED);
StatusOr<int> 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<int*> thing;
EXPECT_FALSE(thing.ok());
EXPECT_EQ(Status::UNKNOWN, thing.status());
EXPECT_EQ(util::UnknownError(""), thing.status());
}
TEST(StatusOr, TestPointerStatusCtor) {
StatusOr<int*> thing(Status::CANCELLED);
StatusOr<int*> 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<int*> original(Status::CANCELLED);
StatusOr<int*> original(util::CancelledError(""));
StatusOr<int*> copy(original);
EXPECT_EQ(original.status(), copy.status());
}
@ -210,7 +210,7 @@ TEST(StatusOr, TestPointerCopyCtorStatusOKConverting) {
}
TEST(StatusOr, TestPointerCopyCtorStatusNotOkConverting) {
StatusOr<Derived*> original(Status::CANCELLED);
StatusOr<Derived*> original(util::CancelledError(""));
StatusOr<Base2*> copy(original);
EXPECT_EQ(original.status(), copy.status());
}
@ -225,7 +225,7 @@ TEST(StatusOr, TestPointerAssignmentStatusOk) {
}
TEST(StatusOr, TestPointerAssignmentStatusNotOk) {
StatusOr<int*> source(Status::CANCELLED);
StatusOr<int*> source(util::CancelledError(""));
StatusOr<int*> target;
target = source;
EXPECT_EQ(source.status(), target.status());
@ -242,7 +242,7 @@ TEST(StatusOr, TestPointerAssignmentStatusOKConverting) {
}
TEST(StatusOr, TestPointerAssignmentStatusNotOkConverting) {
StatusOr<Derived*> source(Status::CANCELLED);
StatusOr<Derived*> source(util::CancelledError(""));
StatusOr<Base2*> target;
target = source;
EXPECT_EQ(source.status(), target.status());
@ -252,8 +252,8 @@ TEST(StatusOr, TestPointerStatus) {
const int kI = 0;
StatusOr<const int*> good(&kI);
EXPECT_TRUE(good.ok());
StatusOr<const int*> bad(Status::CANCELLED);
EXPECT_EQ(Status::CANCELLED, bad.status());
StatusOr<const int*> bad(util::CancelledError(""));
EXPECT_EQ(util::CancelledError(""), bad.status());
}
TEST(StatusOr, TestPointerValue) {

View File

@ -51,17 +51,13 @@ using util::Status;
namespace {
inline Status InvalidArgument(StringPiece value_str) {
return Status(util::error::INVALID_ARGUMENT, value_str);
}
template <typename To, typename From>
util::StatusOr<To> ValidateNumberConversion(To after, From before) {
if (after == before &&
MathUtil::Sign<From>(before) == MathUtil::Sign<To>(after)) {
return after;
} else {
return InvalidArgument(std::is_integral<From>::value
return util::InvalidArgumentError(std::is_integral<From>::value
? ValueAsString(before)
: std::is_same<From, double>::value
? DoubleAsString(before)
@ -106,7 +102,7 @@ util::StatusOr<float> DoubleToFloat(double before) {
} else if (before > std::numeric_limits<float>::max() ||
before < -std::numeric_limits<float>::max()) {
// Double value outside of the range of float.
return InvalidArgument(DoubleAsString(before));
return util::InvalidArgumentError(DoubleAsString(before));
} else {
return static_cast<float>(before);
}
@ -178,7 +174,7 @@ util::StatusOr<double> 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<bool> DataPiece::ToBool() const {
case TYPE_STRING:
return StringToNumber<bool>(safe_strtob);
default:
return InvalidArgument(
return util::InvalidArgumentError(
ValueAsStringOrDefault("Wrong type. Cannot convert to Bool."));
}
}
@ -223,7 +219,7 @@ util::StatusOr<std::string> DataPiece::ToString() const {
return base64;
}
default:
return InvalidArgument(
return util::InvalidArgumentError(
ValueAsStringOrDefault("Cannot convert to string."));
}
}
@ -264,11 +260,11 @@ util::StatusOr<std::string> 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<int> 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<To> DataPiece::GenericConvert() const {
case TYPE_FLOAT:
return NumberConvertAndCheck<To, float>(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 <typename To>
util::StatusOr<To> 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 {

View File

@ -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 '('."));
}

View File

@ -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();

View File

@ -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;

View File

@ -138,7 +138,7 @@ class JsonStreamParserTest : public ::testing::Test {
std::function<void(JsonStreamParser*)> 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<void(JsonStreamParser*)> 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()));
}

View File

@ -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()) {

View File

@ -102,7 +102,7 @@ util::StatusOr<std::string> 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<uint32_t> 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<uint32_t> 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<const google::protobuf::Type*> 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<int64_t, int32_t> 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, "'"));
}

View File

@ -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;

View File

@ -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) {

View File

@ -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));

View File

@ -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;

View File

@ -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) {

View File

@ -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_, "/<typename>', got: ", type_url));
}