A type used to gracefully handle errors across API boundaries.
Declared in <absl/status/status.h>
class [[nodiscard]] Status final
The absl::Status class is generally used to gracefully handle errors across API boundaries (and in particular across RPC boundaries). Some of these errors may be recoverable, but others may not. Most functions which can produce a recoverable error should be designed to return either an absl::Status (or the similar absl::StatusOr<T>, which holds either an object of type T or an error).
API developers should construct their functions to return absl::OkStatus() upon success, or an absl::StatusCode upon another type of error (e.g an absl::StatusCode::kInvalidArgument error). The API provides convenience functions to construct each status code.
Example:
absl::Status myFunction(absl::string_view fname, ...) { ... // encounter error if (error condition) { // Construct an absl::StatusCode::kInvalidArgument error return absl::InvalidArgumentError("bad mode"); } // else, return OK return absl::OkStatus(); }
Users handling status error codes should prefer checking for an OK status using the ok() member function. Handling multiple error codes may justify use of switch statement, but only check for error codes you know how to handle; do not try to exhaustively match against all canonical error codes. Errors that cannot be handled should be logged and/or propagated for higher levels to deal with. If you do use a switch statement, make sure that you also provide a default: switch case, so that code does not break as other canonical codes are added to the API.
Example:
absl::Status result = DoSomething(); if (!result.ok()) { LOG(ERROR) << result; }
// Provide a default if switching on multiple error codes switch (result.code()) { // The user hasn't authenticated. Ask them to reauth case absl::StatusCode::kUnauthenticated: DoReAuth(); break; // The user does not have permission. Log an error. case absl::StatusCode::kPermissionDenied: LOG(ERROR) << result; break; // Propagate the error otherwise. default: return true; }
An absl::Status can optionally include a payload with more information about the error. Typically, this payload serves one of several purposes:
* It may provide more fine-grained semantic information about the error to facilitate actionable remedies. * It may provide human-readable contextual information that is more appropriate to display to an end user.
Example:
absl::Status result = DoSomething(); // Inform user to retry after 30 seconds // See more error details in googleapis/google/rpc/error_details.proto if (absl::IsResourceExhausted(result)) { google::rpc::RetryInfo info; info.retry_delay().seconds() = 30; // Payloads require a unique key (a URL to ensure no collisions with // other payloads), and an absl::Cord to hold the encoded data. absl::string_view url = "type.googleapis.com/google.rpc.RetryInfo"; result.SetPayload(url, info.SerializeAsCord()); return result; }
For documentation see https://abseil.io/docs/cpp/guides/status.
Returned Status objects may not be ignored. status_internal.h has a forward declaration of the form class ABSL_MUST_USE_RESULT Status;
| Name | Description |
|---|---|
Status [constructor] | Constructors |
~Status [destructor] | Destroys the status and releases any owned resources. |
operator= | Assignment operators |
AddSourceLocation | Appends the loc to the current location chain inside the status, iff the status is non-ok and contains a non-empty message. |
ErasePayload | Erases the payload corresponding to the type_url key. |
ForEachPayload | Iterates over the stored payloads and calls the visitor(type_key, payload) callable for each one. |
GetPayload | Gets the payload of a status given its unique type_url key, if present. |
GetSourceLocations | Returns the source locations attached to this status. |
IgnoreError | Ignores any errors. |
SetPayload | Sets the payload for a non-ok status using a type_url key, overwriting any existing payload for that type_url. |
ToString | Returns a string based on the mode. |
Update | Update overloads |
WithSourceLocation | WithSourceLocation overloads |
code | Returns the canonical error code of type absl::StatusCode of this status. |
message | Returns the error message associated with this error code, if available. |
ok | Returns true if this->code() == absl::StatusCode::kOk, indicating the absence of an error. |
raw_code | Returns a raw (canonical) error code corresponding to the enum value of google.rpc.Code definitions within https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto. These values could be out of the range of canonical absl::StatusCode enum values. |
| Name | Description |
|---|---|
absl::status_internal::StatusRep | |
absl::MakeStatusRepImpl | Builds a status representation from an inlined rep and a message. |
absl::StatusOr | A union of an object of type T and an absl::Status. |
absl::status_internal::StatusPrivateAccessorForStatusBuilder | |
absl::status_internal::StatusPrivateAccessor | |
absl::CancelledError | Creates a Status object with the absl::StatusCode::kCancelled error code and an empty message. |
absl::swap | Swaps the contents of one status with another. |
absl::AbslStringify | Support absl::StrCat, absl::StrFormat, etc. |
absl::operator!= | Compares two statuses for inequality. |
absl::operator== | Compares two statuses for equality. |
| Name | Description |
|---|---|
AbortedError | Creates a status with the kAborted error code and message. |
AbortedError | Creates a status with the kAborted error code and message. |
AlreadyExistsError | Creates a status with the kAlreadyExists error code and message. |
AlreadyExistsError | Creates a status with the kAlreadyExists error code and message. |
CancelledError | Creates a status with the kCancelled error code and message. |
CancelledError | Creates a status with the kCancelled error code and message. |
DataLossError | Creates a status with the kDataLoss error code and message. |
DataLossError | Creates a status with the kDataLoss error code and message. |
DeadlineExceededError | Creates a status with the kDeadlineExceeded error code and message. |
DeadlineExceededError | Creates a status with the kDeadlineExceeded error code and message. |
ErrnoToStatus | Convenience function that creates a absl::Status using an error_number, which should be an errno value. |
FailedPreconditionError | Creates a status with the kFailedPrecondition error code and message. |
FailedPreconditionError | Creates a status with the kFailedPrecondition error code and message. |
InternalError | Creates a status with the kInternal error code and message. |
InternalError | Creates a status with the kInternal error code and message. |
InvalidArgumentError | Creates a status with the kInvalidArgument error code and message. |
InvalidArgumentError | Creates a status with the kInvalidArgument error code and message. |
IsAborted | Returns true if status has the kAborted error code. |
IsAlreadyExists | Returns true if status has the kAlreadyExists error code. |
IsCancelled | Returns true if status has the kCancelled error code. |
IsDataLoss | Returns true if status has the kDataLoss error code. |
IsDeadlineExceeded | Returns true if status has the kDeadlineExceeded error code. |
IsFailedPrecondition | Returns true if status has the kFailedPrecondition error code. |
IsInternal | Returns true if status has the kInternal error code. |
IsInvalidArgument | Returns true if status has the kInvalidArgument error code. |
IsNotFound | Returns true if status has the kNotFound error code. |
IsOutOfRange | Returns true if status has the kOutOfRange error code. |
IsPermissionDenied | Returns true if status has the kPermissionDenied error code. |
IsResourceExhausted | Returns true if status has the kResourceExhausted error code. |
IsUnauthenticated | Returns true if status has the kUnauthenticated error code. |
IsUnavailable | Returns true if status has the kUnavailable error code. |
IsUnimplemented | Returns true if status has the kUnimplemented error code. |
IsUnknown | Returns true if status has the kUnknown error code. |
NotFoundError | Creates a status with the kNotFound error code and message. |
NotFoundError | Creates a status with the kNotFound error code and message. |
OkStatus | Returns an OK status, equivalent to a default constructed instance. |
OutOfRangeError | Creates a status with the kOutOfRange error code and message. |
OutOfRangeError | Creates a status with the kOutOfRange error code and message. |
PermissionDeniedError | Creates a status with the kPermissionDenied error code and message. |
PermissionDeniedError | Creates a status with the kPermissionDenied error code and message. |
ResourceExhaustedError | Creates a status with the kResourceExhausted error code and message. |
ResourceExhaustedError | Creates a status with the kResourceExhausted error code and message. |
StatusMessageAsCStr | Retrieves a message's status as a null terminated C string. |
UnauthenticatedError | Creates a status with the kUnauthenticated error code and message. |
UnauthenticatedError | Creates a status with the kUnauthenticated error code and message. |
UnavailableError | Creates a status with the kUnavailable error code and message. |
UnavailableError | Creates a status with the kUnavailable error code and message. |
UnimplementedError | Creates a status with the kUnimplemented error code and message. |
UnimplementedError | Creates a status with the kUnimplemented error code and message. |
UnknownError | Creates a status with the kUnknown error code and message. |
UnknownError | Creates a status with the kUnknown error code and message. |
The return value should not be discarded.