You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

status.h 6.2 kB

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