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