absl::Status

A type used to gracefully handle errors across API boundaries.

Synopsis

Declared in <absl/status/status.h>

class [[nodiscard]] Status final

Description

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;

Member Functions

NameDescription
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.

Friends

NameDescription
absl::status_internal::StatusRep
absl::MakeStatusRepImplBuilds a status representation from an inlined rep and a message.
absl::StatusOrA union of an object of type T and an absl::Status.
absl::status_internal::StatusPrivateAccessorForStatusBuilder
absl::status_internal::StatusPrivateAccessor
absl::CancelledErrorCreates a Status object with the absl::StatusCode::kCancelled error code and an empty message.
absl::swapSwaps the contents of one status with another.
absl::AbslStringifySupport absl::StrCat, absl::StrFormat, etc.
absl::operator!=Compares two statuses for inequality.
absl::operator==Compares two statuses for equality.

Non-Member Functions

NameDescription
AbortedErrorCreates a status with the kAborted error code and message.
AbortedErrorCreates a status with the kAborted error code and message.
AlreadyExistsErrorCreates a status with the kAlreadyExists error code and message.
AlreadyExistsErrorCreates a status with the kAlreadyExists error code and message.
CancelledErrorCreates a status with the kCancelled error code and message.
CancelledErrorCreates a status with the kCancelled error code and message.
DataLossErrorCreates a status with the kDataLoss error code and message.
DataLossErrorCreates a status with the kDataLoss error code and message.
DeadlineExceededErrorCreates a status with the kDeadlineExceeded error code and message.
DeadlineExceededErrorCreates a status with the kDeadlineExceeded error code and message.
ErrnoToStatusConvenience function that creates a absl::Status using an error_number, which should be an errno value.
FailedPreconditionErrorCreates a status with the kFailedPrecondition error code and message.
FailedPreconditionErrorCreates a status with the kFailedPrecondition error code and message.
InternalErrorCreates a status with the kInternal error code and message.
InternalErrorCreates a status with the kInternal error code and message.
InvalidArgumentErrorCreates a status with the kInvalidArgument error code and message.
InvalidArgumentErrorCreates a status with the kInvalidArgument error code and message.
IsAbortedReturns true if status has the kAborted error code.
IsAlreadyExistsReturns true if status has the kAlreadyExists error code.
IsCancelledReturns true if status has the kCancelled error code.
IsDataLossReturns true if status has the kDataLoss error code.
IsDeadlineExceededReturns true if status has the kDeadlineExceeded error code.
IsFailedPreconditionReturns true if status has the kFailedPrecondition error code.
IsInternalReturns true if status has the kInternal error code.
IsInvalidArgumentReturns true if status has the kInvalidArgument error code.
IsNotFoundReturns true if status has the kNotFound error code.
IsOutOfRangeReturns true if status has the kOutOfRange error code.
IsPermissionDeniedReturns true if status has the kPermissionDenied error code.
IsResourceExhaustedReturns true if status has the kResourceExhausted error code.
IsUnauthenticatedReturns true if status has the kUnauthenticated error code.
IsUnavailableReturns true if status has the kUnavailable error code.
IsUnimplementedReturns true if status has the kUnimplemented error code.
IsUnknownReturns true if status has the kUnknown error code.
NotFoundErrorCreates a status with the kNotFound error code and message.
NotFoundErrorCreates a status with the kNotFound error code and message.
OkStatusReturns an OK status, equivalent to a default constructed instance.
OutOfRangeErrorCreates a status with the kOutOfRange error code and message.
OutOfRangeErrorCreates a status with the kOutOfRange error code and message.
PermissionDeniedErrorCreates a status with the kPermissionDenied error code and message.
PermissionDeniedErrorCreates a status with the kPermissionDenied error code and message.
ResourceExhaustedErrorCreates a status with the kResourceExhausted error code and message.
ResourceExhaustedErrorCreates a status with the kResourceExhausted error code and message.
StatusMessageAsCStrRetrieves a message's status as a null terminated C string.
UnauthenticatedErrorCreates a status with the kUnauthenticated error code and message.
UnauthenticatedErrorCreates a status with the kUnauthenticated error code and message.
UnavailableErrorCreates a status with the kUnavailable error code and message.
UnavailableErrorCreates a status with the kUnavailable error code and message.
UnimplementedErrorCreates a status with the kUnimplemented error code and message.
UnimplementedErrorCreates a status with the kUnimplemented error code and message.
UnknownErrorCreates a status with the kUnknown error code and message.
UnknownErrorCreates a status with the kUnknown error code and message.

Return Value

NOTE

The return value should not be discarded.