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