Merge pull request #13141 from yang-g/error_off_by_one

Catch out of order error code
pull/13067/head
Yang Gao 7 years ago committed by GitHub
commit 7fd9623612
  1. 3
      src/cpp/util/error_details.cc
  2. 20
      test/cpp/util/error_details_test.cc

@ -37,7 +37,8 @@ Status SetErrorDetails(const ::google::rpc::Status& from, Status* to) {
return Status(StatusCode::FAILED_PRECONDITION, ""); return Status(StatusCode::FAILED_PRECONDITION, "");
} }
StatusCode code = StatusCode::UNKNOWN; StatusCode code = StatusCode::UNKNOWN;
if (from.code() >= StatusCode::OK && from.code() <= StatusCode::DATA_LOSS) { if (from.code() >= StatusCode::OK &&
from.code() <= StatusCode::UNAUTHENTICATED) {
code = static_cast<StatusCode>(from.code()); code = static_cast<StatusCode>(from.code());
} }
*to = Status(code, from.message(), from.SerializeAsString()); *to = Status(code, from.message(), from.SerializeAsString());

@ -82,7 +82,7 @@ TEST(SetTest, NullInput) {
TEST(SetTest, OutOfScopeErrorCode) { TEST(SetTest, OutOfScopeErrorCode) {
google::rpc::Status expected; google::rpc::Status expected;
expected.set_code(20); // Out of scope (DATA_LOSS is 15). expected.set_code(17); // Out of scope (UNAUTHENTICATED is 16).
expected.set_message("I am an error message"); expected.set_message("I am an error message");
testing::EchoRequest expected_details; testing::EchoRequest expected_details;
expected_details.set_message(grpc::string(100, '\0')); expected_details.set_message(grpc::string(100, '\0'));
@ -96,6 +96,24 @@ TEST(SetTest, OutOfScopeErrorCode) {
EXPECT_EQ(expected.SerializeAsString(), to.error_details()); EXPECT_EQ(expected.SerializeAsString(), to.error_details());
} }
TEST(SetTest, ValidScopeErrorCode) {
for (int c = StatusCode::OK; c <= StatusCode::UNAUTHENTICATED; c++) {
google::rpc::Status expected;
expected.set_code(c);
expected.set_message("I am an error message");
testing::EchoRequest expected_details;
expected_details.set_message(grpc::string(100, '\0'));
expected.add_details()->PackFrom(expected_details);
Status to;
Status s = SetErrorDetails(expected, &to);
EXPECT_TRUE(s.ok());
EXPECT_EQ(c, to.error_code());
EXPECT_EQ(expected.message(), to.error_message());
EXPECT_EQ(expected.SerializeAsString(), to.error_details());
}
}
} // namespace } // namespace
} // namespace grpc } // namespace grpc

Loading…
Cancel
Save