#include "tensorstore/util/status_builder.h"
class [[warn_unused_result]] tensorstore::StatusBuilder;

StatusBuilder is a builder object for constructing absl::Status objects, with methods to override the status code, augment the error message, and attach payloads.

A StatusBuilder is implicitly convertible to absl::Status, and the absl::Status can be explicitly constructed using the method StatusBuilder::BuildStatus().

Example:

absl::Status foo(int i) {
  if (i < 0) {
    return StatusBuilder(absl::StatusCode::kInvalidArgument)
              .Format("i=%d", i);
  }
  return absl::OkStatus();
}

StatusBuilder is primarily used in the TENSORSTORE_RETURN_IF_ERROR and TENSORSTORE_ASSIGN_OR_RETURN macros.

Constructors

explicit StatusBuilder(
    
absl::Status status,
    
std::source_location loc = std::source_location::current());

Creates a StatusBuilder from an existing status. If the status is not absl::StatusCode::kOk, the source location may be added to it.

explicit StatusBuilder(
    
absl::StatusCode code,
    
std::source_location loc = std::source_location::current());

Creates a StatusBuilder with the given status code and an empty message.

Methods

bool ok() const;

Returns whether the current status code is absl::StatusCode::kOk.

absl::StatusCode code() const;

Returns the status code of the underlying status, or the overridden code if one has been set.

StatusBuilderSetCode(absl::StatusCode code) &;
StatusBuilder&SetCode(absl::StatusCode code) &&;

Overrides the absl::StatusCode on the error. code must not be absl::StatusCode::kOk.

StatusBuilder&
Format(const absl::FormatSpec<Args...>& formatconst Args&... args);

Adds a formatted message to the status.

StatusBuilderSetPrepend();

Mutates the builder so that any formatted message is prepended to the status.

StatusBuilderSetAppend();

Mutates the builder so that any formatted message is appended to the status.

std::optional<absl::Cord> GetPayload(std::string_view type_url) const;

Returns payload for the given type URL, or nullopt if not found.

StatusBuilder&
SetPayload(std::string_view type_urlabsl::Cord payload);
StatusBuilder&
SetPayload(std::string_view type_urlstd::string_view payload);
StatusBuilderSetPayload(std::string_view type_urlT&payload);

Sets a payload on the status equivalent to absl::Status::SetPayload.

StatusBuilder&
AddStatusPayload(std::string_view type_urlabsl::Cord payload);

Adds a payload value to status.

auto With(Adaptor&adaptor) & -> decltype(std::forward<Adaptor>(
    
adaptor)(*this));
auto With(Adaptor&adaptor) && -> decltype(std::forward<Adaptor>(
    
adaptor)(std::move(*this)));

Applies a custom adaptor functor to the StatusBuilder.

__attribute__((warn_unused_result)) absl::Status BuildStatus() const&;
__attribute__((warn_unused_result)) absl::Status BuildStatus() &&;

Constructs an absl::Status from the current state.

Conversion operators

operator absl::Status() const&;
operator absl::Status() &&;

Converts to absl::Status.

absl::Status tensorstore::GetStatus(StatusBuilder&status_builder);
absl::Status
tensorstore::GetStatus(const StatusBuilderstatus_builder);

Returns the absl::Status for the StatusBuilder.