status.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #ifndef GOOGLE_PROTOBUF_STUBS_STATUS_H_
  31. #define GOOGLE_PROTOBUF_STUBS_STATUS_H_
  32. #include <string>
  33. #include <google/protobuf/stubs/stringpiece.h>
  34. #include <google/protobuf/port_def.inc>
  35. namespace google {
  36. namespace protobuf {
  37. namespace util {
  38. namespace status_internal {
  39. // These values must match error codes defined in google/rpc/code.proto.
  40. enum class StatusCode : int {
  41. kOk = 0,
  42. kCancelled = 1,
  43. kUnknown = 2,
  44. kInvalidArgument = 3,
  45. kDeadlineExceeded = 4,
  46. kNotFound = 5,
  47. kAlreadyExists = 6,
  48. kPermissionDenied = 7,
  49. kUnauthenticated = 16,
  50. kResourceExhausted = 8,
  51. kFailedPrecondition = 9,
  52. kAborted = 10,
  53. kOutOfRange = 11,
  54. kUnimplemented = 12,
  55. kInternal = 13,
  56. kUnavailable = 14,
  57. kDataLoss = 15,
  58. };
  59. class PROTOBUF_EXPORT Status {
  60. public:
  61. // Creates a "successful" status.
  62. Status();
  63. // Create a status in the canonical error space with the specified
  64. // code, and error message. If "code == 0", error_message is
  65. // ignored and a Status object identical to Status::kOk is
  66. // constructed.
  67. Status(StatusCode error_code, StringPiece error_message);
  68. Status(const Status&);
  69. Status& operator=(const Status& x);
  70. ~Status() {}
  71. // Accessor
  72. bool ok() const { return error_code_ == StatusCode::kOk; }
  73. StatusCode code() const { return error_code_; }
  74. StringPiece message() const {
  75. return error_message_;
  76. }
  77. bool operator==(const Status& x) const;
  78. bool operator!=(const Status& x) const {
  79. return !operator==(x);
  80. }
  81. // Return a combination of the error code name and message.
  82. std::string ToString() const;
  83. private:
  84. StatusCode error_code_;
  85. std::string error_message_;
  86. };
  87. // Returns an OK status, equivalent to a default constructed instance. Prefer
  88. // usage of `OkStatus()` when constructing such an OK status.
  89. PROTOBUF_EXPORT Status OkStatus();
  90. // Prints a human-readable representation of 'x' to 'os'.
  91. PROTOBUF_EXPORT std::ostream& operator<<(std::ostream& os, const Status& x);
  92. // These convenience functions return `true` if a given status matches the
  93. // `StatusCode` error code of its associated function.
  94. PROTOBUF_EXPORT bool IsAborted(const Status& status);
  95. PROTOBUF_EXPORT bool IsAlreadyExists(const Status& status);
  96. PROTOBUF_EXPORT bool IsCancelled(const Status& status);
  97. PROTOBUF_EXPORT bool IsDataLoss(const Status& status);
  98. PROTOBUF_EXPORT bool IsDeadlineExceeded(const Status& status);
  99. PROTOBUF_EXPORT bool IsFailedPrecondition(const Status& status);
  100. PROTOBUF_EXPORT bool IsInternal(const Status& status);
  101. PROTOBUF_EXPORT bool IsInvalidArgument(const Status& status);
  102. PROTOBUF_EXPORT bool IsNotFound(const Status& status);
  103. PROTOBUF_EXPORT bool IsOutOfRange(const Status& status);
  104. PROTOBUF_EXPORT bool IsPermissionDenied(const Status& status);
  105. PROTOBUF_EXPORT bool IsResourceExhausted(const Status& status);
  106. PROTOBUF_EXPORT bool IsUnauthenticated(const Status& status);
  107. PROTOBUF_EXPORT bool IsUnavailable(const Status& status);
  108. PROTOBUF_EXPORT bool IsUnimplemented(const Status& status);
  109. PROTOBUF_EXPORT bool IsUnknown(const Status& status);
  110. // These convenience functions create an `Status` object with an error code as
  111. // indicated by the associated function name, using the error message passed in
  112. // `message`.
  113. //
  114. // These functions are intentionally named `*Error` rather than `*Status` to
  115. // match the names from Abseil:
  116. // https://github.com/abseil/abseil-cpp/blob/2e9532cc6c701a8323d0cffb468999ab804095ab/absl/status/status.h#L716
  117. PROTOBUF_EXPORT Status AbortedError(StringPiece message);
  118. PROTOBUF_EXPORT Status AlreadyExistsError(StringPiece message);
  119. PROTOBUF_EXPORT Status CancelledError(StringPiece message);
  120. PROTOBUF_EXPORT Status DataLossError(StringPiece message);
  121. PROTOBUF_EXPORT Status DeadlineExceededError(StringPiece message);
  122. PROTOBUF_EXPORT Status FailedPreconditionError(StringPiece message);
  123. PROTOBUF_EXPORT Status InternalError(StringPiece message);
  124. PROTOBUF_EXPORT Status InvalidArgumentError(StringPiece message);
  125. PROTOBUF_EXPORT Status NotFoundError(StringPiece message);
  126. PROTOBUF_EXPORT Status OutOfRangeError(StringPiece message);
  127. PROTOBUF_EXPORT Status PermissionDeniedError(StringPiece message);
  128. PROTOBUF_EXPORT Status ResourceExhaustedError(StringPiece message);
  129. PROTOBUF_EXPORT Status UnauthenticatedError(StringPiece message);
  130. PROTOBUF_EXPORT Status UnavailableError(StringPiece message);
  131. PROTOBUF_EXPORT Status UnimplementedError(StringPiece message);
  132. PROTOBUF_EXPORT Status UnknownError(StringPiece message);
  133. } // namespace status_internal
  134. using ::google::protobuf::util::status_internal::Status;
  135. using ::google::protobuf::util::status_internal::StatusCode;
  136. using ::google::protobuf::util::status_internal::IsAborted;
  137. using ::google::protobuf::util::status_internal::IsAlreadyExists;
  138. using ::google::protobuf::util::status_internal::IsCancelled;
  139. using ::google::protobuf::util::status_internal::IsDataLoss;
  140. using ::google::protobuf::util::status_internal::IsDeadlineExceeded;
  141. using ::google::protobuf::util::status_internal::IsFailedPrecondition;
  142. using ::google::protobuf::util::status_internal::IsInternal;
  143. using ::google::protobuf::util::status_internal::IsInvalidArgument;
  144. using ::google::protobuf::util::status_internal::IsNotFound;
  145. using ::google::protobuf::util::status_internal::IsOutOfRange;
  146. using ::google::protobuf::util::status_internal::IsPermissionDenied;
  147. using ::google::protobuf::util::status_internal::IsResourceExhausted;
  148. using ::google::protobuf::util::status_internal::IsUnauthenticated;
  149. using ::google::protobuf::util::status_internal::IsUnavailable;
  150. using ::google::protobuf::util::status_internal::IsUnimplemented;
  151. using ::google::protobuf::util::status_internal::IsUnknown;
  152. using ::google::protobuf::util::status_internal::AbortedError;
  153. using ::google::protobuf::util::status_internal::AlreadyExistsError;
  154. using ::google::protobuf::util::status_internal::CancelledError;
  155. using ::google::protobuf::util::status_internal::DataLossError;
  156. using ::google::protobuf::util::status_internal::DeadlineExceededError;
  157. using ::google::protobuf::util::status_internal::FailedPreconditionError;
  158. using ::google::protobuf::util::status_internal::InternalError;
  159. using ::google::protobuf::util::status_internal::InvalidArgumentError;
  160. using ::google::protobuf::util::status_internal::NotFoundError;
  161. using ::google::protobuf::util::status_internal::OkStatus;
  162. using ::google::protobuf::util::status_internal::OutOfRangeError;
  163. using ::google::protobuf::util::status_internal::PermissionDeniedError;
  164. using ::google::protobuf::util::status_internal::ResourceExhaustedError;
  165. using ::google::protobuf::util::status_internal::UnauthenticatedError;
  166. using ::google::protobuf::util::status_internal::UnavailableError;
  167. using ::google::protobuf::util::status_internal::UnimplementedError;
  168. using ::google::protobuf::util::status_internal::UnknownError;
  169. } // namespace util
  170. } // namespace protobuf
  171. } // namespace google
  172. #include <google/protobuf/port_undef.inc>
  173. #endif // GOOGLE_PROTOBUF_STUBS_STATUS_H_