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