1/*
2 *
3 * Copyright 2016 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#ifndef GRPCPP_IMPL_CODEGEN_STATUS_H
20#define GRPCPP_IMPL_CODEGEN_STATUS_H
21
22// IWYU pragma: private, include <grpcpp/support/status.h>
23
24#include <grpc/impl/codegen/port_platform.h>
25
26#include <grpc/impl/codegen/status.h>
27#include <grpcpp/impl/codegen/config.h>
28#include <grpcpp/impl/codegen/status_code_enum.h>
29
30namespace grpc {
31
32/// Did it work? If it didn't, why?
33///
34/// See \a grpc::StatusCode for details on the available code and their meaning.
35class GRPC_MUST_USE_RESULT_WHEN_USE_STRICT_WARNING Status {
36 public:
37 /// Construct an OK instance.
38 Status() : code_(StatusCode::OK) {
39 // Static assertions to make sure that the C++ API value correctly
40 // maps to the core surface API value
41 static_assert(StatusCode::OK == static_cast<StatusCode>(GRPC_STATUS_OK),
42 "Mismatched status code");
43 static_assert(
44 StatusCode::CANCELLED == static_cast<StatusCode>(GRPC_STATUS_CANCELLED),
45 "Mismatched status code");
46 static_assert(
47 StatusCode::UNKNOWN == static_cast<StatusCode>(GRPC_STATUS_UNKNOWN),
48 "Mismatched status code");
49 static_assert(StatusCode::INVALID_ARGUMENT ==
50 static_cast<StatusCode>(GRPC_STATUS_INVALID_ARGUMENT),
51 "Mismatched status code");
52 static_assert(StatusCode::DEADLINE_EXCEEDED ==
53 static_cast<StatusCode>(GRPC_STATUS_DEADLINE_EXCEEDED),
54 "Mismatched status code");
55 static_assert(
56 StatusCode::NOT_FOUND == static_cast<StatusCode>(GRPC_STATUS_NOT_FOUND),
57 "Mismatched status code");
58 static_assert(StatusCode::ALREADY_EXISTS ==
59 static_cast<StatusCode>(GRPC_STATUS_ALREADY_EXISTS),
60 "Mismatched status code");
61 static_assert(StatusCode::PERMISSION_DENIED ==
62 static_cast<StatusCode>(GRPC_STATUS_PERMISSION_DENIED),
63 "Mismatched status code");
64 static_assert(StatusCode::UNAUTHENTICATED ==
65 static_cast<StatusCode>(GRPC_STATUS_UNAUTHENTICATED),
66 "Mismatched status code");
67 static_assert(StatusCode::RESOURCE_EXHAUSTED ==
68 static_cast<StatusCode>(GRPC_STATUS_RESOURCE_EXHAUSTED),
69 "Mismatched status code");
70 static_assert(StatusCode::FAILED_PRECONDITION ==
71 static_cast<StatusCode>(GRPC_STATUS_FAILED_PRECONDITION),
72 "Mismatched status code");
73 static_assert(
74 StatusCode::ABORTED == static_cast<StatusCode>(GRPC_STATUS_ABORTED),
75 "Mismatched status code");
76 static_assert(StatusCode::OUT_OF_RANGE ==
77 static_cast<StatusCode>(GRPC_STATUS_OUT_OF_RANGE),
78 "Mismatched status code");
79 static_assert(StatusCode::UNIMPLEMENTED ==
80 static_cast<StatusCode>(GRPC_STATUS_UNIMPLEMENTED),
81 "Mismatched status code");
82 static_assert(
83 StatusCode::INTERNAL == static_cast<StatusCode>(GRPC_STATUS_INTERNAL),
84 "Mismatched status code");
85 static_assert(StatusCode::UNAVAILABLE ==
86 static_cast<StatusCode>(GRPC_STATUS_UNAVAILABLE),
87 "Mismatched status code");
88 static_assert(
89 StatusCode::DATA_LOSS == static_cast<StatusCode>(GRPC_STATUS_DATA_LOSS),
90 "Mismatched status code");
91 }
92
93 /// Construct an instance with associated \a code and \a error_message.
94 /// It is an error to construct an OK status with non-empty \a error_message.
95 /// Note that \a message is intentionally accepted as a const reference
96 /// instead of a value (which results in a copy instead of a move) to allow
97 /// for easy transition to absl::Status in the future which accepts an
98 /// absl::string_view as a parameter.
99 Status(StatusCode code, const std::string& error_message)
100 : code_(code), error_message_(error_message) {}
101
102 /// Construct an instance with \a code, \a error_message and
103 /// \a error_details. It is an error to construct an OK status with non-empty
104 /// \a error_message and/or \a error_details.
105 Status(StatusCode code, const std::string& error_message,
106 const std::string& error_details)
107 : code_(code),
108 error_message_(error_message),
109 binary_error_details_(error_details) {}
110
111 // Pre-defined special status objects.
112 /// An OK pre-defined instance.
113 static const Status& OK;
114 /// A CANCELLED pre-defined instance.
115 static const Status& CANCELLED;
116
117 /// Return the instance's error code.
118 StatusCode error_code() const { return code_; }
119 /// Return the instance's error message.
120 std::string error_message() const { return error_message_; }
121 /// Return the (binary) error details.
122 // Usually it contains a serialized google.rpc.Status proto.
123 std::string error_details() const { return binary_error_details_; }
124
125 /// Is the status OK?
126 bool ok() const { return code_ == StatusCode::OK; }
127
128 // Ignores any errors. This method does nothing except potentially suppress
129 // complaints from any tools that are checking that errors are not dropped on
130 // the floor.
131 void IgnoreError() const {}
132
133 private:
134 StatusCode code_;
135 std::string error_message_;
136 std::string binary_error_details_;
137};
138
139} // namespace grpc
140
141#endif // GRPCPP_IMPL_CODEGEN_STATUS_H
142

source code of include/grpcpp/impl/codegen/status.h