error_details_test.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2017 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #include <gtest/gtest.h>
20 
22 
23 #include "src/proto/grpc/status/status.pb.h"
24 #include "src/proto/grpc/testing/echo_messages.pb.h"
26 
27 namespace grpc {
28 namespace {
29 
30 TEST(ExtractTest, Success) {
31  google::rpc::Status expected;
32  expected.set_code(13); // INTERNAL
33  expected.set_message("I am an error message");
34  testing::EchoRequest expected_details;
35  expected_details.set_message(std::string(100, '\0'));
36  expected.add_details()->PackFrom(expected_details);
37 
39  std::string error_details = expected.SerializeAsString();
40  Status from(static_cast<StatusCode>(expected.code()), expected.message(),
41  error_details);
43  EXPECT_EQ(expected.code(), to.code());
44  EXPECT_EQ(expected.message(), to.message());
45  EXPECT_EQ(1, to.details_size());
46  testing::EchoRequest details;
47  to.details(0).UnpackTo(&details);
48  EXPECT_EQ(expected_details.message(), details.message());
49 }
50 
51 TEST(ExtractTest, NullInput) {
53  ExtractErrorDetails(Status(), nullptr).error_code());
54 }
55 
56 TEST(ExtractTest, Unparsable) {
57  std::string error_details("I am not a status object");
58  Status from(StatusCode::INTERNAL, "", error_details);
61  ExtractErrorDetails(from, &to).error_code());
62 }
63 
64 TEST(SetTest, Success) {
65  google::rpc::Status expected;
66  expected.set_code(13); // INTERNAL
67  expected.set_message("I am an error message");
68  testing::EchoRequest expected_details;
69  expected_details.set_message(std::string(100, '\0'));
70  expected.add_details()->PackFrom(expected_details);
71 
72  Status to;
73  Status s = SetErrorDetails(expected, &to);
74  EXPECT_TRUE(s.ok());
75  EXPECT_EQ(expected.code(), to.error_code());
76  EXPECT_EQ(expected.message(), to.error_message());
77  EXPECT_EQ(expected.SerializeAsString(), to.error_details());
78 }
79 
80 TEST(SetTest, NullInput) {
82  SetErrorDetails(google::rpc::Status(), nullptr).error_code());
83 }
84 
85 TEST(SetTest, OutOfScopeErrorCode) {
86  google::rpc::Status expected;
87  expected.set_code(17); // Out of scope (UNAUTHENTICATED is 16).
88  expected.set_message("I am an error message");
89  testing::EchoRequest expected_details;
90  expected_details.set_message(std::string(100, '\0'));
91  expected.add_details()->PackFrom(expected_details);
92 
93  Status to;
94  Status s = SetErrorDetails(expected, &to);
95  EXPECT_TRUE(s.ok());
96  EXPECT_EQ(StatusCode::UNKNOWN, to.error_code());
97  EXPECT_EQ(expected.message(), to.error_message());
98  EXPECT_EQ(expected.SerializeAsString(), to.error_details());
99 }
100 
101 TEST(SetTest, ValidScopeErrorCode) {
102  for (int c = StatusCode::OK; c <= StatusCode::UNAUTHENTICATED; c++) {
103  google::rpc::Status expected;
104  expected.set_code(c);
105  expected.set_message("I am an error message");
106  testing::EchoRequest expected_details;
107  expected_details.set_message(std::string(100, '\0'));
108  expected.add_details()->PackFrom(expected_details);
109 
110  Status to;
111  Status s = SetErrorDetails(expected, &to);
112  EXPECT_TRUE(s.ok());
113  EXPECT_EQ(c, to.error_code());
114  EXPECT_EQ(expected.message(), to.error_message());
115  EXPECT_EQ(expected.SerializeAsString(), to.error_details());
116  }
117 }
118 
119 } // namespace
120 } // namespace grpc
121 
122 int main(int argc, char** argv) {
123  grpc::testing::TestEnvironment env(&argc, argv);
124  ::testing::InitGoogleTest(&argc, argv);
125  return RUN_ALL_TESTS();
126 }
generate.env
env
Definition: generate.py:37
grpc
Definition: grpcpp/alarm.h:33
fix_build_deps.c
list c
Definition: fix_build_deps.py:490
main
int main(int argc, char **argv)
Definition: error_details_test.cc:122
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
absl::FormatConversionChar::s
@ s
to
size_t to
Definition: abseil-cpp/absl/container/internal/layout_test.cc:1385
grpc::EXPECT_TRUE
EXPECT_TRUE(status.ok())
grpc.StatusCode.FAILED_PRECONDITION
tuple FAILED_PRECONDITION
Definition: src/python/grpcio/grpc/__init__.py:272
from
size_t from
Definition: abseil-cpp/absl/container/internal/layout_test.cc:1384
grpc.StatusCode.OK
tuple OK
Definition: src/python/grpcio/grpc/__init__.py:260
grpc.StatusCode.UNKNOWN
tuple UNKNOWN
Definition: src/python/grpcio/grpc/__init__.py:262
error_details.h
grpc.StatusCode.UNAUTHENTICATED
tuple UNAUTHENTICATED
Definition: src/python/grpcio/grpc/__init__.py:280
grpc::StatusCode
StatusCode
Definition: grpcpp/impl/codegen/status_code_enum.h:26
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
details
static grpc_slice details
Definition: test/core/fling/client.cc:46
test_config.h
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
grpc::TEST
TEST(CredentialsTest, StsCredentialsOptionsFromEnv)
Definition: cpp/client/credentials_test.cc:229
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
grpc::protobuf::util::Status
GRPC_CUSTOM_UTIL_STATUS Status
Definition: include/grpcpp/impl/codegen/config_protobuf.h:93
grpc.StatusCode.INVALID_ARGUMENT
tuple INVALID_ARGUMENT
Definition: src/python/grpcio/grpc/__init__.py:263
ok
bool ok
Definition: async_end2end_test.cc:197
grpc::SetErrorDetails
grpc::Status SetErrorDetails(const T &from, grpc::Status *to)
Definition: grpcpp/support/error_details.h:61
grpc::EXPECT_EQ
EXPECT_EQ(grpc::StatusCode::INVALID_ARGUMENT, status.error_code())
grpc::ExtractErrorDetails
grpc::Status ExtractErrorDetails(const grpc::Status &from, T *to)
Definition: grpcpp/support/error_details.h:38
grpc.StatusCode.INTERNAL
tuple INTERNAL
Definition: src/python/grpcio/grpc/__init__.py:277


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:19