Abseil Status

By Xiaoyi Zhang, Google Engineer, Emeritus

The Abseil status library is now available on abseil.io. This library is used within Google for error handling and contains the following two abstractions:

  • absl::Status
  • absl::StatusOr<T>

Within Google, absl::Status is the primary mechanism 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 that 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).

A Status can either return “OK” (represented by the enumumerated value absl::StatusCode::kOk) or one of a number of canonical error codes to indicate certain error conditions:

absl::Status MyFunction(absl::string_view filename, ...) {
  // encounter error
  if (error condition) {
    // absl::StatusCode::kInvalidArgument
    return absl::InvalidArgumentError("bad mode");
  // else, return OK
  return absl::OkStatus();

Abseil also provides an absl::StatusOr<T> class template to represent a union of an absl::Status object and an object of type T. The absl::StatusOr<T> will either contain an object of type T (indicating a successful operation), or an error (of type absl::Status) explaining why such a value is not present. This abstraction is similar to the proposal in C++ for a std::expected type.

For more information, consult the Abseil Status guide and the list of canonical error codes.

Subscribe to the Abseil Blog