status.h 5.7 KB

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