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 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. {
  37. namespace protobuf
  38. {
  39. namespace util
  40. {
  41. namespace status_internal
  42. {
  43. // These values must match error codes defined in google/rpc/code.proto.
  44. enum class StatusCode : int
  45. {
  46. kOk = 0,
  47. kCancelled = 1,
  48. kUnknown = 2,
  49. kInvalidArgument = 3,
  50. kDeadlineExceeded = 4,
  51. kNotFound = 5,
  52. kAlreadyExists = 6,
  53. kPermissionDenied = 7,
  54. kUnauthenticated = 16,
  55. kResourceExhausted = 8,
  56. kFailedPrecondition = 9,
  57. kAborted = 10,
  58. kOutOfRange = 11,
  59. kUnimplemented = 12,
  60. kInternal = 13,
  61. kUnavailable = 14,
  62. kDataLoss = 15,
  63. };
  64. class PROTOBUF_EXPORT Status
  65. {
  66. public:
  67. // Creates a "successful" status.
  68. Status();
  69. // Create a status in the canonical error space with the specified
  70. // code, and error message. If "code == 0", error_message is
  71. // ignored and a Status object identical to Status::kOk is
  72. // constructed.
  73. Status(StatusCode error_code, StringPiece error_message);
  74. Status(const Status&);
  75. Status& operator=(const Status& x);
  76. ~Status()
  77. {
  78. }
  79. // Accessor
  80. bool ok() const
  81. {
  82. return error_code_ == StatusCode::kOk;
  83. }
  84. StatusCode code() const
  85. {
  86. return error_code_;
  87. }
  88. StringPiece message() const
  89. {
  90. return error_message_;
  91. }
  92. bool operator==(const Status& x) const;
  93. bool operator!=(const Status& x) const
  94. {
  95. return !operator==(x);
  96. }
  97. // Return a combination of the error code name and message.
  98. std::string ToString() const;
  99. private:
  100. StatusCode error_code_;
  101. std::string error_message_;
  102. };
  103. // Returns an OK status, equivalent to a default constructed instance. Prefer
  104. // usage of `OkStatus()` when constructing such an OK status.
  105. PROTOBUF_EXPORT Status OkStatus();
  106. // Prints a human-readable representation of 'x' to 'os'.
  107. PROTOBUF_EXPORT std::ostream& operator<<(std::ostream& os, const Status& x);
  108. // These convenience functions return `true` if a given status matches the
  109. // `StatusCode` error code of its associated function.
  110. PROTOBUF_EXPORT bool IsAborted(const Status& status);
  111. PROTOBUF_EXPORT bool IsAlreadyExists(const Status& status);
  112. PROTOBUF_EXPORT bool IsCancelled(const Status& status);
  113. PROTOBUF_EXPORT bool IsDataLoss(const Status& status);
  114. PROTOBUF_EXPORT bool IsDeadlineExceeded(const Status& status);
  115. PROTOBUF_EXPORT bool IsFailedPrecondition(const Status& status);
  116. PROTOBUF_EXPORT bool IsInternal(const Status& status);
  117. PROTOBUF_EXPORT bool IsInvalidArgument(const Status& status);
  118. PROTOBUF_EXPORT bool IsNotFound(const Status& status);
  119. PROTOBUF_EXPORT bool IsOutOfRange(const Status& status);
  120. PROTOBUF_EXPORT bool IsPermissionDenied(const Status& status);
  121. PROTOBUF_EXPORT bool IsResourceExhausted(const Status& status);
  122. PROTOBUF_EXPORT bool IsUnauthenticated(const Status& status);
  123. PROTOBUF_EXPORT bool IsUnavailable(const Status& status);
  124. PROTOBUF_EXPORT bool IsUnimplemented(const Status& status);
  125. PROTOBUF_EXPORT bool IsUnknown(const Status& status);
  126. // These convenience functions create an `Status` object with an error code as
  127. // indicated by the associated function name, using the error message passed in
  128. // `message`.
  129. //
  130. // These functions are intentionally named `*Error` rather than `*Status` to
  131. // match the names from Abseil:
  132. // https://github.com/abseil/abseil-cpp/blob/2e9532cc6c701a8323d0cffb468999ab804095ab/absl/status/status.h#L716
  133. PROTOBUF_EXPORT Status AbortedError(StringPiece message);
  134. PROTOBUF_EXPORT Status AlreadyExistsError(StringPiece message);
  135. PROTOBUF_EXPORT Status CancelledError(StringPiece message);
  136. PROTOBUF_EXPORT Status DataLossError(StringPiece message);
  137. PROTOBUF_EXPORT Status DeadlineExceededError(StringPiece message);
  138. PROTOBUF_EXPORT Status FailedPreconditionError(StringPiece message);
  139. PROTOBUF_EXPORT Status InternalError(StringPiece message);
  140. PROTOBUF_EXPORT Status InvalidArgumentError(StringPiece message);
  141. PROTOBUF_EXPORT Status NotFoundError(StringPiece message);
  142. PROTOBUF_EXPORT Status OutOfRangeError(StringPiece message);
  143. PROTOBUF_EXPORT Status PermissionDeniedError(StringPiece message);
  144. PROTOBUF_EXPORT Status ResourceExhaustedError(StringPiece message);
  145. PROTOBUF_EXPORT Status UnauthenticatedError(StringPiece message);
  146. PROTOBUF_EXPORT Status UnavailableError(StringPiece message);
  147. PROTOBUF_EXPORT Status UnimplementedError(StringPiece message);
  148. PROTOBUF_EXPORT Status UnknownError(StringPiece message);
  149. } // namespace status_internal
  150. using ::google::protobuf::util::status_internal::Status;
  151. using ::google::protobuf::util::status_internal::StatusCode;
  152. using ::google::protobuf::util::status_internal::IsAborted;
  153. using ::google::protobuf::util::status_internal::IsAlreadyExists;
  154. using ::google::protobuf::util::status_internal::IsCancelled;
  155. using ::google::protobuf::util::status_internal::IsDataLoss;
  156. using ::google::protobuf::util::status_internal::IsDeadlineExceeded;
  157. using ::google::protobuf::util::status_internal::IsFailedPrecondition;
  158. using ::google::protobuf::util::status_internal::IsInternal;
  159. using ::google::protobuf::util::status_internal::IsInvalidArgument;
  160. using ::google::protobuf::util::status_internal::IsNotFound;
  161. using ::google::protobuf::util::status_internal::IsOutOfRange;
  162. using ::google::protobuf::util::status_internal::IsPermissionDenied;
  163. using ::google::protobuf::util::status_internal::IsResourceExhausted;
  164. using ::google::protobuf::util::status_internal::IsUnauthenticated;
  165. using ::google::protobuf::util::status_internal::IsUnavailable;
  166. using ::google::protobuf::util::status_internal::IsUnimplemented;
  167. using ::google::protobuf::util::status_internal::IsUnknown;
  168. using ::google::protobuf::util::status_internal::AbortedError;
  169. using ::google::protobuf::util::status_internal::AlreadyExistsError;
  170. using ::google::protobuf::util::status_internal::CancelledError;
  171. using ::google::protobuf::util::status_internal::DataLossError;
  172. using ::google::protobuf::util::status_internal::DeadlineExceededError;
  173. using ::google::protobuf::util::status_internal::FailedPreconditionError;
  174. using ::google::protobuf::util::status_internal::InternalError;
  175. using ::google::protobuf::util::status_internal::InvalidArgumentError;
  176. using ::google::protobuf::util::status_internal::NotFoundError;
  177. using ::google::protobuf::util::status_internal::OkStatus;
  178. using ::google::protobuf::util::status_internal::OutOfRangeError;
  179. using ::google::protobuf::util::status_internal::PermissionDeniedError;
  180. using ::google::protobuf::util::status_internal::ResourceExhaustedError;
  181. using ::google::protobuf::util::status_internal::UnauthenticatedError;
  182. using ::google::protobuf::util::status_internal::UnavailableError;
  183. using ::google::protobuf::util::status_internal::UnimplementedError;
  184. using ::google::protobuf::util::status_internal::UnknownError;
  185. } // namespace util
  186. } // namespace protobuf
  187. } // namespace google
  188. #include <google/protobuf/port_undef.inc>
  189. #endif // GOOGLE_PROTOBUF_STUBS_STATUS_H_