/src/sentencepiece/third_party/absl/status/status.h
Line | Count | Source |
1 | | // Copyright 2019 The Abseil Authors. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | // |
15 | | // ----------------------------------------------------------------------------- |
16 | | // File: status.h |
17 | | // ----------------------------------------------------------------------------- |
18 | | // |
19 | | // This header file defines the Abseil `status` library, consisting of: |
20 | | // |
21 | | // * An `absl::Status` class for holding error handling information |
22 | | // * A set of canonical `absl::StatusCode` error codes, and associated |
23 | | // utilities for generating and propagating status codes. |
24 | | // * A set of helper functions for creating status codes and checking their |
25 | | // values |
26 | | // |
27 | | // Within Google, `absl::Status` is the primary mechanism for communicating |
28 | | // errors in C++, and is used to represent error state in both in-process |
29 | | // library calls as well as RPC calls. Some of these errors may be recoverable, |
30 | | // but others may not. Most functions that can produce a recoverable error |
31 | | // should be designed to return an `absl::Status` (or `absl::StatusOr`). |
32 | | // |
33 | | // Example: |
34 | | // |
35 | | // absl::Status myFunction(absl::string_view fname, ...) { |
36 | | // ... |
37 | | // // encounter error |
38 | | // if (error condition) { |
39 | | // return absl::InvalidArgumentError("bad mode"); |
40 | | // } |
41 | | // // else, return OK |
42 | | // return absl::OkStatus(); |
43 | | // } |
44 | | // |
45 | | // An `absl::Status` is designed to either return "OK" or one of a number of |
46 | | // different error codes, corresponding to typical error conditions. |
47 | | // In almost all cases, when using `absl::Status` you should use the canonical |
48 | | // error codes (of type `absl::StatusCode`) enumerated in this header file. |
49 | | // These canonical codes are understood across the codebase and will be |
50 | | // accepted across all API and RPC boundaries. |
51 | | #ifndef ABSL_STATUS_STATUS_H_ |
52 | | #define ABSL_STATUS_STATUS_H_ |
53 | | |
54 | | #include <cassert> |
55 | | #include <cstdint> |
56 | | #include <optional> |
57 | | #include <ostream> |
58 | | #include <string> |
59 | | #include <type_traits> |
60 | | #include <utility> |
61 | | |
62 | | #include "absl/base/attributes.h" |
63 | | #include "absl/base/config.h" |
64 | | #include "absl/base/macros.h" |
65 | | #include "absl/base/nullability.h" |
66 | | #include "absl/base/optimization.h" |
67 | | #include "absl/functional/function_ref.h" |
68 | | #include "absl/status/internal/status_internal.h" |
69 | | #include "absl/strings/cord.h" |
70 | | #include "absl/strings/string_view.h" |
71 | | #include "absl/types/optional.h" |
72 | | #include "absl/types/source_location.h" |
73 | | #include "absl/types/span.h" |
74 | | |
75 | | namespace absl { |
76 | | ABSL_NAMESPACE_BEGIN |
77 | | |
78 | | #ifndef SWIG // SWIG chokes on enum class |
79 | | // The following canonical error codes should always be in sync with |
80 | | // https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto. |
81 | | |
82 | | // absl::StatusCode |
83 | | // |
84 | | // An `absl::StatusCode` is an enumerated type indicating either no error ("OK") |
85 | | // or an error condition. In most cases, an `absl::Status` indicates a |
86 | | // recoverable error, and the purpose of signalling an error is to indicate what |
87 | | // action to take in response to that error. These error codes map to the proto |
88 | | // RPC error codes indicated in https://cloud.google.com/apis/design/errors. |
89 | | // |
90 | | // The errors listed below are the canonical errors associated with |
91 | | // `absl::Status` and are used throughout the codebase. As a result, these |
92 | | // error codes are somewhat generic. |
93 | | // |
94 | | // In general, try to return the most specific error that applies if more than |
95 | | // one error may pertain. For example, prefer `kOutOfRange` over |
96 | | // `kFailedPrecondition` if both codes apply. Similarly prefer `kNotFound` or |
97 | | // `kAlreadyExists` over `kFailedPrecondition`. |
98 | | // |
99 | | // Because these errors may cross RPC boundaries, these codes are tied to the |
100 | | // `google.rpc.Code` definitions within |
101 | | // https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto |
102 | | // The string value of these RPC codes is denoted within each enum below. |
103 | | // |
104 | | // If your error handling code requires more context, you can attach payloads |
105 | | // to your status. See `absl::Status::SetPayload()` and |
106 | | // `absl::Status::GetPayload()` below. |
107 | | enum class StatusCode : int { |
108 | | // StatusCode::kOk |
109 | | // |
110 | | // kOK (gRPC code "OK") does not indicate an error; this value is returned on |
111 | | // success. It is typical to check for this value before proceeding on any |
112 | | // given call across an API or RPC boundary. To check this value, use the |
113 | | // `absl::Status::ok()` member function rather than inspecting the raw code. |
114 | | kOk = 0, |
115 | | |
116 | | // StatusCode::kCancelled |
117 | | // |
118 | | // kCancelled (gRPC code "CANCELLED") indicates the operation was cancelled, |
119 | | // typically by the caller. |
120 | | kCancelled = 1, |
121 | | |
122 | | // StatusCode::kUnknown |
123 | | // |
124 | | // kUnknown (gRPC code "UNKNOWN") indicates an unknown error occurred. In |
125 | | // general, more specific errors should be raised, if possible. Errors raised |
126 | | // by APIs that do not return enough error information may be converted to |
127 | | // this error. |
128 | | kUnknown = 2, |
129 | | |
130 | | // StatusCode::kInvalidArgument |
131 | | // |
132 | | // kInvalidArgument (gRPC code "INVALID_ARGUMENT") indicates the caller |
133 | | // specified an invalid argument, such as a malformed filename. Note that use |
134 | | // of such errors should be narrowly limited to indicate the invalid nature of |
135 | | // the arguments themselves. Errors with validly formed arguments that may |
136 | | // cause errors with the state of the receiving system should be denoted with |
137 | | // `kFailedPrecondition` instead. |
138 | | kInvalidArgument = 3, |
139 | | |
140 | | // StatusCode::kDeadlineExceeded |
141 | | // |
142 | | // kDeadlineExceeded (gRPC code "DEADLINE_EXCEEDED") indicates a deadline |
143 | | // expired before the operation could complete. For operations that may change |
144 | | // state within a system, this error may be returned even if the operation has |
145 | | // completed successfully. For example, a successful response from a server |
146 | | // could have been delayed long enough for the deadline to expire. |
147 | | kDeadlineExceeded = 4, |
148 | | |
149 | | // StatusCode::kNotFound |
150 | | // |
151 | | // kNotFound (gRPC code "NOT_FOUND") indicates some requested entity (such as |
152 | | // a file or directory) was not found. |
153 | | // |
154 | | // `kNotFound` is useful if a request should be denied for an entire class of |
155 | | // users, such as during a gradual feature rollout or undocumented allow list. |
156 | | // If a request should be denied for specific sets of users, such as through |
157 | | // user-based access control, use `kPermissionDenied` instead. |
158 | | kNotFound = 5, |
159 | | |
160 | | // StatusCode::kAlreadyExists |
161 | | // |
162 | | // kAlreadyExists (gRPC code "ALREADY_EXISTS") indicates that the entity a |
163 | | // caller attempted to create (such as a file or directory) is already |
164 | | // present. |
165 | | kAlreadyExists = 6, |
166 | | |
167 | | // StatusCode::kPermissionDenied |
168 | | // |
169 | | // kPermissionDenied (gRPC code "PERMISSION_DENIED") indicates that the caller |
170 | | // does not have permission to execute the specified operation. Note that this |
171 | | // error is different than an error due to an *un*authenticated user. This |
172 | | // error code does not imply the request is valid or the requested entity |
173 | | // exists or satisfies any other pre-conditions. |
174 | | // |
175 | | // `kPermissionDenied` must not be used for rejections caused by exhausting |
176 | | // some resource. Instead, use `kResourceExhausted` for those errors. |
177 | | // `kPermissionDenied` must not be used if the caller cannot be identified. |
178 | | // Instead, use `kUnauthenticated` for those errors. |
179 | | kPermissionDenied = 7, |
180 | | |
181 | | // StatusCode::kResourceExhausted |
182 | | // |
183 | | // kResourceExhausted (gRPC code "RESOURCE_EXHAUSTED") indicates some resource |
184 | | // has been exhausted, perhaps a per-user quota, or perhaps the entire file |
185 | | // system is out of space. |
186 | | kResourceExhausted = 8, |
187 | | |
188 | | // StatusCode::kFailedPrecondition |
189 | | // |
190 | | // kFailedPrecondition (gRPC code "FAILED_PRECONDITION") indicates that the |
191 | | // operation was rejected because the system is not in a state required for |
192 | | // the operation's execution. For example, a directory to be deleted may be |
193 | | // non-empty, an "rmdir" operation is applied to a non-directory, etc. |
194 | | // |
195 | | // Some guidelines that may help a service implementer in deciding between |
196 | | // `kFailedPrecondition`, `kAborted`, and `kUnavailable`: |
197 | | // |
198 | | // (a) Use `kUnavailable` if the client can retry just the failing call. |
199 | | // (b) Use `kAborted` if the client should retry at a higher transaction |
200 | | // level (such as when a client-specified test-and-set fails, indicating |
201 | | // the client should restart a read-modify-write sequence). |
202 | | // (c) Use `kFailedPrecondition` if the client should not retry until |
203 | | // the system state has been explicitly fixed. For example, if a "rmdir" |
204 | | // fails because the directory is non-empty, `kFailedPrecondition` |
205 | | // should be returned since the client should not retry unless |
206 | | // the files are deleted from the directory. |
207 | | kFailedPrecondition = 9, |
208 | | |
209 | | // StatusCode::kAborted |
210 | | // |
211 | | // kAborted (gRPC code "ABORTED") indicates the operation was aborted, |
212 | | // typically due to a concurrency issue such as a sequencer check failure or a |
213 | | // failed transaction. |
214 | | // |
215 | | // See the guidelines above for deciding between `kFailedPrecondition`, |
216 | | // `kAborted`, and `kUnavailable`. |
217 | | kAborted = 10, |
218 | | |
219 | | // StatusCode::kOutOfRange |
220 | | // |
221 | | // kOutOfRange (gRPC code "OUT_OF_RANGE") indicates the operation was |
222 | | // attempted past the valid range, such as seeking or reading past an |
223 | | // end-of-file. |
224 | | // |
225 | | // Unlike `kInvalidArgument`, this error indicates a problem that may |
226 | | // be fixed if the system state changes. For example, a 32-bit file |
227 | | // system will generate `kInvalidArgument` if asked to read at an |
228 | | // offset that is not in the range [0,2^32-1], but it will generate |
229 | | // `kOutOfRange` if asked to read from an offset past the current |
230 | | // file size. |
231 | | // |
232 | | // There is a fair bit of overlap between `kFailedPrecondition` and |
233 | | // `kOutOfRange`. We recommend using `kOutOfRange` (the more specific |
234 | | // error) when it applies so that callers who are iterating through |
235 | | // a space can easily look for an `kOutOfRange` error to detect when |
236 | | // they are done. |
237 | | kOutOfRange = 11, |
238 | | |
239 | | // StatusCode::kUnimplemented |
240 | | // |
241 | | // kUnimplemented (gRPC code "UNIMPLEMENTED") indicates the operation is not |
242 | | // implemented or supported in this service. In this case, the operation |
243 | | // should not be re-attempted. |
244 | | kUnimplemented = 12, |
245 | | |
246 | | // StatusCode::kInternal |
247 | | // |
248 | | // kInternal (gRPC code "INTERNAL") indicates an internal error has occurred |
249 | | // and some invariants expected by the underlying system have not been |
250 | | // satisfied. This error code is reserved for serious errors. |
251 | | kInternal = 13, |
252 | | |
253 | | // StatusCode::kUnavailable |
254 | | // |
255 | | // kUnavailable (gRPC code "UNAVAILABLE") indicates the service is currently |
256 | | // unavailable and that this is most likely a transient condition. An error |
257 | | // such as this can be corrected by retrying with a backoff scheme. Note that |
258 | | // it is not always safe to retry non-idempotent operations. |
259 | | // |
260 | | // See the guidelines above for deciding between `kFailedPrecondition`, |
261 | | // `kAborted`, and `kUnavailable`. |
262 | | kUnavailable = 14, |
263 | | |
264 | | // StatusCode::kDataLoss |
265 | | // |
266 | | // kDataLoss (gRPC code "DATA_LOSS") indicates that unrecoverable data loss or |
267 | | // corruption has occurred. As this error is serious, proper alerting should |
268 | | // be attached to errors such as this. |
269 | | kDataLoss = 15, |
270 | | |
271 | | // StatusCode::kUnauthenticated |
272 | | // |
273 | | // kUnauthenticated (gRPC code "UNAUTHENTICATED") indicates that the request |
274 | | // does not have valid authentication credentials for the operation. Correct |
275 | | // the authentication and try again. |
276 | | kUnauthenticated = 16, |
277 | | |
278 | | // StatusCode::DoNotUseReservedForFutureExpansionUseDefaultInSwitchInstead_ |
279 | | // |
280 | | // NOTE: this error code entry should not be used and you should not rely on |
281 | | // its value, which may change. |
282 | | // |
283 | | // The purpose of this enumerated value is to force people who handle status |
284 | | // codes with `switch()` statements to *not* simply enumerate all possible |
285 | | // values, but instead provide a "default:" case. Providing such a default |
286 | | // case ensures that code will compile when new codes are added. |
287 | | kDoNotUseReservedForFutureExpansionUseDefaultInSwitchInstead_ = 20 |
288 | | }; |
289 | | |
290 | | // StatusCodeToString() |
291 | | // |
292 | | // Returns the name for the status code, or "" if it is an unknown value. |
293 | | std::string StatusCodeToString(StatusCode code); |
294 | | |
295 | | // StatusCodeToStringView() |
296 | | // |
297 | | // Same as StatusCodeToString(), but returns a string_view. |
298 | | absl::string_view StatusCodeToStringView(StatusCode code); |
299 | | |
300 | | // operator<< |
301 | | // |
302 | | // Streams StatusCodeToString(code) to `os`. |
303 | | std::ostream& operator<<(std::ostream& os, StatusCode code); |
304 | | |
305 | | // absl::StatusToStringMode |
306 | | // |
307 | | // An `absl::StatusToStringMode` is an enumerated type indicating how |
308 | | // `absl::Status::ToString()` should construct the output string for a non-ok |
309 | | // status. |
310 | | enum class StatusToStringMode : int { |
311 | | // ToString will not contain any extra data (such as payloads). It will only |
312 | | // contain the error code and message, if any. |
313 | | kWithNoExtraData = 0, |
314 | | // ToString will contain the payloads. |
315 | | kWithPayload = 1 << 0, |
316 | | // ToString will contain the source locations. |
317 | | kWithSourceLocation = 1 << 1, |
318 | | // ToString will include all the extra data this Status has. |
319 | | kWithEverything = ~kWithNoExtraData, |
320 | | // Default mode used by ToString. Its exact value might change in the future. |
321 | | kDefault = kWithPayload, |
322 | | }; |
323 | | |
324 | | // absl::StatusToStringMode is specified as a bitmask type, which means the |
325 | | // following operations must be provided: |
326 | | constexpr StatusToStringMode operator&(StatusToStringMode lhs, |
327 | | StatusToStringMode rhs) { |
328 | | return static_cast<StatusToStringMode>(static_cast<int>(lhs) & |
329 | | static_cast<int>(rhs)); |
330 | | } |
331 | | constexpr StatusToStringMode operator|(StatusToStringMode lhs, |
332 | | StatusToStringMode rhs) { |
333 | | return static_cast<StatusToStringMode>(static_cast<int>(lhs) | |
334 | | static_cast<int>(rhs)); |
335 | | } |
336 | | constexpr StatusToStringMode operator^(StatusToStringMode lhs, |
337 | | StatusToStringMode rhs) { |
338 | | return static_cast<StatusToStringMode>(static_cast<int>(lhs) ^ |
339 | | static_cast<int>(rhs)); |
340 | | } |
341 | | constexpr StatusToStringMode operator~(StatusToStringMode arg) { |
342 | | return static_cast<StatusToStringMode>(~static_cast<int>(arg)); |
343 | | } |
344 | | inline StatusToStringMode& operator&=(StatusToStringMode& lhs, |
345 | | StatusToStringMode rhs) { |
346 | | lhs = lhs & rhs; |
347 | | return lhs; |
348 | | } |
349 | | inline StatusToStringMode& operator|=(StatusToStringMode& lhs, |
350 | | StatusToStringMode rhs) { |
351 | | lhs = lhs | rhs; |
352 | | return lhs; |
353 | | } |
354 | | inline StatusToStringMode& operator^=(StatusToStringMode& lhs, |
355 | | StatusToStringMode rhs) { |
356 | | lhs = lhs ^ rhs; |
357 | | return lhs; |
358 | | } |
359 | | #endif // SWIG |
360 | | |
361 | | // absl::Status |
362 | | // |
363 | | // The `absl::Status` class is generally used to gracefully handle errors |
364 | | // across API boundaries (and in particular across RPC boundaries). Some of |
365 | | // these errors may be recoverable, but others may not. Most |
366 | | // functions which can produce a recoverable error should be designed to return |
367 | | // either an `absl::Status` (or the similar `absl::StatusOr<T>`, which holds |
368 | | // either an object of type `T` or an error). |
369 | | // |
370 | | // API developers should construct their functions to return `absl::OkStatus()` |
371 | | // upon success, or an `absl::StatusCode` upon another type of error (e.g |
372 | | // an `absl::StatusCode::kInvalidArgument` error). The API provides convenience |
373 | | // functions to construct each status code. |
374 | | // |
375 | | // Example: |
376 | | // |
377 | | // absl::Status myFunction(absl::string_view fname, ...) { |
378 | | // ... |
379 | | // // encounter error |
380 | | // if (error condition) { |
381 | | // // Construct an absl::StatusCode::kInvalidArgument error |
382 | | // return absl::InvalidArgumentError("bad mode"); |
383 | | // } |
384 | | // // else, return OK |
385 | | // return absl::OkStatus(); |
386 | | // } |
387 | | // |
388 | | // Users handling status error codes should prefer checking for an OK status |
389 | | // using the `ok()` member function. Handling multiple error codes may justify |
390 | | // use of switch statement, but only check for error codes you know how to |
391 | | // handle; do not try to exhaustively match against all canonical error codes. |
392 | | // Errors that cannot be handled should be logged and/or propagated for higher |
393 | | // levels to deal with. If you do use a switch statement, make sure that you |
394 | | // also provide a `default:` switch case, so that code does not break as other |
395 | | // canonical codes are added to the API. |
396 | | // |
397 | | // Example: |
398 | | // |
399 | | // absl::Status result = DoSomething(); |
400 | | // if (!result.ok()) { |
401 | | // LOG(ERROR) << result; |
402 | | // } |
403 | | // |
404 | | // // Provide a default if switching on multiple error codes |
405 | | // switch (result.code()) { |
406 | | // // The user hasn't authenticated. Ask them to reauth |
407 | | // case absl::StatusCode::kUnauthenticated: |
408 | | // DoReAuth(); |
409 | | // break; |
410 | | // // The user does not have permission. Log an error. |
411 | | // case absl::StatusCode::kPermissionDenied: |
412 | | // LOG(ERROR) << result; |
413 | | // break; |
414 | | // // Propagate the error otherwise. |
415 | | // default: |
416 | | // return true; |
417 | | // } |
418 | | // |
419 | | // An `absl::Status` can optionally include a payload with more information |
420 | | // about the error. Typically, this payload serves one of several purposes: |
421 | | // |
422 | | // * It may provide more fine-grained semantic information about the error to |
423 | | // facilitate actionable remedies. |
424 | | // * It may provide human-readable contextual information that is more |
425 | | // appropriate to display to an end user. |
426 | | // |
427 | | // Example: |
428 | | // |
429 | | // absl::Status result = DoSomething(); |
430 | | // // Inform user to retry after 30 seconds |
431 | | // // See more error details in googleapis/google/rpc/error_details.proto |
432 | | // if (absl::IsResourceExhausted(result)) { |
433 | | // google::rpc::RetryInfo info; |
434 | | // info.retry_delay().seconds() = 30; |
435 | | // // Payloads require a unique key (a URL to ensure no collisions with |
436 | | // // other payloads), and an `absl::Cord` to hold the encoded data. |
437 | | // absl::string_view url = "type.googleapis.com/google.rpc.RetryInfo"; |
438 | | // result.SetPayload(url, info.SerializeAsCord()); |
439 | | // return result; |
440 | | // } |
441 | | // |
442 | | // For documentation see https://abseil.io/docs/cpp/guides/status. |
443 | | // |
444 | | // Returned Status objects may not be ignored. status_internal.h has a forward |
445 | | // declaration of the form |
446 | | // class ABSL_MUST_USE_RESULT Status; |
447 | | class ABSL_ATTRIBUTE_TRIVIAL_ABI Status final { |
448 | | public: |
449 | | // Constructors |
450 | | |
451 | | // This default constructor creates an OK status with no message or payload. |
452 | | // Avoid this constructor and prefer explicit construction of an OK status |
453 | | // with `absl::OkStatus()`. |
454 | | Status(); |
455 | | |
456 | | // Creates a status in the canonical error space with the specified |
457 | | // `absl::StatusCode` and error message. If `code == absl::StatusCode::kOk`, |
458 | | // `msg` is ignored and an object identical to an OK status is constructed. |
459 | | // |
460 | | // The `msg` string must be in UTF-8. The implementation may complain (e.g., |
461 | | // by printing a warning) if it is not. |
462 | | // |
463 | | // The `loc` is the SourceLocation of the callsite. It will be stored in the |
464 | | // Status iff `code != absl::StatusCode::kOk` and `!msg.empty()`. |
465 | | Status(absl::StatusCode code, absl::string_view msg, |
466 | | absl::SourceLocation loc = SourceLocation::current()); |
467 | | |
468 | | #ifndef SWIG |
469 | | // Same as above but for rvalue string. |
470 | | // Note: using a template to disambiguate the case of matching string_view and |
471 | | // string&& (e.g. char*) as a template lowers the priority of the overload. |
472 | | template <typename String, |
473 | | typename = std::enable_if_t<std::is_same_v<String, std::string>>> |
474 | | Status(absl::StatusCode code, String&& msg, |
475 | | absl::SourceLocation loc = SourceLocation::current()); |
476 | | #endif // SWIG |
477 | | |
478 | | // Create a status from a `base_status` and a `loc`. The `loc` will be |
479 | | // appended to the location chain of the new status, iff the `base_status` is |
480 | | // not ok and has non-empty msg. |
481 | | Status(const Status& base_status, absl::SourceLocation loc) |
482 | | : Status(base_status) { |
483 | | AddSourceLocation(loc); |
484 | | } |
485 | | #ifndef SWIG |
486 | | Status(Status&& base_status, absl::SourceLocation loc) |
487 | | : Status(std::move(base_status)) { |
488 | | AddSourceLocation(loc); |
489 | | } |
490 | | #endif // SWIG |
491 | | |
492 | | Status(const Status&); |
493 | | Status& operator=(const Status& x); |
494 | | |
495 | | #ifndef SWIG |
496 | | // Move operators |
497 | | |
498 | | // The moved-from state is valid but unspecified. |
499 | | Status(Status&&) noexcept; |
500 | | Status& operator=(Status&&) noexcept; |
501 | | #endif // SWIG |
502 | | |
503 | | ~Status(); |
504 | | |
505 | | // Status::Update() |
506 | | // |
507 | | // Updates the existing status with `new_status` provided that `this->ok()`. |
508 | | // If the existing status already contains a non-OK error, this update has no |
509 | | // effect and preserves the current data. Note that this behavior may change |
510 | | // in the future to augment a current non-ok status with additional |
511 | | // information about `new_status`. |
512 | | // |
513 | | // `Update()` provides a convenient way of keeping track of the first error |
514 | | // encountered. |
515 | | // |
516 | | // Example: |
517 | | // // Instead of "if (overall_status.ok()) overall_status = new_status" |
518 | | // overall_status.Update(new_status); |
519 | | // |
520 | | void Update(const Status& new_status); |
521 | | #ifndef SWIG |
522 | | void Update(Status&& new_status); |
523 | | #endif // SWIG |
524 | | |
525 | | // Status::ok() |
526 | | // |
527 | | // Returns `true` if `this->code()` == `absl::StatusCode::kOk`, |
528 | | // indicating the absence of an error. |
529 | | // Prefer checking for an OK status using this member function. |
530 | | ABSL_MUST_USE_RESULT bool ok() const; |
531 | | |
532 | | // Status::code() |
533 | | // |
534 | | // Returns the canonical error code of type `absl::StatusCode` of this status. |
535 | | absl::StatusCode code() const; |
536 | | |
537 | | // Status::raw_code() |
538 | | // |
539 | | // Returns a raw (canonical) error code corresponding to the enum value of |
540 | | // `google.rpc.Code` definitions within |
541 | | // https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto. |
542 | | // These values could be out of the range of canonical `absl::StatusCode` |
543 | | // enum values. |
544 | | // |
545 | | // NOTE: This function should only be called when converting to an associated |
546 | | // wire format. Use `Status::code()` for error handling. |
547 | | int raw_code() const; |
548 | | |
549 | | #ifndef SWIG |
550 | | |
551 | | // Status::message() |
552 | | // |
553 | | // Returns the error message associated with this error code, if available. |
554 | | // Note that this message rarely describes the error code. It is not unusual |
555 | | // for the error message to be the empty string. As a result, prefer |
556 | | // `operator<<` or `Status::ToString()` for debug logging. |
557 | | absl::string_view message() const; |
558 | | #endif // SWIG |
559 | | |
560 | | friend bool operator==(const Status&, const Status&); |
561 | | friend bool operator!=(const Status&, const Status&); |
562 | | |
563 | | #ifndef SWIG |
564 | | // `ToString` has stubs in SWIG to remain backward compatible with the old |
565 | | // format by calling `util::StatusToString`, to save migration cost. |
566 | | |
567 | | // Status::ToString() |
568 | | // |
569 | | // Returns a string based on the `mode`. By default, it returns combination of |
570 | | // the error code name, the message and any associated payload messages. This |
571 | | // string is designed simply to be human readable and its exact format should |
572 | | // not be load bearing. Do not depend on the exact format of the result of |
573 | | // `ToString()` which is subject to change. |
574 | | // |
575 | | // The printed code name and the message are generally substrings of the |
576 | | // result, and the payloads to be printed use the status payload printer |
577 | | // mechanism (which is internal). |
578 | | std::string ToString( |
579 | | StatusToStringMode mode = StatusToStringMode::kDefault) const; |
580 | | |
581 | | // Support `absl::StrCat`, `absl::StrFormat`, etc. |
582 | | template <typename Sink> |
583 | | friend void AbslStringify(Sink& sink, const Status& status) { |
584 | | sink.Append(status.ToString(StatusToStringMode::kWithEverything)); |
585 | | } |
586 | | #endif // SWIG |
587 | | |
588 | | // Status::IgnoreError() |
589 | | // |
590 | | // Ignores any errors. This method does nothing except potentially suppress |
591 | | // complaints from any tools that are checking that errors are not dropped on |
592 | | // the floor. |
593 | | void IgnoreError() const; |
594 | | |
595 | | // swap() |
596 | | // |
597 | | // Swap the contents of one status with another. |
598 | | #ifndef SWIG |
599 | | friend void swap(Status& a, Status& b) noexcept; |
600 | | #else |
601 | | friend void swap(Status& a, Status& b); |
602 | | #endif // SWIG |
603 | | |
604 | | //---------------------------------------------------------------------------- |
605 | | // Payload Management APIs |
606 | | //---------------------------------------------------------------------------- |
607 | | |
608 | | // A payload may be attached to a status to provide additional context to an |
609 | | // error that may not be satisfied by an existing `absl::StatusCode`. |
610 | | // Typically, this payload serves one of several purposes: |
611 | | // |
612 | | // * It may provide more fine-grained semantic information about the error |
613 | | // to facilitate actionable remedies. |
614 | | // * It may provide human-readable contextual information that is more |
615 | | // appropriate to display to an end user. |
616 | | // |
617 | | // A payload consists of a [key,value] pair, where the key is a string |
618 | | // referring to a unique "type URL" and the value is an object of type |
619 | | // `absl::Cord` to hold the contextual data. |
620 | | // |
621 | | // The "type URL" should be unique and follow the format of a URL |
622 | | // (https://en.wikipedia.org/wiki/URL) and, ideally, provide some |
623 | | // documentation or schema on how to interpret its associated data. For |
624 | | // example, the default type URL for a protobuf message type is |
625 | | // "type.googleapis.com/packagename.messagename". Other custom wire formats |
626 | | // should define the format of type URL in a similar practice so as to |
627 | | // minimize the chance of conflict between type URLs. |
628 | | // Users should ensure that the type URL can be mapped to a concrete |
629 | | // C++ type if they want to deserialize the payload and read it effectively. |
630 | | // |
631 | | // To attach a payload to a status object, call `Status::SetPayload()`, |
632 | | // passing it the type URL and an `absl::Cord` of associated data. Similarly, |
633 | | // to extract the payload from a status, call `Status::GetPayload()`. You |
634 | | // may attach multiple payloads (with differing type URLs) to any given |
635 | | // status object, provided that the status is currently exhibiting an error |
636 | | // code (i.e. is not OK). |
637 | | |
638 | | // Status::GetPayload() |
639 | | // |
640 | | // Gets the payload of a status given its unique `type_url` key, if present. |
641 | | std::optional<absl::Cord> GetPayload(absl::string_view type_url) const; |
642 | | |
643 | | // Status::SetPayload() |
644 | | // |
645 | | // Sets the payload for a non-ok status using a `type_url` key, overwriting |
646 | | // any existing payload for that `type_url`. |
647 | | // |
648 | | // NOTE: This function does nothing if the Status is ok. |
649 | | void SetPayload(absl::string_view type_url, absl::Cord payload); |
650 | | |
651 | | // Status::ErasePayload() |
652 | | // |
653 | | // Erases the payload corresponding to the `type_url` key. Returns `true` if |
654 | | // the payload was present. |
655 | | bool ErasePayload(absl::string_view type_url); |
656 | | |
657 | | // Status::ForEachPayload() |
658 | | // |
659 | | // Iterates over the stored payloads and calls the |
660 | | // `visitor(type_key, payload)` callable for each one. |
661 | | // |
662 | | // NOTE: The order of calls to `visitor()` is not specified and may change at |
663 | | // any time. |
664 | | // |
665 | | // NOTE: Any mutation on the same 'absl::Status' object during visitation is |
666 | | // forbidden and could result in undefined behavior. |
667 | | // FunctionRef doesn't work nicely with Swig. |
668 | | // TODO(b/189736749): Consider making this available once FunctionRef is |
669 | | // supported. |
670 | | #ifndef SWIG |
671 | | void ForEachPayload( |
672 | | absl::FunctionRef<void(absl::string_view, const absl::Cord&)> visitor) |
673 | | const; |
674 | | #endif // SWIG |
675 | | |
676 | | absl::Span<const absl::SourceLocation> GetSourceLocations() const { |
677 | | if (IsInlined(rep_)) return {}; |
678 | | return RepToPointer(rep_)->GetSourceLocations(); |
679 | | } |
680 | | // Appends the `loc` to the current location chain inside the status, iff the |
681 | | // status is non-ok and contains a non-empty message. |
682 | | void AddSourceLocation( |
683 | | absl::SourceLocation loc = absl::SourceLocation::current()) { |
684 | | if (ok()) return; |
685 | | rep_ = AddSourceLocationImpl(rep_, loc); |
686 | | [[maybe_unused]] bool okay = ok(); |
687 | | // This hint tells the optimizer that the status is still not ok after the |
688 | | // AddSourceLocation() call. This is useful when passing a known !ok status |
689 | | // to StatusOr. StatusOr checks for ok() on its constructor and this assume |
690 | | // helps the optimizer remove that check. |
691 | | ABSL_ASSUME(!okay); |
692 | | } |
693 | | |
694 | | #ifndef SWIG |
695 | | |
696 | | // Status::WithSourceLocation() |
697 | | // |
698 | | // Returns a copy of the current status, with `loc` appended to its location |
699 | | // chain iff the status is non-ok and contains a non-empty message. |
700 | | // |
701 | | // Example: |
702 | | // |
703 | | // if (Status status = Foo(); !status.ok()) { |
704 | | // return status.WithSourceLocation(); |
705 | | // } |
706 | | Status WithSourceLocation( |
707 | | absl::SourceLocation loc = absl::SourceLocation::current()) const& { |
708 | | return Status(*this, loc); |
709 | | } |
710 | | |
711 | | // Status::WithSourceLocation() |
712 | | // |
713 | | // Appends the `loc` to the current location chain inside the status iff the |
714 | | // status is non-ok and contains a non-empty message, and returns an rvalue |
715 | | // reference to `*this`. |
716 | | // |
717 | | // Example: |
718 | | // |
719 | | // Status Finalize(...); |
720 | | // |
721 | | // Status DoSomething(...) { |
722 | | // ... |
723 | | // return Finalize().WithSourceLocation(); |
724 | | // } |
725 | | ABSL_MUST_USE_RESULT Status&& WithSourceLocation( |
726 | | absl::SourceLocation loc = absl::SourceLocation::current()) && { |
727 | | AddSourceLocation(loc); |
728 | | return std::move(*this); |
729 | | } |
730 | | #endif // SWIG |
731 | | |
732 | | private: |
733 | | friend Status CancelledError(); |
734 | | |
735 | | #ifndef SWIG |
736 | | // Returns a `Status` object which is not `ok()` but |
737 | | // `code() == absl::StatusCode::kOk`. This is necessary to be compatible with |
738 | | // `Status` objects created with an error code in a custom `ErrorSpace` that |
739 | | // is mapped to the canonical code `absl::StatusCode::kOk`. |
740 | | static Status MakeNonOkStatusWithOkCode(absl::string_view message); |
741 | | |
742 | | friend class absl::status_internal::StatusPrivateAccessor; |
743 | | friend class absl::status_internal::StatusPrivateAccessorForStatusBuilder; |
744 | | template <typename T> |
745 | | friend class absl::StatusOr; |
746 | | #endif // !SWIG |
747 | | |
748 | | // Creates a status in the canonical error space with the specified |
749 | | // code, and an empty error message. |
750 | | explicit Status(absl::StatusCode code); |
751 | | |
752 | | // Delegate factory in header that ensures CodeToInlinedRep is inlined |
753 | | // where possible. |
754 | | static uintptr_t MakeRepFromStringView(uintptr_t inlined_rep, |
755 | | absl::string_view msg, |
756 | | absl::SourceLocation loc); |
757 | | |
758 | | #ifndef SWIG |
759 | | // Same as above but for rvalue string. |
760 | | static uintptr_t MakeRepFromStringRvalue(uintptr_t inlined_rep, |
761 | | std::string&& msg, |
762 | | absl::SourceLocation loc); |
763 | | #endif // SWIG |
764 | | |
765 | | template <typename StringOrView> |
766 | | friend uintptr_t MakeStatusRepImpl(uintptr_t inlined_rep, StringOrView msg, |
767 | | absl::SourceLocation loc); |
768 | | |
769 | | // Underlying constructor for status from a rep_. |
770 | 1.23M | explicit Status(uintptr_t rep) : rep_(rep) {} |
771 | | |
772 | | // An out-of-line AddSourceLocation that mutates rep directly. |
773 | | static uintptr_t AddSourceLocationImpl(uintptr_t rep, |
774 | | absl::SourceLocation loc); |
775 | | |
776 | | static void Ref(uintptr_t rep); |
777 | | static void Unref(uintptr_t rep); |
778 | | |
779 | | // REQUIRES: !ok() |
780 | | // Ensures rep is not inlined or shared with any other Status. |
781 | | static status_internal::StatusRep* absl_nonnull PrepareToModify( |
782 | | uintptr_t rep); |
783 | | |
784 | | // MSVC 14.0 limitation requires the const. |
785 | | static constexpr const char kMovedFromString[] = |
786 | | "Status accessed after move."; |
787 | | |
788 | | static const std::string* absl_nonnull EmptyString(); |
789 | | static const std::string* absl_nonnull MovedFromString(); |
790 | | |
791 | | // Returns whether rep contains an inlined representation. |
792 | | // See rep_ for details. |
793 | | static constexpr bool IsInlined(uintptr_t rep); |
794 | | |
795 | | // Indicates whether this Status was the rhs of a move operation. See rep_ |
796 | | // for details. |
797 | | static constexpr bool IsMovedFrom(uintptr_t rep); |
798 | | static constexpr uintptr_t MovedFromRep(); |
799 | | |
800 | | // Convert between error::Code and the inlined uintptr_t representation used |
801 | | // by rep_. See rep_ for details. |
802 | | static constexpr uintptr_t CodeToInlinedRep(absl::StatusCode code); |
803 | | static constexpr absl::StatusCode InlinedRepToCode(uintptr_t rep); |
804 | | |
805 | | // Converts between StatusRep* and the external uintptr_t representation used |
806 | | // by rep_. See rep_ for details. |
807 | | static uintptr_t PointerToRep(status_internal::StatusRep* absl_nonnull rep); |
808 | | static const status_internal::StatusRep* absl_nonnull RepToPointer( |
809 | | uintptr_t rep); |
810 | | |
811 | | static std::string ToStringSlow(uintptr_t rep, StatusToStringMode mode); |
812 | | |
813 | | // Status supports two different representations. |
814 | | // - When the low bit is set it is an inlined representation. |
815 | | // It uses the canonical error space, no message or payload. |
816 | | // The error code is (rep_ >> 2). |
817 | | // The (rep_ & 2) bit is the "moved from" indicator, used in IsMovedFrom(). |
818 | | // - When the low bit is off it is an external representation. |
819 | | // In this case all the data comes from a heap allocated Rep object. |
820 | | // rep_ is a status_internal::StatusRep* pointer to that structure. |
821 | | uintptr_t rep_; |
822 | | |
823 | | friend class status_internal::StatusRep; |
824 | | }; |
825 | | |
826 | | // OkStatus() |
827 | | // |
828 | | // Returns an OK status, equivalent to a default constructed instance. Prefer |
829 | | // usage of `absl::OkStatus()` when constructing such an OK status. |
830 | | Status OkStatus(); |
831 | | |
832 | | // operator<<() |
833 | | // |
834 | | // Prints a human-readable representation of `x` to `os`. |
835 | | std::ostream& operator<<(std::ostream& os, const Status& x); |
836 | | |
837 | | // IsAborted() |
838 | | // IsAlreadyExists() |
839 | | // IsCancelled() |
840 | | // IsDataLoss() |
841 | | // IsDeadlineExceeded() |
842 | | // IsFailedPrecondition() |
843 | | // IsInternal() |
844 | | // IsInvalidArgument() |
845 | | // IsNotFound() |
846 | | // IsOutOfRange() |
847 | | // IsPermissionDenied() |
848 | | // IsResourceExhausted() |
849 | | // IsUnauthenticated() |
850 | | // IsUnavailable() |
851 | | // IsUnimplemented() |
852 | | // IsUnknown() |
853 | | // |
854 | | // These convenience functions return `true` if a given status matches the |
855 | | // `absl::StatusCode` error code of its associated function. |
856 | | ABSL_MUST_USE_RESULT bool IsAborted(const Status& status); |
857 | | ABSL_MUST_USE_RESULT bool IsAlreadyExists(const Status& status); |
858 | | ABSL_MUST_USE_RESULT bool IsCancelled(const Status& status); |
859 | | ABSL_MUST_USE_RESULT bool IsDataLoss(const Status& status); |
860 | | ABSL_MUST_USE_RESULT bool IsDeadlineExceeded(const Status& status); |
861 | | ABSL_MUST_USE_RESULT bool IsFailedPrecondition(const Status& status); |
862 | | ABSL_MUST_USE_RESULT bool IsInternal(const Status& status); |
863 | | ABSL_MUST_USE_RESULT bool IsInvalidArgument(const Status& status); |
864 | | ABSL_MUST_USE_RESULT bool IsNotFound(const Status& status); |
865 | | ABSL_MUST_USE_RESULT bool IsOutOfRange(const Status& status); |
866 | | ABSL_MUST_USE_RESULT bool IsPermissionDenied(const Status& status); |
867 | | ABSL_MUST_USE_RESULT bool IsResourceExhausted(const Status& status); |
868 | | ABSL_MUST_USE_RESULT bool IsUnauthenticated(const Status& status); |
869 | | ABSL_MUST_USE_RESULT bool IsUnavailable(const Status& status); |
870 | | ABSL_MUST_USE_RESULT bool IsUnimplemented(const Status& status); |
871 | | ABSL_MUST_USE_RESULT bool IsUnknown(const Status& status); |
872 | | |
873 | | // AbortedError() |
874 | | // AlreadyExistsError() |
875 | | // CancelledError() |
876 | | // DataLossError() |
877 | | // DeadlineExceededError() |
878 | | // FailedPreconditionError() |
879 | | // InternalError() |
880 | | // InvalidArgumentError() |
881 | | // NotFoundError() |
882 | | // OutOfRangeError() |
883 | | // PermissionDeniedError() |
884 | | // ResourceExhaustedError() |
885 | | // UnauthenticatedError() |
886 | | // UnavailableError() |
887 | | // UnimplementedError() |
888 | | // UnknownError() |
889 | | // |
890 | | // These convenience functions create an `absl::Status` object with an error |
891 | | // code as indicated by the associated function name, using the error message |
892 | | // passed in `message`. |
893 | | Status AbortedError(absl::string_view message, |
894 | | absl::SourceLocation loc = SourceLocation::current()); |
895 | | Status AlreadyExistsError(absl::string_view message, |
896 | | absl::SourceLocation loc = SourceLocation::current()); |
897 | | Status CancelledError(absl::string_view message, |
898 | | absl::SourceLocation loc = SourceLocation::current()); |
899 | | Status DataLossError(absl::string_view message, |
900 | | absl::SourceLocation loc = SourceLocation::current()); |
901 | | Status DeadlineExceededError( |
902 | | absl::string_view message, |
903 | | absl::SourceLocation loc = SourceLocation::current()); |
904 | | Status FailedPreconditionError( |
905 | | absl::string_view message, |
906 | | absl::SourceLocation loc = SourceLocation::current()); |
907 | | Status InternalError(absl::string_view message, |
908 | | absl::SourceLocation loc = SourceLocation::current()); |
909 | | Status InvalidArgumentError( |
910 | | absl::string_view message, |
911 | | absl::SourceLocation loc = SourceLocation::current()); |
912 | | Status NotFoundError(absl::string_view message, |
913 | | absl::SourceLocation loc = SourceLocation::current()); |
914 | | Status OutOfRangeError(absl::string_view message, |
915 | | absl::SourceLocation loc = SourceLocation::current()); |
916 | | Status PermissionDeniedError( |
917 | | absl::string_view message, |
918 | | absl::SourceLocation loc = SourceLocation::current()); |
919 | | Status ResourceExhaustedError( |
920 | | absl::string_view message, |
921 | | absl::SourceLocation loc = SourceLocation::current()); |
922 | | Status UnauthenticatedError( |
923 | | absl::string_view message, |
924 | | absl::SourceLocation loc = SourceLocation::current()); |
925 | | Status UnavailableError(absl::string_view message, |
926 | | absl::SourceLocation loc = SourceLocation::current()); |
927 | | Status UnimplementedError(absl::string_view message, |
928 | | absl::SourceLocation loc = SourceLocation::current()); |
929 | | Status UnknownError(absl::string_view message, |
930 | | absl::SourceLocation loc = SourceLocation::current()); |
931 | | |
932 | | // ErrnoToStatusCode() |
933 | | // |
934 | | // Returns the StatusCode for `error_number`, which should be an `errno` value. |
935 | | // See https://en.cppreference.com/w/cpp/error/errno_macros and similar |
936 | | // references. |
937 | | absl::StatusCode ErrnoToStatusCode(int error_number); |
938 | | |
939 | | // ErrnoToStatus() |
940 | | // |
941 | | // Convenience function that creates a `absl::Status` using an `error_number`, |
942 | | // which should be an `errno` value. |
943 | | Status ErrnoToStatus(int error_number, absl::string_view message, |
944 | | absl::SourceLocation loc = SourceLocation::current()); |
945 | | |
946 | | //------------------------------------------------------------------------------ |
947 | | // Implementation details follow |
948 | | //------------------------------------------------------------------------------ |
949 | | |
950 | 480k | inline Status::Status() : Status(absl::StatusCode::kOk) {} |
951 | | |
952 | 480k | inline Status::Status(absl::StatusCode code) : Status(CodeToInlinedRep(code)) {} |
953 | | |
954 | | inline Status::Status(absl::StatusCode code, absl::string_view msg, |
955 | | absl::SourceLocation loc) |
956 | 6.68k | : Status(MakeRepFromStringView(CodeToInlinedRep(code), msg, loc)) {} |
957 | | |
958 | | #ifndef SWIG |
959 | | template <typename String, typename> |
960 | | inline Status::Status(absl::StatusCode code, String&& msg, |
961 | | absl::SourceLocation loc) |
962 | 0 | : Status(MakeRepFromStringRvalue(CodeToInlinedRep(code), |
963 | 0 | std::forward<String>(msg), loc)) {} |
964 | | #endif // SWIG |
965 | | |
966 | 275k | inline Status::Status(const Status& x) : Status(x.rep_) { Ref(rep_); } |
967 | | |
968 | 0 | inline Status& Status::operator=(const Status& x) { |
969 | 0 | uintptr_t old_rep = rep_; |
970 | 0 | if (x.rep_ != old_rep) { |
971 | 0 | Ref(x.rep_); |
972 | 0 | rep_ = x.rep_; |
973 | 0 | Unref(old_rep); |
974 | 0 | } |
975 | 0 | return *this; |
976 | 0 | } |
977 | | |
978 | | #ifndef SWIG |
979 | 466k | inline Status::Status(Status&& x) noexcept : Status(x.rep_) { |
980 | 466k | x.rep_ = MovedFromRep(); |
981 | 466k | } |
982 | | |
983 | 46.8k | inline Status& Status::operator=(Status&& x) noexcept { |
984 | 46.8k | uintptr_t old_rep = rep_; |
985 | 46.8k | if (x.rep_ != old_rep) { |
986 | 72 | rep_ = x.rep_; |
987 | 72 | x.rep_ = MovedFromRep(); |
988 | 72 | Unref(old_rep); |
989 | 72 | } |
990 | 46.8k | return *this; |
991 | 46.8k | } |
992 | | #endif // SWIG |
993 | | |
994 | | inline void Status::Update(const Status& new_status) { |
995 | | if (ok()) { |
996 | | *this = new_status; |
997 | | } |
998 | | } |
999 | | |
1000 | | #ifndef SWIG |
1001 | | inline void Status::Update(Status&& new_status) { |
1002 | | if (ok()) { |
1003 | | *this = std::move(new_status); |
1004 | | } |
1005 | | } |
1006 | | #endif // SWIG |
1007 | | |
1008 | | inline Status::~Status() { Unref(rep_); } |
1009 | | |
1010 | 733k | inline bool Status::ok() const { |
1011 | 733k | return rep_ == CodeToInlinedRep(absl::StatusCode::kOk); |
1012 | 733k | } |
1013 | | |
1014 | | inline absl::StatusCode Status::code() const { |
1015 | | return status_internal::MapToLocalCode(raw_code()); |
1016 | | } |
1017 | | |
1018 | | inline int Status::raw_code() const { |
1019 | | if (IsInlined(rep_)) return static_cast<int>(InlinedRepToCode(rep_)); |
1020 | | return static_cast<int>(RepToPointer(rep_)->code()); |
1021 | | } |
1022 | | |
1023 | 6.64k | inline absl::string_view Status::message() const { |
1024 | 6.64k | return !IsInlined(rep_) |
1025 | 6.64k | ? RepToPointer(rep_)->message() |
1026 | 6.64k | : (IsMovedFrom(rep_) ? absl::string_view(kMovedFromString) |
1027 | 6.64k | : absl::string_view()); |
1028 | 6.64k | } |
1029 | | |
1030 | 0 | inline bool operator==(const Status& lhs, const Status& rhs) { |
1031 | 0 | if (lhs.rep_ == rhs.rep_) return true; |
1032 | 0 | if (Status::IsInlined(lhs.rep_)) return false; |
1033 | 0 | if (Status::IsInlined(rhs.rep_)) return false; |
1034 | 0 | return *Status::RepToPointer(lhs.rep_) == *Status::RepToPointer(rhs.rep_); |
1035 | 0 | } |
1036 | | |
1037 | 0 | inline bool operator!=(const Status& lhs, const Status& rhs) { |
1038 | 0 | return !(lhs == rhs); |
1039 | 0 | } |
1040 | | |
1041 | | inline std::string Status::ToString(StatusToStringMode mode) const { |
1042 | | return ok() ? "OK" : ToStringSlow(rep_, mode); |
1043 | | } |
1044 | | |
1045 | 74.9k | inline void Status::IgnoreError() const { |
1046 | | // no-op |
1047 | 74.9k | } |
1048 | | |
1049 | | #ifndef SWIG |
1050 | | inline void swap(absl::Status& a, absl::Status& b) noexcept { |
1051 | | using std::swap; |
1052 | | swap(a.rep_, b.rep_); |
1053 | | } |
1054 | | #else |
1055 | | inline void swap(absl::Status& a, absl::Status& b) { |
1056 | | using std::swap; |
1057 | | swap(a.rep_, b.rep_); |
1058 | | } |
1059 | | #endif // SWIG |
1060 | | |
1061 | | inline std::optional<absl::Cord> Status::GetPayload( |
1062 | | absl::string_view type_url) const { |
1063 | | if (IsInlined(rep_)) return std::nullopt; |
1064 | | return RepToPointer(rep_)->GetPayload(type_url); |
1065 | | } |
1066 | | |
1067 | | inline void Status::SetPayload(absl::string_view type_url, absl::Cord payload) { |
1068 | | if (ok()) return; |
1069 | | status_internal::StatusRep* rep = PrepareToModify(rep_); |
1070 | | rep->SetPayload(type_url, std::move(payload)); |
1071 | | rep_ = PointerToRep(rep); |
1072 | | } |
1073 | | |
1074 | | inline bool Status::ErasePayload(absl::string_view type_url) { |
1075 | | if (IsInlined(rep_)) return false; |
1076 | | status_internal::StatusRep* rep = PrepareToModify(rep_); |
1077 | | auto res = rep->ErasePayload(type_url); |
1078 | | rep_ = res.new_rep; |
1079 | | return res.erased; |
1080 | | } |
1081 | | |
1082 | | inline void Status::ForEachPayload( |
1083 | | absl::FunctionRef<void(absl::string_view, const absl::Cord&)> visitor) |
1084 | | const { |
1085 | | if (IsInlined(rep_)) return; |
1086 | | RepToPointer(rep_)->ForEachPayload(visitor); |
1087 | | } |
1088 | | |
1089 | | constexpr bool Status::IsInlined(uintptr_t rep) { return (rep & 1) != 0; } |
1090 | | |
1091 | 6.64k | constexpr bool Status::IsMovedFrom(uintptr_t rep) { return (rep & 2) != 0; } |
1092 | | |
1093 | 1.69M | constexpr uintptr_t Status::CodeToInlinedRep(absl::StatusCode code) { |
1094 | 1.69M | return (static_cast<uintptr_t>(code) << 2) + 1; |
1095 | 1.69M | } |
1096 | | |
1097 | | constexpr absl::StatusCode Status::InlinedRepToCode(uintptr_t rep) { |
1098 | | ABSL_ASSERT(IsInlined(rep)); |
1099 | | return static_cast<absl::StatusCode>(rep >> 2); |
1100 | | } |
1101 | | |
1102 | 466k | constexpr uintptr_t Status::MovedFromRep() { |
1103 | 466k | return CodeToInlinedRep(absl::StatusCode::kInternal) | 2; |
1104 | 466k | } |
1105 | | |
1106 | | inline const status_internal::StatusRep* absl_nonnull Status::RepToPointer( |
1107 | | uintptr_t rep) { |
1108 | | assert(!IsInlined(rep)); |
1109 | | return reinterpret_cast<const status_internal::StatusRep*>(rep); |
1110 | | } |
1111 | | |
1112 | | inline uintptr_t Status::PointerToRep( |
1113 | | status_internal::StatusRep* absl_nonnull rep) { |
1114 | | return reinterpret_cast<uintptr_t>(rep); |
1115 | | } |
1116 | | |
1117 | 275k | inline void Status::Ref(uintptr_t rep) { |
1118 | 275k | if (!IsInlined(rep)) RepToPointer(rep)->Ref(); |
1119 | 275k | } |
1120 | | |
1121 | | inline void Status::Unref(uintptr_t rep) { |
1122 | | if (!IsInlined(rep)) RepToPointer(rep)->Unref(); |
1123 | | } |
1124 | | |
1125 | | #ifndef SWIG |
1126 | 460k | inline Status OkStatus() { return Status(); } |
1127 | | #endif // SWIG |
1128 | | |
1129 | | // Creates a `Status` object with the `absl::StatusCode::kCancelled` error code |
1130 | | // and an empty message. It is provided only for efficiency, given that |
1131 | | // message-less kCancelled errors are common in the infrastructure. |
1132 | | inline Status CancelledError() { return Status(absl::StatusCode::kCancelled); } |
1133 | | |
1134 | | // Retrieves a message's status as a null terminated C string. The lifetime of |
1135 | | // this string is tied to the lifetime of the status object itself. |
1136 | | // |
1137 | | // If the status's message is empty, the empty string is returned. |
1138 | | // |
1139 | | // StatusMessageAsCStr exists for C support. Use `status.message()` in C++. |
1140 | | const char* absl_nonnull StatusMessageAsCStr( |
1141 | | const Status& status ABSL_ATTRIBUTE_LIFETIME_BOUND); |
1142 | | |
1143 | | namespace status_internal { |
1144 | | // We use an int in the template parameter to shorten mangled names. |
1145 | | template <int error_code> |
1146 | | Status MakeErrorImpl(string_view message, SourceLocation loc); |
1147 | | // Make the instantiations extern to reduce bloat on callers. |
1148 | | #ifndef SWIG |
1149 | | extern template Status MakeErrorImpl<0>(string_view, SourceLocation); |
1150 | | extern template Status MakeErrorImpl<1>(string_view, SourceLocation); |
1151 | | extern template Status MakeErrorImpl<2>(string_view, SourceLocation); |
1152 | | extern template Status MakeErrorImpl<3>(string_view, SourceLocation); |
1153 | | extern template Status MakeErrorImpl<4>(string_view, SourceLocation); |
1154 | | extern template Status MakeErrorImpl<5>(string_view, SourceLocation); |
1155 | | extern template Status MakeErrorImpl<6>(string_view, SourceLocation); |
1156 | | extern template Status MakeErrorImpl<7>(string_view, SourceLocation); |
1157 | | extern template Status MakeErrorImpl<8>(string_view, SourceLocation); |
1158 | | extern template Status MakeErrorImpl<9>(string_view, SourceLocation); |
1159 | | extern template Status MakeErrorImpl<10>(string_view, SourceLocation); |
1160 | | extern template Status MakeErrorImpl<11>(string_view, SourceLocation); |
1161 | | extern template Status MakeErrorImpl<12>(string_view, SourceLocation); |
1162 | | extern template Status MakeErrorImpl<13>(string_view, SourceLocation); |
1163 | | extern template Status MakeErrorImpl<14>(string_view, SourceLocation); |
1164 | | extern template Status MakeErrorImpl<15>(string_view, SourceLocation); |
1165 | | extern template Status MakeErrorImpl<16>(string_view, SourceLocation); |
1166 | | #endif // SWIG |
1167 | | |
1168 | | template <StatusCode error_code> |
1169 | 33 | Status MakeError(string_view message, SourceLocation loc) { |
1170 | 33 | Status out = MakeErrorImpl<static_cast<int>(error_code)>(message, loc); |
1171 | | // -Wassume warning complains about potential side effects of `ok()`, so use a |
1172 | | // local to avoid that. |
1173 | 33 | [[maybe_unused]] bool ok = out.ok(); |
1174 | 33 | ABSL_ASSUME(!ok); |
1175 | 33 | return out; |
1176 | 33 | } absl::lts_20260817::Status absl::lts_20260817::status_internal::MakeError<(absl::lts_20260817::StatusCode)13>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, std::__1::source_location) Line | Count | Source | 1169 | 33 | Status MakeError(string_view message, SourceLocation loc) { | 1170 | 33 | Status out = MakeErrorImpl<static_cast<int>(error_code)>(message, loc); | 1171 | | // -Wassume warning complains about potential side effects of `ok()`, so use a | 1172 | | // local to avoid that. | 1173 | 33 | [[maybe_unused]] bool ok = out.ok(); | 1174 | 33 | ABSL_ASSUME(!ok); | 1175 | 33 | return out; | 1176 | 33 | } |
Unexecuted instantiation: absl::lts_20260817::Status absl::lts_20260817::status_internal::MakeError<(absl::lts_20260817::StatusCode)3>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, std::__1::source_location) Unexecuted instantiation: absl::lts_20260817::Status absl::lts_20260817::status_internal::MakeError<(absl::lts_20260817::StatusCode)5>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, std::__1::source_location) |
1177 | | } // namespace status_internal |
1178 | | |
1179 | | // Inline implementations to give the compiler static knowledge about the |
1180 | | // objects. |
1181 | | inline Status AbortedError(absl::string_view message, |
1182 | | absl::SourceLocation loc) { |
1183 | | return status_internal::MakeError<StatusCode::kAborted>(message, loc); |
1184 | | } |
1185 | | inline Status AlreadyExistsError(absl::string_view message, |
1186 | | absl::SourceLocation loc) { |
1187 | | return status_internal::MakeError<StatusCode::kAlreadyExists>(message, loc); |
1188 | | } |
1189 | | inline Status CancelledError(absl::string_view message, |
1190 | | absl::SourceLocation loc) { |
1191 | | return status_internal::MakeError<StatusCode::kCancelled>(message, loc); |
1192 | | } |
1193 | | inline Status DataLossError(absl::string_view message, |
1194 | | absl::SourceLocation loc) { |
1195 | | return status_internal::MakeError<StatusCode::kDataLoss>(message, loc); |
1196 | | } |
1197 | | inline Status DeadlineExceededError(absl::string_view message, |
1198 | | absl::SourceLocation loc) { |
1199 | | return status_internal::MakeError<StatusCode::kDeadlineExceeded>(message, |
1200 | | loc); |
1201 | | } |
1202 | | inline Status FailedPreconditionError(absl::string_view message, |
1203 | | absl::SourceLocation loc) { |
1204 | | return status_internal::MakeError<StatusCode::kFailedPrecondition>(message, |
1205 | | loc); |
1206 | | } |
1207 | | inline Status InternalError(absl::string_view message, |
1208 | 33 | absl::SourceLocation loc) { |
1209 | 33 | return status_internal::MakeError<StatusCode::kInternal>(message, loc); |
1210 | 33 | } |
1211 | | inline Status InvalidArgumentError(absl::string_view message, |
1212 | 0 | absl::SourceLocation loc) { |
1213 | 0 | return status_internal::MakeError<StatusCode::kInvalidArgument>(message, loc); |
1214 | 0 | } |
1215 | | inline Status NotFoundError(absl::string_view message, |
1216 | 0 | absl::SourceLocation loc) { |
1217 | 0 | return status_internal::MakeError<StatusCode::kNotFound>(message, loc); |
1218 | 0 | } |
1219 | | inline Status OutOfRangeError(absl::string_view message, |
1220 | | absl::SourceLocation loc) { |
1221 | | return status_internal::MakeError<StatusCode::kOutOfRange>(message, loc); |
1222 | | } |
1223 | | inline Status PermissionDeniedError(absl::string_view message, |
1224 | | absl::SourceLocation loc) { |
1225 | | return status_internal::MakeError<StatusCode::kPermissionDenied>(message, |
1226 | | loc); |
1227 | | } |
1228 | | inline Status ResourceExhaustedError(absl::string_view message, |
1229 | | absl::SourceLocation loc) { |
1230 | | return status_internal::MakeError<StatusCode::kResourceExhausted>(message, |
1231 | | loc); |
1232 | | } |
1233 | | inline Status UnauthenticatedError(absl::string_view message, |
1234 | | absl::SourceLocation loc) { |
1235 | | return status_internal::MakeError<StatusCode::kUnauthenticated>(message, loc); |
1236 | | } |
1237 | | inline Status UnavailableError(absl::string_view message, |
1238 | | absl::SourceLocation loc) { |
1239 | | return status_internal::MakeError<StatusCode::kUnavailable>(message, loc); |
1240 | | } |
1241 | | inline Status UnimplementedError(absl::string_view message, |
1242 | | absl::SourceLocation loc) { |
1243 | | return status_internal::MakeError<StatusCode::kUnimplemented>(message, loc); |
1244 | | } |
1245 | | inline Status UnknownError(absl::string_view message, |
1246 | | absl::SourceLocation loc) { |
1247 | | return status_internal::MakeError<StatusCode::kUnknown>(message, loc); |
1248 | | } |
1249 | | |
1250 | | ABSL_NAMESPACE_END |
1251 | | } // namespace absl |
1252 | | |
1253 | | #endif // ABSL_STATUS_STATUS_H_ |