error-inl.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. #ifndef STRIPPED_ERROR_INL_H_
  2. #error "Direct inclusion of this file is not allowed, include error.h"
  3. // For the sake of sane code completion.
  4. #include "error.h"
  5. #endif
  6. #include <library/cpp/yt/error/error_attributes.h>
  7. #include <library/cpp/yt/string/format.h>
  8. namespace NYT {
  9. ////////////////////////////////////////////////////////////////////////////////
  10. inline constexpr TErrorCode::TErrorCode()
  11. : Value_(static_cast<int>(NYT::EErrorCode::OK))
  12. { }
  13. inline constexpr TErrorCode::TErrorCode(int value)
  14. : Value_(value)
  15. { }
  16. template <class E>
  17. requires std::is_enum_v<E>
  18. constexpr TErrorCode::TErrorCode(E value)
  19. : Value_(static_cast<int>(value))
  20. { }
  21. inline constexpr TErrorCode::operator int() const
  22. {
  23. return Value_;
  24. }
  25. template <class E>
  26. requires std::is_enum_v<E>
  27. constexpr TErrorCode::operator E() const
  28. {
  29. return static_cast<E>(Value_);
  30. }
  31. template <class E>
  32. requires std::is_enum_v<E>
  33. constexpr bool TErrorCode::operator == (E rhs) const
  34. {
  35. return Value_ == static_cast<int>(rhs);
  36. }
  37. constexpr bool TErrorCode::operator == (TErrorCode rhs) const
  38. {
  39. return Value_ == static_cast<int>(rhs);
  40. }
  41. ////////////////////////////////////////////////////////////////////////////////
  42. namespace NDetail {
  43. template <class... TArgs>
  44. TString FormatErrorMessage(TStringBuf format, TArgs&&... args)
  45. {
  46. return Format(TRuntimeFormat{format}, std::forward<TArgs>(args)...);
  47. }
  48. inline TString FormatErrorMessage(TStringBuf format)
  49. {
  50. return TString(format);
  51. }
  52. } // namespace NDetail
  53. ////////////////////////////////////////////////////////////////////////////////
  54. template <class... TArgs>
  55. TError::TErrorOr(TFormatString<TArgs...> format, TArgs&&... args)
  56. : TErrorOr(NYT::EErrorCode::Generic, NYT::NDetail::FormatErrorMessage(format.Get(), std::forward<TArgs>(args)...), DisableFormat)
  57. { }
  58. template <class... TArgs>
  59. TError::TErrorOr(TErrorCode code, TFormatString<TArgs...> format, TArgs&&... args)
  60. : TErrorOr(code, NYT::NDetail::FormatErrorMessage(format.Get(), std::forward<TArgs>(args)...), DisableFormat)
  61. { }
  62. template <CInvocable<bool(const TError&)> TFilter>
  63. std::optional<TError> TError::FindMatching(const TFilter& filter) const
  64. {
  65. if (!Impl_) {
  66. return {};
  67. }
  68. if (filter(*this)) {
  69. return *this;
  70. }
  71. for (const auto& innerError : InnerErrors()) {
  72. if (auto innerResult = innerError.FindMatching(filter)) {
  73. return innerResult;
  74. }
  75. }
  76. return {};
  77. }
  78. template <CInvocable<bool(TErrorCode)> TFilter>
  79. std::optional<TError> TError::FindMatching(const TFilter& filter) const
  80. {
  81. return FindMatching([&] (const TError& error) { return filter(error.GetCode()); });
  82. }
  83. #define IMPLEMENT_COPY_WRAP(...) \
  84. return TError(__VA_ARGS__) << *this; \
  85. static_assert(true)
  86. #define IMPLEMENT_MOVE_WRAP(...) \
  87. return TError(__VA_ARGS__) << std::move(*this); \
  88. static_assert(true)
  89. template <class U>
  90. requires (!CStringLiteral<std::remove_cvref_t<U>>)
  91. TError TError::Wrap(U&& u) const &
  92. {
  93. IMPLEMENT_COPY_WRAP(std::forward<U>(u));
  94. }
  95. template <class... TArgs>
  96. TError TError::Wrap(TFormatString<TArgs...> format, TArgs&&... args) const &
  97. {
  98. IMPLEMENT_COPY_WRAP(format, std::forward<TArgs>(args)...);
  99. }
  100. template <class... TArgs>
  101. TError TError::Wrap(TErrorCode code, TFormatString<TArgs...> format, TArgs&&... args) const &
  102. {
  103. IMPLEMENT_COPY_WRAP(code, format, std::forward<TArgs>(args)...);
  104. }
  105. template <class U>
  106. requires (!CStringLiteral<std::remove_cvref_t<U>>)
  107. TError TError::Wrap(U&& u) &&
  108. {
  109. IMPLEMENT_MOVE_WRAP(std::forward<U>(u));
  110. }
  111. template <class... TArgs>
  112. TError TError::Wrap(TFormatString<TArgs...> format, TArgs&&... args) &&
  113. {
  114. IMPLEMENT_MOVE_WRAP(format, std::forward<TArgs>(args)...);
  115. }
  116. template <class... TArgs>
  117. TError TError::Wrap(TErrorCode code, TFormatString<TArgs...> format, TArgs&&... args) &&
  118. {
  119. IMPLEMENT_MOVE_WRAP(code, format, std::forward<TArgs>(args)...);
  120. }
  121. #undef IMPLEMENT_COPY_WRAP
  122. #undef IMPLEMENT_MOVE_WRAP
  123. template <CErrorNestable TValue>
  124. TError&& TError::operator << (TValue&& rhs) &&
  125. {
  126. return std::move(*this <<= std::forward<TValue>(rhs));
  127. }
  128. template <CErrorNestable TValue>
  129. TError TError::operator << (TValue&& rhs) const &
  130. {
  131. return TError(*this) << std::forward<TValue>(rhs);
  132. }
  133. template <CErrorNestable TValue>
  134. TError&& TError::operator << (const std::optional<TValue>& rhs) &&
  135. {
  136. if (rhs) {
  137. return std::move(*this <<= *rhs);
  138. } else {
  139. return std::move(*this);
  140. }
  141. }
  142. template <CErrorNestable TValue>
  143. TError TError::operator << (const std::optional<TValue>& rhs) const &
  144. {
  145. if (rhs) {
  146. return TError(*this) << *rhs;
  147. } else {
  148. return *this;
  149. }
  150. }
  151. #define IMPLEMENT_THROW_ON_ERROR(...) \
  152. if (!IsOK()) { \
  153. THROW_ERROR std::move(*this).Wrap(__VA_ARGS__); \
  154. } \
  155. static_assert(true)
  156. template <class U>
  157. requires (!CStringLiteral<std::remove_cvref_t<U>>)
  158. void TError::ThrowOnError(U&& u) const &
  159. {
  160. IMPLEMENT_THROW_ON_ERROR(std::forward<U>(u));
  161. }
  162. template <class... TArgs>
  163. void TError::ThrowOnError(TFormatString<TArgs...> format, TArgs&&... args) const &
  164. {
  165. IMPLEMENT_THROW_ON_ERROR(format, std::forward<TArgs>(args)...);
  166. }
  167. template <class... TArgs>
  168. void TError::ThrowOnError(TErrorCode code, TFormatString<TArgs...> format, TArgs&&... args) const &
  169. {
  170. IMPLEMENT_THROW_ON_ERROR(code, format, std::forward<TArgs>(args)...);
  171. }
  172. inline void TError::ThrowOnError() const &
  173. {
  174. IMPLEMENT_THROW_ON_ERROR();
  175. }
  176. template <class U>
  177. requires (!CStringLiteral<std::remove_cvref_t<U>>)
  178. void TError::ThrowOnError(U&& u) &&
  179. {
  180. IMPLEMENT_THROW_ON_ERROR(std::forward<U>(u));
  181. }
  182. template <class... TArgs>
  183. void TError::ThrowOnError(TFormatString<TArgs...> format, TArgs&&... args) &&
  184. {
  185. IMPLEMENT_THROW_ON_ERROR(format, std::forward<TArgs>(args)...);
  186. }
  187. template <class... TArgs>
  188. void TError::ThrowOnError(TErrorCode code, TFormatString<TArgs...> format, TArgs&&... args) &&
  189. {
  190. IMPLEMENT_THROW_ON_ERROR(code, format, std::forward<TArgs>(args)...);
  191. }
  192. inline void TError::ThrowOnError() &&
  193. {
  194. IMPLEMENT_THROW_ON_ERROR();
  195. }
  196. #undef IMPLEMENT_THROW_ON_ERROR
  197. ////////////////////////////////////////////////////////////////////////////////
  198. template <class T>
  199. TErrorOr<T>::TErrorOr()
  200. {
  201. Value_.emplace();
  202. }
  203. template <class T>
  204. TErrorOr<T>::TErrorOr(T&& value) noexcept
  205. : Value_(std::move(value))
  206. { }
  207. template <class T>
  208. TErrorOr<T>::TErrorOr(const T& value)
  209. : Value_(value)
  210. { }
  211. template <class T>
  212. TErrorOr<T>::TErrorOr(const TError& other)
  213. : TError(other)
  214. {
  215. YT_VERIFY(!IsOK());
  216. }
  217. template <class T>
  218. TErrorOr<T>::TErrorOr(TError&& other) noexcept
  219. : TError(std::move(other))
  220. {
  221. YT_VERIFY(!IsOK());
  222. }
  223. template <class T>
  224. TErrorOr<T>::TErrorOr(const TErrorOr<T>& other)
  225. : TError(other)
  226. {
  227. if (IsOK()) {
  228. Value_.emplace(other.Value());
  229. }
  230. }
  231. template <class T>
  232. TErrorOr<T>::TErrorOr(TErrorOr<T>&& other) noexcept
  233. : TError(std::move(other))
  234. {
  235. if (IsOK()) {
  236. Value_.emplace(std::move(other.Value()));
  237. }
  238. }
  239. template <class T>
  240. template <class U>
  241. TErrorOr<T>::TErrorOr(const TErrorOr<U>& other)
  242. : TError(other)
  243. {
  244. if (IsOK()) {
  245. Value_.emplace(other.Value());
  246. }
  247. }
  248. template <class T>
  249. template <class U>
  250. TErrorOr<T>::TErrorOr(TErrorOr<U>&& other) noexcept
  251. : TError(other)
  252. {
  253. if (IsOK()) {
  254. Value_.emplace(std::move(other.Value()));
  255. }
  256. }
  257. template <class T>
  258. TErrorOr<T>::TErrorOr(const std::exception& ex)
  259. : TError(ex)
  260. { }
  261. template <class T>
  262. TErrorOr<T>& TErrorOr<T>::operator = (const TErrorOr<T>& other)
  263. requires std::is_copy_assignable_v<T>
  264. {
  265. static_cast<TError&>(*this) = static_cast<const TError&>(other);
  266. Value_ = other.Value_;
  267. return *this;
  268. }
  269. template <class T>
  270. TErrorOr<T>& TErrorOr<T>::operator = (TErrorOr<T>&& other) noexcept
  271. requires std::is_nothrow_move_assignable_v<T>
  272. {
  273. static_cast<TError&>(*this) = std::move(other);
  274. Value_ = std::move(other.Value_);
  275. return *this;
  276. }
  277. #define IMPLEMENT_VALUE_OR_THROW_REF(...) \
  278. if (!IsOK()) { \
  279. THROW_ERROR Wrap(__VA_ARGS__); \
  280. } \
  281. return *Value_; \
  282. static_assert(true)
  283. #define IMPLEMENT_VALUE_OR_THROW_MOVE(...) \
  284. if (!IsOK()) { \
  285. THROW_ERROR std::move(*this).Wrap(__VA_ARGS__); \
  286. } \
  287. return std::move(*Value_); \
  288. static_assert(true)
  289. template <class T>
  290. template <class U>
  291. requires (!CStringLiteral<std::remove_cvref_t<U>>)
  292. const T& TErrorOr<T>::ValueOrThrow(U&& u) const & Y_LIFETIME_BOUND
  293. {
  294. IMPLEMENT_VALUE_OR_THROW_REF(std::forward<U>(u));
  295. }
  296. template <class T>
  297. template <class... TArgs>
  298. const T& TErrorOr<T>::ValueOrThrow(TFormatString<TArgs...> format, TArgs&&... args) const & Y_LIFETIME_BOUND
  299. {
  300. IMPLEMENT_VALUE_OR_THROW_REF(format, std::forward<TArgs>(args)...);
  301. }
  302. template <class T>
  303. template <class... TArgs>
  304. const T& TErrorOr<T>::ValueOrThrow(TErrorCode code, TFormatString<TArgs...> format, TArgs&&... args) const & Y_LIFETIME_BOUND
  305. {
  306. IMPLEMENT_VALUE_OR_THROW_REF(code, format, std::forward<TArgs>(args)...);
  307. }
  308. template <class T>
  309. const T& TErrorOr<T>::ValueOrThrow() const & Y_LIFETIME_BOUND
  310. {
  311. IMPLEMENT_VALUE_OR_THROW_REF();
  312. }
  313. template <class T>
  314. template <class U>
  315. requires (!CStringLiteral<std::remove_cvref_t<U>>)
  316. T& TErrorOr<T>::ValueOrThrow(U&& u) & Y_LIFETIME_BOUND
  317. {
  318. IMPLEMENT_VALUE_OR_THROW_REF(std::forward<U>(u));
  319. }
  320. template <class T>
  321. template <class... TArgs>
  322. T& TErrorOr<T>::ValueOrThrow(TFormatString<TArgs...> format, TArgs&&... args) & Y_LIFETIME_BOUND
  323. {
  324. IMPLEMENT_VALUE_OR_THROW_REF(format, std::forward<TArgs>(args)...);
  325. }
  326. template <class T>
  327. template <class... TArgs>
  328. T& TErrorOr<T>::ValueOrThrow(TErrorCode code, TFormatString<TArgs...> format, TArgs&&... args) & Y_LIFETIME_BOUND
  329. {
  330. IMPLEMENT_VALUE_OR_THROW_REF(code, format, std::forward<TArgs>(args)...);
  331. }
  332. template <class T>
  333. T& TErrorOr<T>::ValueOrThrow() & Y_LIFETIME_BOUND
  334. {
  335. IMPLEMENT_VALUE_OR_THROW_REF();
  336. }
  337. template <class T>
  338. template <class U>
  339. requires (!CStringLiteral<std::remove_cvref_t<U>>)
  340. T&& TErrorOr<T>::ValueOrThrow(U&& u) && Y_LIFETIME_BOUND
  341. {
  342. IMPLEMENT_VALUE_OR_THROW_MOVE(std::forward<U>(u));
  343. }
  344. template <class T>
  345. template <class... TArgs>
  346. T&& TErrorOr<T>::ValueOrThrow(TFormatString<TArgs...> format, TArgs&&... args) && Y_LIFETIME_BOUND
  347. {
  348. IMPLEMENT_VALUE_OR_THROW_MOVE(format, std::forward<TArgs>(args)...);
  349. }
  350. template <class T>
  351. template <class... TArgs>
  352. T&& TErrorOr<T>::ValueOrThrow(TErrorCode code, TFormatString<TArgs...> format, TArgs&&... args) && Y_LIFETIME_BOUND
  353. {
  354. IMPLEMENT_VALUE_OR_THROW_MOVE(code, format, std::forward<TArgs>(args)...);
  355. }
  356. template <class T>
  357. T&& TErrorOr<T>::ValueOrThrow() && Y_LIFETIME_BOUND
  358. {
  359. IMPLEMENT_VALUE_OR_THROW_MOVE();
  360. }
  361. #undef IMPLEMENT_VALUE_OR_THROW_REF
  362. #undef IMPLEMENT_VALUE_OR_THROW_MOVE
  363. template <class T>
  364. T&& TErrorOr<T>::Value() && Y_LIFETIME_BOUND
  365. {
  366. YT_ASSERT(IsOK());
  367. return std::move(*Value_);
  368. }
  369. template <class T>
  370. T& TErrorOr<T>::Value() & Y_LIFETIME_BOUND
  371. {
  372. YT_ASSERT(IsOK());
  373. return *Value_;
  374. }
  375. template <class T>
  376. const T& TErrorOr<T>::Value() const & Y_LIFETIME_BOUND
  377. {
  378. YT_ASSERT(IsOK());
  379. return *Value_;
  380. }
  381. template <class T>
  382. const T& TErrorOr<T>::ValueOrDefault(const T& defaultValue Y_LIFETIME_BOUND) const & Y_LIFETIME_BOUND
  383. {
  384. return IsOK() ? *Value_ : defaultValue;
  385. }
  386. template <class T>
  387. T& TErrorOr<T>::ValueOrDefault(T& defaultValue Y_LIFETIME_BOUND) & Y_LIFETIME_BOUND
  388. {
  389. return IsOK() ? *Value_ : defaultValue;
  390. }
  391. template <class T>
  392. constexpr T TErrorOr<T>::ValueOrDefault(T&& defaultValue) const &
  393. {
  394. return IsOK()
  395. ? *Value_
  396. : std::forward<T>(defaultValue);
  397. }
  398. template <class T>
  399. constexpr T TErrorOr<T>::ValueOrDefault(T&& defaultValue) &&
  400. {
  401. return IsOK()
  402. ? std::move(*Value_)
  403. : std::forward<T>(defaultValue);
  404. }
  405. ////////////////////////////////////////////////////////////////////////////////
  406. template <class TException>
  407. requires std::derived_from<std::remove_cvref_t<TException>, TErrorException>
  408. TException&& operator <<= (TException&& ex, const TError& error)
  409. {
  410. YT_VERIFY(!error.IsOK());
  411. ex.Error() = error;
  412. return std::move(ex);
  413. }
  414. template <class TException>
  415. requires std::derived_from<std::remove_cvref_t<TException>, TErrorException>
  416. TException&& operator <<= (TException&& ex, TError&& error)
  417. {
  418. YT_VERIFY(!error.IsOK());
  419. ex.Error() = std::move(error);
  420. return std::move(ex);
  421. }
  422. ////////////////////////////////////////////////////////////////////////////////
  423. namespace NDetail {
  424. template <class TArg>
  425. requires std::constructible_from<TError, TArg>
  426. TError TErrorAdaptor::operator << (TArg&& rhs) const
  427. {
  428. return TError(std::forward<TArg>(rhs));
  429. }
  430. template <class TArg>
  431. requires
  432. std::constructible_from<TError, TArg> &&
  433. std::derived_from<std::remove_cvref_t<TArg>, TError>
  434. TArg&& TErrorAdaptor::operator << (TArg&& rhs) const
  435. {
  436. return std::forward<TArg>(rhs);
  437. }
  438. template <class TErrorLike, class U>
  439. requires
  440. std::derived_from<std::remove_cvref_t<TErrorLike>, TError> &&
  441. (!CStringLiteral<std::remove_cvref_t<U>>)
  442. void ThrowErrorExceptionIfFailed(TErrorLike&& error, U&& u)
  443. {
  444. std::move(error).ThrowOnError(std::forward<U>(u));
  445. }
  446. template <class TErrorLike, class... TArgs>
  447. requires std::derived_from<std::remove_cvref_t<TErrorLike>, TError>
  448. void ThrowErrorExceptionIfFailed(TErrorLike&& error, TFormatString<TArgs...> format, TArgs&&... args)
  449. {
  450. std::move(error).ThrowOnError(format, std::forward<TArgs>(args)...);
  451. }
  452. template <class TErrorLike, class... TArgs>
  453. requires std::derived_from<std::remove_cvref_t<TErrorLike>, TError>
  454. void ThrowErrorExceptionIfFailed(TErrorLike&& error, TErrorCode code, TFormatString<TArgs...> format, TArgs&&... args)
  455. {
  456. std::move(error).ThrowOnError(code, format, std::forward<TArgs>(args)...);
  457. }
  458. template <class TErrorLike>
  459. requires std::derived_from<std::remove_cvref_t<TErrorLike>, TError>
  460. void ThrowErrorExceptionIfFailed(TErrorLike&& error)
  461. {
  462. std::move(error).ThrowOnError();
  463. }
  464. } // namespace NDetail
  465. ////////////////////////////////////////////////////////////////////////////////
  466. template <class T>
  467. void FormatValue(TStringBuilderBase* builder, const TErrorOr<T>& error, TStringBuf spec)
  468. {
  469. FormatValue(builder, static_cast<const TError&>(error), spec);
  470. }
  471. ////////////////////////////////////////////////////////////////////////////////
  472. } // namespace NYT