| 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 | #include <google/protobuf/stubs/status.h> |
| 31 | |
| 32 | #include <ostream> |
| 33 | #include <stdio.h> |
| 34 | #include <string> |
| 35 | #include <utility> |
| 36 | |
| 37 | namespace google { |
| 38 | namespace protobuf { |
| 39 | namespace util { |
| 40 | namespace status_internal { |
| 41 | namespace { |
| 42 | |
| 43 | inline std::string StatusCodeToString(StatusCode code) { |
| 44 | switch (code) { |
| 45 | case StatusCode::kOk: |
| 46 | return "OK" ; |
| 47 | case StatusCode::kCancelled: |
| 48 | return "CANCELLED" ; |
| 49 | case StatusCode::kUnknown: |
| 50 | return "UNKNOWN" ; |
| 51 | case StatusCode::kInvalidArgument: |
| 52 | return "INVALID_ARGUMENT" ; |
| 53 | case StatusCode::kDeadlineExceeded: |
| 54 | return "DEADLINE_EXCEEDED" ; |
| 55 | case StatusCode::kNotFound: |
| 56 | return "NOT_FOUND" ; |
| 57 | case StatusCode::kAlreadyExists: |
| 58 | return "ALREADY_EXISTS" ; |
| 59 | case StatusCode::kPermissionDenied: |
| 60 | return "PERMISSION_DENIED" ; |
| 61 | case StatusCode::kUnauthenticated: |
| 62 | return "UNAUTHENTICATED" ; |
| 63 | case StatusCode::kResourceExhausted: |
| 64 | return "RESOURCE_EXHAUSTED" ; |
| 65 | case StatusCode::kFailedPrecondition: |
| 66 | return "FAILED_PRECONDITION" ; |
| 67 | case StatusCode::kAborted: |
| 68 | return "ABORTED" ; |
| 69 | case StatusCode::kOutOfRange: |
| 70 | return "OUT_OF_RANGE" ; |
| 71 | case StatusCode::kUnimplemented: |
| 72 | return "UNIMPLEMENTED" ; |
| 73 | case StatusCode::kInternal: |
| 74 | return "INTERNAL" ; |
| 75 | case StatusCode::kUnavailable: |
| 76 | return "UNAVAILABLE" ; |
| 77 | case StatusCode::kDataLoss: |
| 78 | return "DATA_LOSS" ; |
| 79 | } |
| 80 | |
| 81 | // No default clause, clang will abort if a code is missing from |
| 82 | // above switch. |
| 83 | return "UNKNOWN" ; |
| 84 | } |
| 85 | |
| 86 | } // namespace |
| 87 | |
| 88 | Status::Status() : error_code_(StatusCode::kOk) {} |
| 89 | |
| 90 | Status::Status(StatusCode error_code, StringPiece error_message) |
| 91 | : error_code_(error_code) { |
| 92 | if (error_code != StatusCode::kOk) { |
| 93 | error_message_ = error_message.ToString(); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | Status::Status(const Status& other) |
| 98 | : error_code_(other.error_code_), error_message_(other.error_message_) { |
| 99 | } |
| 100 | |
| 101 | Status& Status::operator=(const Status& other) { |
| 102 | error_code_ = other.error_code_; |
| 103 | error_message_ = other.error_message_; |
| 104 | return *this; |
| 105 | } |
| 106 | |
| 107 | bool Status::operator==(const Status& x) const { |
| 108 | return error_code_ == x.error_code_ && |
| 109 | error_message_ == x.error_message_; |
| 110 | } |
| 111 | |
| 112 | std::string Status::ToString() const { |
| 113 | if (error_code_ == StatusCode::kOk) { |
| 114 | return "OK" ; |
| 115 | } else { |
| 116 | if (error_message_.empty()) { |
| 117 | return StatusCodeToString(code: error_code_); |
| 118 | } else { |
| 119 | return StatusCodeToString(code: error_code_) + ":" + error_message_; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | Status OkStatus() { return Status(); } |
| 125 | |
| 126 | std::ostream& operator<<(std::ostream& os, const Status& x) { |
| 127 | os << x.ToString(); |
| 128 | return os; |
| 129 | } |
| 130 | |
| 131 | bool IsAborted(const Status& status) { |
| 132 | return status.code() == StatusCode::kAborted; |
| 133 | } |
| 134 | |
| 135 | bool IsAlreadyExists(const Status& status) { |
| 136 | return status.code() == StatusCode::kAlreadyExists; |
| 137 | } |
| 138 | |
| 139 | bool IsCancelled(const Status& status) { |
| 140 | return status.code() == StatusCode::kCancelled; |
| 141 | } |
| 142 | |
| 143 | bool IsDataLoss(const Status& status) { |
| 144 | return status.code() == StatusCode::kDataLoss; |
| 145 | } |
| 146 | |
| 147 | bool IsDeadlineExceeded(const Status& status) { |
| 148 | return status.code() == StatusCode::kDeadlineExceeded; |
| 149 | } |
| 150 | |
| 151 | bool IsFailedPrecondition(const Status& status) { |
| 152 | return status.code() == StatusCode::kFailedPrecondition; |
| 153 | } |
| 154 | |
| 155 | bool IsInternal(const Status& status) { |
| 156 | return status.code() == StatusCode::kInternal; |
| 157 | } |
| 158 | |
| 159 | bool IsInvalidArgument(const Status& status) { |
| 160 | return status.code() == StatusCode::kInvalidArgument; |
| 161 | } |
| 162 | |
| 163 | bool IsNotFound(const Status& status) { |
| 164 | return status.code() == StatusCode::kNotFound; |
| 165 | } |
| 166 | |
| 167 | bool IsOutOfRange(const Status& status) { |
| 168 | return status.code() == StatusCode::kOutOfRange; |
| 169 | } |
| 170 | |
| 171 | bool IsPermissionDenied(const Status& status) { |
| 172 | return status.code() == StatusCode::kPermissionDenied; |
| 173 | } |
| 174 | |
| 175 | bool IsResourceExhausted(const Status& status) { |
| 176 | return status.code() == StatusCode::kResourceExhausted; |
| 177 | } |
| 178 | |
| 179 | bool IsUnauthenticated(const Status& status) { |
| 180 | return status.code() == StatusCode::kUnauthenticated; |
| 181 | } |
| 182 | |
| 183 | bool IsUnavailable(const Status& status) { |
| 184 | return status.code() == StatusCode::kUnavailable; |
| 185 | } |
| 186 | |
| 187 | bool IsUnimplemented(const Status& status) { |
| 188 | return status.code() == StatusCode::kUnimplemented; |
| 189 | } |
| 190 | |
| 191 | bool IsUnknown(const Status& status) { |
| 192 | return status.code() == StatusCode::kUnknown; |
| 193 | } |
| 194 | |
| 195 | Status AbortedError(StringPiece message) { |
| 196 | return Status(StatusCode::kAborted, message); |
| 197 | } |
| 198 | |
| 199 | Status AlreadyExistsError(StringPiece message) { |
| 200 | return Status(StatusCode::kAlreadyExists, message); |
| 201 | } |
| 202 | |
| 203 | Status CancelledError(StringPiece message) { |
| 204 | return Status(StatusCode::kCancelled, message); |
| 205 | } |
| 206 | |
| 207 | Status DataLossError(StringPiece message) { |
| 208 | return Status(StatusCode::kDataLoss, message); |
| 209 | } |
| 210 | |
| 211 | Status DeadlineExceededError(StringPiece message) { |
| 212 | return Status(StatusCode::kDeadlineExceeded, message); |
| 213 | } |
| 214 | |
| 215 | Status FailedPreconditionError(StringPiece message) { |
| 216 | return Status(StatusCode::kFailedPrecondition, message); |
| 217 | } |
| 218 | |
| 219 | Status InternalError(StringPiece message) { |
| 220 | return Status(StatusCode::kInternal, message); |
| 221 | } |
| 222 | |
| 223 | Status InvalidArgumentError(StringPiece message) { |
| 224 | return Status(StatusCode::kInvalidArgument, message); |
| 225 | } |
| 226 | |
| 227 | Status NotFoundError(StringPiece message) { |
| 228 | return Status(StatusCode::kNotFound, message); |
| 229 | } |
| 230 | |
| 231 | Status OutOfRangeError(StringPiece message) { |
| 232 | return Status(StatusCode::kOutOfRange, message); |
| 233 | } |
| 234 | |
| 235 | Status PermissionDeniedError(StringPiece message) { |
| 236 | return Status(StatusCode::kPermissionDenied, message); |
| 237 | } |
| 238 | |
| 239 | Status ResourceExhaustedError(StringPiece message) { |
| 240 | return Status(StatusCode::kResourceExhausted, message); |
| 241 | } |
| 242 | |
| 243 | Status UnauthenticatedError(StringPiece message) { |
| 244 | return Status(StatusCode::kUnauthenticated, message); |
| 245 | } |
| 246 | |
| 247 | Status UnavailableError(StringPiece message) { |
| 248 | return Status(StatusCode::kUnavailable, message); |
| 249 | } |
| 250 | |
| 251 | Status UnimplementedError(StringPiece message) { |
| 252 | return Status(StatusCode::kUnimplemented, message); |
| 253 | } |
| 254 | |
| 255 | Status UnknownError(StringPiece message) { |
| 256 | return Status(StatusCode::kUnknown, message); |
| 257 | } |
| 258 | |
| 259 | } // namespace status_internal |
| 260 | } // namespace util |
| 261 | } // namespace protobuf |
| 262 | } // namespace google |
| 263 | |