status.h 5.4 KB

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