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