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 #ifndef GRPC_SRC_CORE_LIB_CHANNEL_STATUS_UTIL_H 20 #define GRPC_SRC_CORE_LIB_CHANNEL_STATUS_UTIL_H 21 22 #include <grpc/status.h> 23 #include <grpc/support/port_platform.h> 24 25 #include <string> 26 27 #include "absl/status/status.h" 28 #include "absl/strings/string_view.h" 29 30 /// If \a status_str is a valid status string, sets \a status to the 31 /// corresponding status value and returns true. 32 bool grpc_status_code_from_string(const char* status_str, 33 grpc_status_code* status); 34 35 /// Returns the string form of \a status, or "UNKNOWN" if invalid. 36 const char* grpc_status_code_to_string(grpc_status_code status); 37 38 // Converts an int to grpc_status_code. If the int is not a valid status code, 39 // sets the code to GRPC_STATUS_UNKNOWN and returns false. Otherwise, returns 40 // true. 41 bool grpc_status_code_from_int(int status_int, grpc_status_code* status); 42 43 namespace grpc_core { 44 namespace internal { 45 46 /// A set of grpc_status_code values. 47 class StatusCodeSet { 48 public: Empty()49 bool Empty() const { return status_code_mask_ == 0; } 50 Add(grpc_status_code status)51 StatusCodeSet& Add(grpc_status_code status) { 52 status_code_mask_ |= (1 << status); 53 return *this; 54 } 55 Contains(grpc_status_code status)56 bool Contains(grpc_status_code status) const { 57 return status_code_mask_ & (1 << status); 58 } 59 60 bool operator==(const StatusCodeSet& other) const { 61 return status_code_mask_ == other.status_code_mask_; 62 } 63 64 std::string ToString() const; 65 66 private: 67 int status_code_mask_ = 0; // A bitfield of status codes in the set. 68 }; 69 70 } // namespace internal 71 72 // Optionally rewrites a status as per 73 // https://github.com/grpc/proposal/blob/master/A54-restrict-control-plane-status-codes.md. 74 // The source parameter indicates where the status came from. 75 absl::Status MaybeRewriteIllegalStatusCode(absl::Status status, 76 absl::string_view source); 77 78 } // namespace grpc_core 79 80 #endif // GRPC_SRC_CORE_LIB_CHANNEL_STATUS_UTIL_H 81