class Status
Defined at line 442 of file ../../third_party/abseil-cpp/src/absl/status/status.h
absl::Status
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;
Public Methods
void Status (const Status & base_status, absl::SourceLocation loc)
Create a status from a `base_status` and a `loc`. The `loc` will be
appended to the location chain of the new status, iff the `base_status` is
not ok and has non-empty msg.
Defined at line 474 of file ../../third_party/abseil-cpp/src/absl/status/status.h
void Status (Status && base_status, absl::SourceLocation loc)
Defined at line 478 of file ../../third_party/abseil-cpp/src/absl/status/status.h
absl::Span<const absl::SourceLocation> GetSourceLocations ()
Defined at line 646 of file ../../third_party/abseil-cpp/src/absl/status/status.h
void AddSourceLocation (absl::SourceLocation loc)
Appends the `loc` to the current location chain inside the status, iff the
status is non-ok and contains a non-empty message.
Defined at line 652 of file ../../third_party/abseil-cpp/src/absl/status/status.h
Status WithSourceLocation (absl::SourceLocation loc)
Status::WithSourceLocation()
Returns a copy of the current status, with `loc` appended to its location
chain iff the status is non-ok and contains a non-empty message.
Example:
if (Status status = Foo(); !status.ok()) {
return status.WithSourceLocation();
}
Defined at line 674 of file ../../third_party/abseil-cpp/src/absl/status/status.h
Status && WithSourceLocation (absl::SourceLocation loc)
Status::WithSourceLocation()
Appends the `loc` to the current location chain inside the status iff the
status is non-ok and contains a non-empty message, and returns an rvalue
reference to `*this`.
Example:
Status Finalize(...);
Status DoSomething(...) {
...
return Finalize().WithSourceLocation();
}
Defined at line 693 of file ../../third_party/abseil-cpp/src/absl/status/status.h
void Status ()
This default constructor creates an OK status with no message or payload.
Avoid this constructor and prefer explicit construction of an OK status
with `absl::OkStatus()`.
Defined at line 915 of file ../../third_party/abseil-cpp/src/absl/status/status.h
void Status (absl::StatusCodecode,absl::string_viewmsg,absl::SourceLocationloc)
Creates a status in the canonical error space with the specified
`absl::StatusCode` and error message. If `code == absl::StatusCode::kOk`,
`msg` is ignored and an object identical to an OK status is constructed.
The `msg` string must be in UTF-8. The implementation may complain (e.g.,
by printing a warning) if it is not.
The `loc` is the SourceLocation of the callsite. It will be stored in the
Status iff `code != absl::StatusCode::kOk` and `!msg.empty()`.
Defined at line 919 of file ../../third_party/abseil-cpp/src/absl/status/status.h
template <typename String, typename = std::enable_if_t<std::is_same_v<String, std::string>>>
void Status (absl::StatusCodecode,String &&msg,absl::SourceLocationloc)
Same as above but for rvalue string.
Note: using a template to disambiguate the case of matching string_view and
string
&
&
(e.g. char*) as a template lowers the priority of the overload.
Defined at line 924 of file ../../third_party/abseil-cpp/src/absl/status/status.h
void Status (const Status & )
Defined at line 929 of file ../../third_party/abseil-cpp/src/absl/status/status.h
Status & operator= (const Status & x)
Defined at line 931 of file ../../third_party/abseil-cpp/src/absl/status/status.h
void Status (Status && )
The moved-from state is valid but unspecified.
Defined at line 941 of file ../../third_party/abseil-cpp/src/absl/status/status.h
Status & operator= (Status && )
Defined at line 945 of file ../../third_party/abseil-cpp/src/absl/status/status.h
void ~Status ()
Defined at line 967 of file ../../third_party/abseil-cpp/src/absl/status/status.h
void Update (const Status & new_status)
Status::Update()
Updates the existing status with `new_status` provided that `this->ok()`.
If the existing status already contains a non-OK error, this update has no
effect and preserves the current data. Note that this behavior may change
in the future to augment a current non-ok status with additional
information about `new_status`.
`Update()` provides a convenient way of keeping track of the first error
encountered.
Example:
// Instead of "if (overall_status.ok()) overall_status = new_status"
overall_status.Update(new_status);
Defined at line 955 of file ../../third_party/abseil-cpp/src/absl/status/status.h
void Update (Status && new_status)
Defined at line 961 of file ../../third_party/abseil-cpp/src/absl/status/status.h
bool ok ()
Status::ok()
Returns `true` if `this->code()` == `absl::StatusCode::kOk`,
indicating the absence of an error.
Prefer checking for an OK status using this member function.
Defined at line 969 of file ../../third_party/abseil-cpp/src/absl/status/status.h
absl::StatusCode code ()
Status::code()
Returns the canonical error code of type `absl::StatusCode` of this status.
Defined at line 973 of file ../../third_party/abseil-cpp/src/absl/status/status.h
int raw_code ()
Status::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.
NOTE: This function should only be called when converting to an associated
wire format. Use `Status::code()` for error handling.
Defined at line 977 of file ../../third_party/abseil-cpp/src/absl/status/status.h
absl::string_view message ()
Status::message()
Returns the error message associated with this error code, if available.
Note that this message rarely describes the error code. It is not unusual
for the error message to be the empty string. As a result, prefer
`operator
<
<
` or `Status::ToString()` for debug logging.
Defined at line 982 of file ../../third_party/abseil-cpp/src/absl/status/status.h
std::string ToString (StatusToStringMode mode)
Status::ToString()
Returns a string based on the `mode`. By default, it returns combination of
the error code name, the message and any associated payload messages. This
string is designed simply to be human readable and its exact format should
not be load bearing. Do not depend on the exact format of the result of
`ToString()` which is subject to change.
The printed code name and the message are generally substrings of the
result, and the payloads to be printed use the status payload printer
mechanism (which is internal).
Defined at line 1000 of file ../../third_party/abseil-cpp/src/absl/status/status.h
void IgnoreError ()
Status::IgnoreError()
Ignores any errors. This method does nothing except potentially suppress
complaints from any tools that are checking that errors are not dropped on
the floor.
Defined at line 1004 of file ../../third_party/abseil-cpp/src/absl/status/status.h
std::optional<absl::Cord> GetPayload (absl::string_view type_url)
Status::GetPayload()
Gets the payload of a status given its unique `type_url` key, if present.
Defined at line 1013 of file ../../third_party/abseil-cpp/src/absl/status/status.h
void SetPayload (absl::string_view type_url, absl::Cord payload)
Status::SetPayload()
Sets the payload for a non-ok status using a `type_url` key, overwriting
any existing payload for that `type_url`.
NOTE: This function does nothing if the Status is ok.
Defined at line 1019 of file ../../third_party/abseil-cpp/src/absl/status/status.h
bool ErasePayload (absl::string_view type_url)
Status::ErasePayload()
Erases the payload corresponding to the `type_url` key. Returns `true` if
the payload was present.
Defined at line 1026 of file ../../third_party/abseil-cpp/src/absl/status/status.h
void ForEachPayload (absl::FunctionRef<void (absl::string_view, const absl::Cord &)> visitor)
Status::ForEachPayload()
Iterates over the stored payloads and calls the
`visitor(type_key, payload)` callable for each one.
NOTE: The order of calls to `visitor()` is not specified and may change at
any time.
NOTE: Any mutation on the same 'absl::Status' object during visitation is
forbidden and could result in undefined behavior.
Defined at line 1034 of file ../../third_party/abseil-cpp/src/absl/status/status.h
Friends
class StatusRep
template <typename StringOrView>
uintptr_t Status (uintptr_t inlined_rep, StringOrView msg, absl::SourceLocation loc)
template <typename T>
class StatusOr
class StatusPrivateAccessorForStatusBuilder
class StatusPrivateAccessor
Status Status ()
void Status (Status & a, Status & b)
template <typename Sink>
void Status (Sink & sink, const Status & status)
bool Status (const Status & , const Status & )
bool Status (const Status & const Status & )