19 #ifndef ASYLO_UTIL_STATUS_H_ 20 #define ASYLO_UTIL_STATUS_H_ 25 #include <type_traits> 29 #include "absl/base/attributes.h" 30 #include "absl/container/flat_hash_map.h" 31 #include "absl/meta/type_traits.h" 32 #include "absl/status/status.h" 33 #include "absl/status/statusor.h" 34 #include "absl/strings/cord.h" 35 #include "absl/strings/string_view.h" 36 #include "absl/types/optional.h" 37 #include "asylo/util/logging.h" 38 #include "asylo/util/error_codes.h" 39 #include "asylo/util/error_space.h" 40 #include "asylo/util/status.pb.h" 41 #include "asylo/util/status_error_space.h" 42 #include "asylo/util/status_internal.h" 65 "Deprecated as part of Asylo's absl::Status migration. Use payloads to " 66 "communicate additional error information if needed.")
67 Status(
const error::ErrorSpace *space,
int code, absl::string_view message);
74 template <
typename Enum>
75 Status(Enum code, absl::string_view message) {
79 Status(
const Status &other) =
default;
83 Status(Status &&other);
99 template <
typename StatusT,
100 typename E =
typename absl::enable_if_t<
101 status_internal::status_type_traits<StatusT>::is_status>>
103 "Deprecated as part of Asylo's absl::Status migration. Use " 104 "ConvertStatus() from status_helpers.h instead.")
105 explicit Status(
const StatusT &other) {
106 Set(status_internal::status_type_traits<StatusT>::CanonicalCode(other),
107 other.error_message());
110 Status(
const absl::Status &other);
112 Status &operator=(
const Status &other) =
default;
116 Status &operator=(Status &&other);
124 "Deprecated as part of Asylo's absl::Status migration. Use " 125 "absl::OkStatus() or asylo::OkStatus() instead.")
126 static Status OkStatus();
144 template <
typename StatusT,
145 typename E =
typename absl::enable_if_t<
146 status_internal::status_type_traits<StatusT>::is_status>>
148 "Deprecated as part of Asylo's absl::Status migration. Use " 149 "ConvertStatus() from status_helpers.h instead.")
150 StatusT ToOtherStatus() {
151 Status status = ToCanonical();
152 return StatusT(status_internal::ErrorCodeHolder(status.error_code_),
181 operator ::absl::Status()
const;
184 operator absl::StatusOr<T>()
const {
185 return absl::StatusOr<T>(::absl::Status(*
this));
194 "Deprecated as part of Asylo's absl::Status migration. Use raw_code() " 196 int error_code()
const;
201 int raw_code()
const;
209 "Deprecated as part of Asylo's absl::Status migration. Use message() " 211 absl::string_view error_message()
const;
216 absl::string_view message()
const;
224 "Deprecated as part of Asylo's absl::Status migration. Use payloads " 225 "instead of error spaces.")
226 const error::ErrorSpace *error_space()
const;
242 std::string ToString()
const;
258 "Deprecated as part of Asylo's absl::Status migration. Use payloads " 259 "instead of error spaces.")
260 Status ToCanonical()
const;
268 "Deprecated as part of Asylo's absl::Status migration. Use code() " 270 error::GoogleError CanonicalCode()
const;
275 absl::StatusCode code()
const;
285 "Deprecated as part of Asylo's absl::Status migration. Use " 286 "StatusToProto() from status_helpers.h instead.")
287 void SaveTo(StatusProto *status_proto)
const;
311 "Deprecated as part of Asylo's absl::Status migration. Use " 312 "StatusFromProto() from status_helpers.h instead.")
313 void RestoreFrom(
const StatusProto &status_proto);
323 template <
typename Enum>
325 "Deprecated as part of Asylo's absl::Status migration. Use payloads " 326 "instead of error spaces.")
327 bool Is(Enum code)
const {
328 return (
static_cast<
int>(code) == error_code_) &&
329 (error::error_enum_traits<Enum>::get_error_space() == error_space_);
341 "Deprecated as part of Asylo's absl::Status migration. Use WithContext() " 342 "from status_helpers.h instead.")
343 Status WithPrependedContext(absl::string_view context);
350 absl::optional<absl::Cord> GetPayload(absl::string_view type_url)
const;
356 void SetPayload(absl::string_view type_url, absl::Cord payload);
362 bool ErasePayload(absl::string_view type_url);
375 const std::function<
void(absl::string_view,
const absl::Cord &)> &visitor)
379 friend bool operator==(
const Status &lhs,
const Status &rhs);
382 template <
typename Enum,
typename StringT>
383 void Set(Enum code, StringT &&message) {
384 error_space_ = error::error_enum_traits<Enum>::get_error_space();
385 error_code_ =
static_cast<
int>(code);
386 if (error_code_ != 0) {
387 message_ = std::string(std::forward<StringT>(message));
395 bool IsCanonical()
const;
398 std::string ToStringWithoutPayloads()
const;
400 const error::ErrorSpace *error_space_;
405 std::string message_;
407 absl::flat_hash_map<std::string, absl::Cord> payloads_;
410 bool operator==(
const Status &lhs,
const Status &rhs);
412 bool operator!=(
const Status &lhs,
const Status &rhs);
414 std::ostream &operator<<(std::ostream &os,
const Status &status);
416 bool operator==(
const Status &lhs,
const absl::Status &rhs);
418 bool operator!=(
const Status &lhs,
const absl::Status &rhs);
420 bool operator==(
const absl::Status &lhs,
const Status &rhs);
422 bool operator!=(
const absl::Status &lhs,
const Status &rhs);
430 #define ASYLO_CHECK_OK(val) CHECK_EQ(::asylo::OkStatus(), (val))