#ifndef STRIPPED_ERROR_INL_H_ #error "Direct inclusion of this file is not allowed, include error.h" // For the sake of sane code completion. #include "error.h" #endif #include #include namespace NYT { //////////////////////////////////////////////////////////////////////////////// inline constexpr TErrorCode::TErrorCode() : Value_(static_cast(NYT::EErrorCode::OK)) { } inline constexpr TErrorCode::TErrorCode(int value) : Value_(value) { } template requires std::is_enum_v constexpr TErrorCode::TErrorCode(E value) : Value_(static_cast(value)) { } inline constexpr TErrorCode::operator int() const { return Value_; } template requires std::is_enum_v constexpr TErrorCode::operator E() const { return static_cast(Value_); } template requires std::is_enum_v constexpr bool TErrorCode::operator == (E rhs) const { return Value_ == static_cast(rhs); } constexpr bool TErrorCode::operator == (TErrorCode rhs) const { return Value_ == static_cast(rhs); } //////////////////////////////////////////////////////////////////////////////// namespace NDetail { template TString FormatErrorMessage(TStringBuf format, TArgs&&... args) { return Format(TRuntimeFormat{format}, std::forward(args)...); } inline TString FormatErrorMessage(TStringBuf format) { return TString(format); } } // namespace NDetail //////////////////////////////////////////////////////////////////////////////// template TError::TErrorOr(TFormatString format, TArgs&&... args) : TErrorOr(NYT::EErrorCode::Generic, NYT::NDetail::FormatErrorMessage(format.Get(), std::forward(args)...), DisableFormat) { } template TError::TErrorOr(TErrorCode code, TFormatString format, TArgs&&... args) : TErrorOr(code, NYT::NDetail::FormatErrorMessage(format.Get(), std::forward(args)...), DisableFormat) { } template TFilter> std::optional TError::FindMatching(const TFilter& filter) const { if (!Impl_) { return {}; } if (filter(*this)) { return *this; } for (const auto& innerError : InnerErrors()) { if (auto innerResult = innerError.FindMatching(filter)) { return innerResult; } } return {}; } template TFilter> std::optional TError::FindMatching(const TFilter& filter) const { return FindMatching([&] (const TError& error) { return filter(error.GetCode()); }); } #define IMPLEMENT_COPY_WRAP(...) \ return TError(__VA_ARGS__) << *this; \ static_assert(true) #define IMPLEMENT_MOVE_WRAP(...) \ return TError(__VA_ARGS__) << std::move(*this); \ static_assert(true) template requires (!CStringLiteral>) TError TError::Wrap(U&& u) const & { IMPLEMENT_COPY_WRAP(std::forward(u)); } template TError TError::Wrap(TFormatString format, TArgs&&... args) const & { IMPLEMENT_COPY_WRAP(format, std::forward(args)...); } template TError TError::Wrap(TErrorCode code, TFormatString format, TArgs&&... args) const & { IMPLEMENT_COPY_WRAP(code, format, std::forward(args)...); } template requires (!CStringLiteral>) TError TError::Wrap(U&& u) && { IMPLEMENT_MOVE_WRAP(std::forward(u)); } template TError TError::Wrap(TFormatString format, TArgs&&... args) && { IMPLEMENT_MOVE_WRAP(format, std::forward(args)...); } template TError TError::Wrap(TErrorCode code, TFormatString format, TArgs&&... args) && { IMPLEMENT_MOVE_WRAP(code, format, std::forward(args)...); } #undef IMPLEMENT_COPY_WRAP #undef IMPLEMENT_MOVE_WRAP template TError&& TError::operator << (TValue&& rhs) && { return std::move(*this <<= std::forward(rhs)); } template TError TError::operator << (TValue&& rhs) const & { return TError(*this) << std::forward(rhs); } template TError&& TError::operator << (const std::optional& rhs) && { if (rhs) { return std::move(*this <<= *rhs); } else { return std::move(*this); } } template TError TError::operator << (const std::optional& rhs) const & { if (rhs) { return TError(*this) << *rhs; } else { return *this; } } #define IMPLEMENT_THROW_ON_ERROR(...) \ if (!IsOK()) { \ THROW_ERROR std::move(*this).Wrap(__VA_ARGS__); \ } \ static_assert(true) template requires (!CStringLiteral>) void TError::ThrowOnError(U&& u) const & { IMPLEMENT_THROW_ON_ERROR(std::forward(u)); } template void TError::ThrowOnError(TFormatString format, TArgs&&... args) const & { IMPLEMENT_THROW_ON_ERROR(format, std::forward(args)...); } template void TError::ThrowOnError(TErrorCode code, TFormatString format, TArgs&&... args) const & { IMPLEMENT_THROW_ON_ERROR(code, format, std::forward(args)...); } inline void TError::ThrowOnError() const & { IMPLEMENT_THROW_ON_ERROR(); } template requires (!CStringLiteral>) void TError::ThrowOnError(U&& u) && { IMPLEMENT_THROW_ON_ERROR(std::forward(u)); } template void TError::ThrowOnError(TFormatString format, TArgs&&... args) && { IMPLEMENT_THROW_ON_ERROR(format, std::forward(args)...); } template void TError::ThrowOnError(TErrorCode code, TFormatString format, TArgs&&... args) && { IMPLEMENT_THROW_ON_ERROR(code, format, std::forward(args)...); } inline void TError::ThrowOnError() && { IMPLEMENT_THROW_ON_ERROR(); } #undef IMPLEMENT_THROW_ON_ERROR //////////////////////////////////////////////////////////////////////////////// template TErrorOr::TErrorOr() { Value_.emplace(); } template TErrorOr::TErrorOr(T&& value) noexcept : Value_(std::move(value)) { } template TErrorOr::TErrorOr(const T& value) : Value_(value) { } template TErrorOr::TErrorOr(const TError& other) : TError(other) { YT_VERIFY(!IsOK()); } template TErrorOr::TErrorOr(TError&& other) noexcept : TError(std::move(other)) { YT_VERIFY(!IsOK()); } template TErrorOr::TErrorOr(const TErrorOr& other) : TError(other) { if (IsOK()) { Value_.emplace(other.Value()); } } template TErrorOr::TErrorOr(TErrorOr&& other) noexcept : TError(std::move(other)) { if (IsOK()) { Value_.emplace(std::move(other.Value())); } } template template TErrorOr::TErrorOr(const TErrorOr& other) : TError(other) { if (IsOK()) { Value_.emplace(other.Value()); } } template template TErrorOr::TErrorOr(TErrorOr&& other) noexcept : TError(other) { if (IsOK()) { Value_.emplace(std::move(other.Value())); } } template TErrorOr::TErrorOr(const std::exception& ex) : TError(ex) { } template TErrorOr& TErrorOr::operator = (const TErrorOr& other) requires std::is_copy_assignable_v { static_cast(*this) = static_cast(other); Value_ = other.Value_; return *this; } template TErrorOr& TErrorOr::operator = (TErrorOr&& other) noexcept requires std::is_nothrow_move_assignable_v { static_cast(*this) = std::move(other); Value_ = std::move(other.Value_); return *this; } #define IMPLEMENT_VALUE_OR_THROW_REF(...) \ if (!IsOK()) { \ THROW_ERROR Wrap(__VA_ARGS__); \ } \ return *Value_; \ static_assert(true) #define IMPLEMENT_VALUE_OR_THROW_MOVE(...) \ if (!IsOK()) { \ THROW_ERROR std::move(*this).Wrap(__VA_ARGS__); \ } \ return std::move(*Value_); \ static_assert(true) template template requires (!CStringLiteral>) const T& TErrorOr::ValueOrThrow(U&& u) const & Y_LIFETIME_BOUND { IMPLEMENT_VALUE_OR_THROW_REF(std::forward(u)); } template template const T& TErrorOr::ValueOrThrow(TFormatString format, TArgs&&... args) const & Y_LIFETIME_BOUND { IMPLEMENT_VALUE_OR_THROW_REF(format, std::forward(args)...); } template template const T& TErrorOr::ValueOrThrow(TErrorCode code, TFormatString format, TArgs&&... args) const & Y_LIFETIME_BOUND { IMPLEMENT_VALUE_OR_THROW_REF(code, format, std::forward(args)...); } template const T& TErrorOr::ValueOrThrow() const & Y_LIFETIME_BOUND { IMPLEMENT_VALUE_OR_THROW_REF(); } template template requires (!CStringLiteral>) T& TErrorOr::ValueOrThrow(U&& u) & Y_LIFETIME_BOUND { IMPLEMENT_VALUE_OR_THROW_REF(std::forward(u)); } template template T& TErrorOr::ValueOrThrow(TFormatString format, TArgs&&... args) & Y_LIFETIME_BOUND { IMPLEMENT_VALUE_OR_THROW_REF(format, std::forward(args)...); } template template T& TErrorOr::ValueOrThrow(TErrorCode code, TFormatString format, TArgs&&... args) & Y_LIFETIME_BOUND { IMPLEMENT_VALUE_OR_THROW_REF(code, format, std::forward(args)...); } template T& TErrorOr::ValueOrThrow() & Y_LIFETIME_BOUND { IMPLEMENT_VALUE_OR_THROW_REF(); } template template requires (!CStringLiteral>) T&& TErrorOr::ValueOrThrow(U&& u) && Y_LIFETIME_BOUND { IMPLEMENT_VALUE_OR_THROW_MOVE(std::forward(u)); } template template T&& TErrorOr::ValueOrThrow(TFormatString format, TArgs&&... args) && Y_LIFETIME_BOUND { IMPLEMENT_VALUE_OR_THROW_MOVE(format, std::forward(args)...); } template template T&& TErrorOr::ValueOrThrow(TErrorCode code, TFormatString format, TArgs&&... args) && Y_LIFETIME_BOUND { IMPLEMENT_VALUE_OR_THROW_MOVE(code, format, std::forward(args)...); } template T&& TErrorOr::ValueOrThrow() && Y_LIFETIME_BOUND { IMPLEMENT_VALUE_OR_THROW_MOVE(); } #undef IMPLEMENT_VALUE_OR_THROW_REF #undef IMPLEMENT_VALUE_OR_THROW_MOVE template T&& TErrorOr::Value() && Y_LIFETIME_BOUND { YT_ASSERT(IsOK()); return std::move(*Value_); } template T& TErrorOr::Value() & Y_LIFETIME_BOUND { YT_ASSERT(IsOK()); return *Value_; } template const T& TErrorOr::Value() const & Y_LIFETIME_BOUND { YT_ASSERT(IsOK()); return *Value_; } template const T& TErrorOr::ValueOrDefault(const T& defaultValue Y_LIFETIME_BOUND) const & Y_LIFETIME_BOUND { return IsOK() ? *Value_ : defaultValue; } template T& TErrorOr::ValueOrDefault(T& defaultValue Y_LIFETIME_BOUND) & Y_LIFETIME_BOUND { return IsOK() ? *Value_ : defaultValue; } template constexpr T TErrorOr::ValueOrDefault(T&& defaultValue) const & { return IsOK() ? *Value_ : std::forward(defaultValue); } template constexpr T TErrorOr::ValueOrDefault(T&& defaultValue) && { return IsOK() ? std::move(*Value_) : std::forward(defaultValue); } //////////////////////////////////////////////////////////////////////////////// template requires std::derived_from, TErrorException> TException&& operator <<= (TException&& ex, const TError& error) { YT_VERIFY(!error.IsOK()); ex.Error() = error; return std::move(ex); } template requires std::derived_from, TErrorException> TException&& operator <<= (TException&& ex, TError&& error) { YT_VERIFY(!error.IsOK()); ex.Error() = std::move(error); return std::move(ex); } //////////////////////////////////////////////////////////////////////////////// namespace NDetail { template requires std::constructible_from TError TErrorAdaptor::operator << (TArg&& rhs) const { return TError(std::forward(rhs)); } template requires std::constructible_from && std::derived_from, TError> TArg&& TErrorAdaptor::operator << (TArg&& rhs) const { return std::forward(rhs); } template requires std::derived_from, TError> && (!CStringLiteral>) void ThrowErrorExceptionIfFailed(TErrorLike&& error, U&& u) { std::move(error).ThrowOnError(std::forward(u)); } template requires std::derived_from, TError> void ThrowErrorExceptionIfFailed(TErrorLike&& error, TFormatString format, TArgs&&... args) { std::move(error).ThrowOnError(format, std::forward(args)...); } template requires std::derived_from, TError> void ThrowErrorExceptionIfFailed(TErrorLike&& error, TErrorCode code, TFormatString format, TArgs&&... args) { std::move(error).ThrowOnError(code, format, std::forward(args)...); } template requires std::derived_from, TError> void ThrowErrorExceptionIfFailed(TErrorLike&& error) { std::move(error).ThrowOnError(); } } // namespace NDetail //////////////////////////////////////////////////////////////////////////////// template void FormatValue(TStringBuilderBase* builder, const TErrorOr& error, TStringBuf spec) { FormatValue(builder, static_cast(error), spec); } //////////////////////////////////////////////////////////////////////////////// } // namespace NYT