error.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. #include "error.h"
  2. #include <library/cpp/yt/exception/exception.h>
  3. #include <library/cpp/yt/error/error_attributes.h>
  4. #include <library/cpp/yt/error/origin_attributes.h>
  5. #include <library/cpp/yt/string/string.h>
  6. #include <library/cpp/yt/system/proc.h>
  7. #include <library/cpp/yt/yson_string/string.h>
  8. #include <util/string/subst.h>
  9. #include <util/system/error.h>
  10. #include <util/system/thread.h>
  11. namespace NYT {
  12. ////////////////////////////////////////////////////////////////////////////////
  13. void FormatValue(TStringBuilderBase* builder, TErrorCode code, TStringBuf spec)
  14. {
  15. FormatValue(builder, static_cast<int>(code), spec);
  16. }
  17. ////////////////////////////////////////////////////////////////////////////////
  18. constexpr TStringBuf ErrorMessageTruncatedSuffix = "...<message truncated>";
  19. ////////////////////////////////////////////////////////////////////////////////
  20. class TError::TImpl
  21. {
  22. public:
  23. TImpl()
  24. : Code_(NYT::EErrorCode::OK)
  25. { }
  26. TImpl(const TError::TImpl& other)
  27. : Code_(other.Code_)
  28. , Message_(other.Message_)
  29. , OriginAttributes_(other.OriginAttributes_)
  30. , Attributes_(other.Attributes_)
  31. , InnerErrors_(other.InnerErrors_)
  32. { }
  33. explicit TImpl(std::string message)
  34. : Code_(NYT::EErrorCode::Generic)
  35. , Message_(std::move(message))
  36. {
  37. OriginAttributes_.Capture();
  38. }
  39. TImpl(TErrorCode code, std::string message)
  40. : Code_(code)
  41. , Message_(std::move(message))
  42. {
  43. if (!IsOK()) {
  44. OriginAttributes_.Capture();
  45. }
  46. }
  47. bool IsOK() const noexcept
  48. {
  49. return Code_ == NYT::EErrorCode::OK;
  50. }
  51. TErrorCode GetCode() const noexcept
  52. {
  53. return Code_;
  54. }
  55. void SetCode(TErrorCode code) noexcept
  56. {
  57. Code_ = code;
  58. }
  59. const std::string& GetMessage() const noexcept
  60. {
  61. return Message_;
  62. }
  63. std::string* MutableMessage() noexcept
  64. {
  65. return &Message_;
  66. }
  67. bool HasOriginAttributes() const noexcept
  68. {
  69. return OriginAttributes_.ThreadName.Length > 0;
  70. }
  71. const TOriginAttributes& OriginAttributes() const noexcept
  72. {
  73. return OriginAttributes_;
  74. }
  75. TOriginAttributes* MutableOriginAttributes() noexcept
  76. {
  77. return &OriginAttributes_;
  78. }
  79. TProcessId GetPid() const noexcept
  80. {
  81. return OriginAttributes_.Pid;
  82. }
  83. NThreading::TThreadId GetTid() const noexcept
  84. {
  85. return OriginAttributes_.Tid;
  86. }
  87. TStringBuf GetThreadName() const noexcept
  88. {
  89. return OriginAttributes_.ThreadName.ToStringBuf();
  90. }
  91. bool HasDatetime() const noexcept
  92. {
  93. return OriginAttributes_.Datetime != TInstant();
  94. }
  95. TInstant GetDatetime() const noexcept
  96. {
  97. return OriginAttributes_.Datetime;
  98. }
  99. void SetDatetime(TInstant datetime) noexcept
  100. {
  101. OriginAttributes_.Datetime = datetime;
  102. }
  103. bool HasAttributes() const noexcept
  104. {
  105. return true;
  106. }
  107. const TErrorAttributes& Attributes() const
  108. {
  109. return Attributes_;
  110. }
  111. void UpdateOriginAttributes()
  112. {
  113. OriginAttributes_ = NYT::NDetail::ExtractFromDictionary(&Attributes_);
  114. }
  115. TErrorAttributes* MutableAttributes() noexcept
  116. {
  117. return &Attributes_;
  118. }
  119. const std::vector<TError>& InnerErrors() const noexcept
  120. {
  121. return InnerErrors_;
  122. }
  123. std::vector<TError>* MutableInnerErrors() noexcept
  124. {
  125. return &InnerErrors_;
  126. }
  127. private:
  128. TErrorCode Code_;
  129. std::string Message_;
  130. TOriginAttributes OriginAttributes_{
  131. .Pid = 0,
  132. .Tid = NThreading::InvalidThreadId,
  133. };
  134. TErrorAttributes Attributes_;
  135. std::vector<TError> InnerErrors_;
  136. };
  137. ////////////////////////////////////////////////////////////////////////////////
  138. namespace {
  139. bool IsWhitelisted(const TError& error, const THashSet<TStringBuf>& attributeWhitelist)
  140. {
  141. for (const auto& key : error.Attributes().ListKeys()) {
  142. if (attributeWhitelist.contains(key)) {
  143. return true;
  144. }
  145. }
  146. for (const auto& innerError : error.InnerErrors()) {
  147. if (IsWhitelisted(innerError, attributeWhitelist)) {
  148. return true;
  149. }
  150. }
  151. return false;
  152. }
  153. //! Returns vector which consists of objects from errors such that:
  154. //! if N is the number of objects in errors s.t. IsWhitelisted is true
  155. //! then first N objects of returned vector are the ones for which IsWhitelisted is true
  156. //! followed by std::max(0, maxInnerErrorCount - N - 1) remaining objects
  157. //! finally followed by errors.back().
  158. std::vector<TError>& ApplyWhitelist(std::vector<TError>& errors, const THashSet<TStringBuf>& attributeWhitelist, int maxInnerErrorCount)
  159. {
  160. if (std::ssize(errors) < std::max(2, maxInnerErrorCount)) {
  161. return errors;
  162. }
  163. auto firstNotWhitelisted = std::partition(
  164. errors.begin(),
  165. std::prev(errors.end()),
  166. [&attributeWhitelist] (const TError& error) {
  167. return IsWhitelisted(error, attributeWhitelist);
  168. });
  169. int lastErrorOffset = std::max<int>(firstNotWhitelisted - errors.begin(), maxInnerErrorCount - 1);
  170. *(errors.begin() + lastErrorOffset) = std::move(errors.back());
  171. errors.resize(lastErrorOffset + 1);
  172. return errors;
  173. }
  174. } // namespace
  175. ////////////////////////////////////////////////////////////////////////////////
  176. TError::TErrorOr() = default;
  177. TError::~TErrorOr() = default;
  178. TError::TErrorOr(const TError& other)
  179. {
  180. if (!other.IsOK()) {
  181. Impl_ = std::make_unique<TImpl>(*other.Impl_);
  182. }
  183. }
  184. TError::TErrorOr(TError&& other) noexcept
  185. : Impl_(std::move(other.Impl_))
  186. { }
  187. TError::TErrorOr(const TErrorException& errorEx) noexcept
  188. {
  189. *this = errorEx.Error();
  190. // NB: TErrorException verifies that error not IsOK at throwing end.
  191. }
  192. TError::TErrorOr(const std::exception& ex)
  193. {
  194. if (auto simpleException = dynamic_cast<const TSimpleException*>(&ex)) {
  195. *this = TError(NYT::EErrorCode::Generic, TRuntimeFormat{simpleException->GetMessage()});
  196. // NB: clang-14 is incapable of capturing structured binding variables
  197. // so we force materialize them via this function call.
  198. auto addAttribute = [this] (const auto& key, const auto& value) {
  199. std::visit([&] (const auto& actual) {
  200. *this <<= TErrorAttribute(key, actual);
  201. }, value);
  202. };
  203. for (const auto& [key, value] : simpleException->GetAttributes()) {
  204. addAttribute(key, value);
  205. }
  206. try {
  207. if (simpleException->GetInnerException()) {
  208. std::rethrow_exception(simpleException->GetInnerException());
  209. }
  210. } catch (const std::exception& innerEx) {
  211. *this <<= TError(innerEx);
  212. }
  213. } else if (const auto* errorEx = dynamic_cast<const TErrorException*>(&ex)) {
  214. *this = errorEx->Error();
  215. } else {
  216. *this = TError(NYT::EErrorCode::Generic, TRuntimeFormat{ex.what()});
  217. }
  218. YT_VERIFY(!IsOK());
  219. }
  220. TError::TErrorOr(std::string message, TDisableFormat)
  221. : Impl_(std::make_unique<TImpl>(std::move(message)))
  222. { }
  223. TError::TErrorOr(TErrorCode code, std::string message, TDisableFormat)
  224. : Impl_(std::make_unique<TImpl>(code, std::move(message)))
  225. { }
  226. TError& TError::operator = (const TError& other)
  227. {
  228. if (other.IsOK()) {
  229. Impl_.reset();
  230. } else {
  231. Impl_ = std::make_unique<TImpl>(*other.Impl_);
  232. }
  233. return *this;
  234. }
  235. TError& TError::operator = (TError&& other) noexcept
  236. {
  237. Impl_ = std::move(other.Impl_);
  238. return *this;
  239. }
  240. TError TError::FromSystem()
  241. {
  242. return FromSystem(LastSystemError());
  243. }
  244. TError TError::FromSystem(int error)
  245. {
  246. return TError(TErrorCode(LinuxErrorCodeBase + error), TRuntimeFormat{LastSystemErrorText(error)}) <<
  247. TErrorAttribute("errno", error);
  248. }
  249. TError TError::FromSystem(const TSystemError& error)
  250. {
  251. return FromSystem(error.Status());
  252. }
  253. TErrorCode TError::GetCode() const
  254. {
  255. if (!Impl_) {
  256. return NYT::EErrorCode::OK;
  257. }
  258. return Impl_->GetCode();
  259. }
  260. TError& TError::SetCode(TErrorCode code)
  261. {
  262. MakeMutable();
  263. Impl_->SetCode(code);
  264. return *this;
  265. }
  266. TErrorCode TError::GetNonTrivialCode() const
  267. {
  268. if (!Impl_) {
  269. return NYT::EErrorCode::OK;
  270. }
  271. if (GetCode() != NYT::EErrorCode::Generic) {
  272. return GetCode();
  273. }
  274. for (const auto& innerError : InnerErrors()) {
  275. auto innerCode = innerError.GetNonTrivialCode();
  276. if (innerCode != NYT::EErrorCode::Generic) {
  277. return innerCode;
  278. }
  279. }
  280. return GetCode();
  281. }
  282. THashSet<TErrorCode> TError::GetDistinctNonTrivialErrorCodes() const
  283. {
  284. THashSet<TErrorCode> result;
  285. TraverseError(*this, [&result] (const TError& error, int /*depth*/) {
  286. if (auto errorCode = error.GetCode(); errorCode != NYT::EErrorCode::OK) {
  287. result.insert(errorCode);
  288. }
  289. });
  290. return result;
  291. }
  292. const std::string& TError::GetMessage() const
  293. {
  294. if (!Impl_) {
  295. static const std::string Result;
  296. return Result;
  297. }
  298. return Impl_->GetMessage();
  299. }
  300. TError& TError::SetMessage(std::string message)
  301. {
  302. MakeMutable();
  303. *Impl_->MutableMessage() = std::move(message);
  304. return *this;
  305. }
  306. bool TError::HasOriginAttributes() const
  307. {
  308. if (!Impl_) {
  309. return false;
  310. }
  311. return Impl_->HasOriginAttributes();
  312. }
  313. bool TError::HasDatetime() const
  314. {
  315. if (!Impl_) {
  316. return false;
  317. }
  318. return Impl_->HasDatetime();
  319. }
  320. TInstant TError::GetDatetime() const
  321. {
  322. if (!Impl_) {
  323. return {};
  324. }
  325. return Impl_->GetDatetime();
  326. }
  327. TProcessId TError::GetPid() const
  328. {
  329. if (!Impl_) {
  330. return 0;
  331. }
  332. return Impl_->GetPid();
  333. }
  334. NThreading::TThreadId TError::GetTid() const
  335. {
  336. if (!Impl_) {
  337. return NThreading::InvalidThreadId;
  338. }
  339. return Impl_->GetTid();
  340. }
  341. TStringBuf TError::GetThreadName() const
  342. {
  343. if (!Impl_) {
  344. static std::string empty;
  345. return empty;
  346. }
  347. return Impl_->GetThreadName();
  348. }
  349. bool TError::HasAttributes() const noexcept
  350. {
  351. if (!Impl_) {
  352. return false;
  353. }
  354. return Impl_->HasAttributes();
  355. }
  356. const TErrorAttributes& TError::Attributes() const
  357. {
  358. if (!Impl_) {
  359. static TErrorAttributes empty;
  360. return empty;
  361. }
  362. return Impl_->Attributes();
  363. }
  364. TErrorAttributes* TError::MutableAttributes()
  365. {
  366. MakeMutable();
  367. return Impl_->MutableAttributes();
  368. }
  369. const std::vector<TError>& TError::InnerErrors() const
  370. {
  371. if (!Impl_) {
  372. static const std::vector<TError> Result;
  373. return Result;
  374. }
  375. return Impl_->InnerErrors();
  376. }
  377. std::vector<TError>* TError::MutableInnerErrors()
  378. {
  379. MakeMutable();
  380. return Impl_->MutableInnerErrors();
  381. }
  382. TOriginAttributes* TError::MutableOriginAttributes() const noexcept
  383. {
  384. if (!Impl_) {
  385. return nullptr;
  386. }
  387. return Impl_->MutableOriginAttributes();
  388. }
  389. void TError::UpdateOriginAttributes()
  390. {
  391. if (!Impl_) {
  392. return;
  393. }
  394. Impl_->UpdateOriginAttributes();
  395. }
  396. const std::string InnerErrorsTruncatedKey("inner_errors_truncated");
  397. TError TError::Truncate(
  398. int maxInnerErrorCount,
  399. i64 stringLimit,
  400. const THashSet<TStringBuf>& attributeWhitelist) const &
  401. {
  402. if (!Impl_) {
  403. return TError();
  404. }
  405. auto truncateInnerError = [=, &attributeWhitelist] (const TError& innerError) {
  406. return innerError.Truncate(maxInnerErrorCount, stringLimit, attributeWhitelist);
  407. };
  408. auto truncateAttributes = [stringLimit, &attributeWhitelist] (const TErrorAttributes& attributes, TErrorAttributes* mutableAttributes) {
  409. for (const auto& [key, value] : attributes.ListPairs()) {
  410. if (std::ssize(value) > stringLimit && !attributeWhitelist.contains(key)) {
  411. mutableAttributes->SetValue(
  412. key,
  413. NYT::ToErrorAttributeValue("...<attribute truncated>..."));
  414. } else {
  415. mutableAttributes->SetValue(
  416. key,
  417. value);
  418. }
  419. }
  420. };
  421. auto result = std::make_unique<TImpl>();
  422. result->SetCode(GetCode());
  423. *result->MutableMessage() = TruncateString(GetMessage(), stringLimit, ErrorMessageTruncatedSuffix);
  424. if (Impl_->HasAttributes()) {
  425. truncateAttributes(Impl_->Attributes(), result->MutableAttributes());
  426. }
  427. *result->MutableOriginAttributes() = Impl_->OriginAttributes();
  428. const auto& innerErrors = InnerErrors();
  429. auto& copiedInnerErrors = *result->MutableInnerErrors();
  430. if (std::ssize(innerErrors) <= maxInnerErrorCount) {
  431. for (const auto& innerError : innerErrors) {
  432. copiedInnerErrors.push_back(truncateInnerError(innerError));
  433. }
  434. } else {
  435. result->MutableAttributes()->SetValue(InnerErrorsTruncatedKey, NYT::ToErrorAttributeValue(true));
  436. // NB(arkady-e1ppa): We want to always keep the last inner error,
  437. // so we make room for it and do not check if it is whitelisted.
  438. for (int idx = 0; idx < std::ssize(innerErrors) - 1; ++idx) {
  439. const auto& innerError = innerErrors[idx];
  440. if (
  441. IsWhitelisted(innerError, attributeWhitelist) ||
  442. std::ssize(copiedInnerErrors) < maxInnerErrorCount - 1)
  443. {
  444. copiedInnerErrors.push_back(truncateInnerError(innerError));
  445. }
  446. }
  447. copiedInnerErrors.push_back(truncateInnerError(innerErrors.back()));
  448. }
  449. return TError(std::move(result));
  450. }
  451. TError TError::Truncate(
  452. int maxInnerErrorCount,
  453. i64 stringLimit,
  454. const THashSet<TStringBuf>& attributeWhitelist) &&
  455. {
  456. if (!Impl_) {
  457. return TError();
  458. }
  459. auto truncateInnerError = [=, &attributeWhitelist] (TError& innerError) {
  460. innerError = std::move(innerError).Truncate(maxInnerErrorCount, stringLimit, attributeWhitelist);
  461. };
  462. auto truncateAttributes = [stringLimit, &attributeWhitelist] (TErrorAttributes* attributes) {
  463. for (const auto& [key, value] : attributes->ListPairs()) {
  464. if (std::ssize(value) > stringLimit && !attributeWhitelist.contains(key)) {
  465. attributes->SetValue(
  466. key,
  467. NYT::ToErrorAttributeValue("...<attribute truncated>..."));
  468. }
  469. }
  470. };
  471. TruncateStringInplace(Impl_->MutableMessage(), stringLimit, ErrorMessageTruncatedSuffix);
  472. if (Impl_->HasAttributes()) {
  473. truncateAttributes(Impl_->MutableAttributes());
  474. }
  475. if (std::ssize(InnerErrors()) <= maxInnerErrorCount) {
  476. for (auto& innerError : *MutableInnerErrors()) {
  477. truncateInnerError(innerError);
  478. }
  479. } else {
  480. auto& innerErrors = ApplyWhitelist(*MutableInnerErrors(), attributeWhitelist, maxInnerErrorCount);
  481. MutableAttributes()->SetValue(InnerErrorsTruncatedKey, NYT::ToErrorAttributeValue(true));
  482. for (auto& innerError : innerErrors) {
  483. truncateInnerError(innerError);
  484. }
  485. }
  486. return std::move(*this);
  487. }
  488. bool TError::IsOK() const
  489. {
  490. if (!Impl_) {
  491. return true;
  492. }
  493. return Impl_->IsOK();
  494. }
  495. TError TError::Wrap() const &
  496. {
  497. return *this;
  498. }
  499. TError TError::Wrap() &&
  500. {
  501. return std::move(*this);
  502. }
  503. Y_WEAK std::string GetErrorSkeleton(const TError& /*error*/)
  504. {
  505. // Proper implementation resides in yt/yt/library/error_skeleton/skeleton.cpp.
  506. THROW_ERROR_EXCEPTION("Error skeleton implementation library is not linked; consider PEERDIR'ing yt/yt/library/error_skeleton");
  507. }
  508. std::string TError::GetSkeleton() const
  509. {
  510. return GetErrorSkeleton(*this);
  511. }
  512. std::optional<TError> TError::FindMatching(TErrorCode code) const
  513. {
  514. return FindMatching([&] (TErrorCode errorCode) {
  515. return code == errorCode;
  516. });
  517. }
  518. std::optional<TError> TError::FindMatching(const THashSet<TErrorCode>& codes) const
  519. {
  520. return FindMatching([&] (TErrorCode code) {
  521. return codes.contains(code);
  522. });
  523. }
  524. TError::TErrorOr(std::unique_ptr<TImpl> impl)
  525. : Impl_(std::move(impl))
  526. { }
  527. void TError::MakeMutable()
  528. {
  529. if (!Impl_) {
  530. Impl_ = std::make_unique<TImpl>();
  531. }
  532. }
  533. ////////////////////////////////////////////////////////////////////////////////
  534. TError& TError::operator <<= (const TErrorAttribute& attribute) &
  535. {
  536. MutableAttributes()->SetAttribute(attribute);
  537. return *this;
  538. }
  539. TError& TError::operator <<= (const std::vector<TErrorAttribute>& attributes) &
  540. {
  541. for (const auto& attribute : attributes) {
  542. MutableAttributes()->SetAttribute(attribute);
  543. }
  544. return *this;
  545. }
  546. TError& TError::operator <<= (const TError& innerError) &
  547. {
  548. MutableInnerErrors()->push_back(innerError);
  549. return *this;
  550. }
  551. TError& TError::operator <<= (TError&& innerError) &
  552. {
  553. MutableInnerErrors()->push_back(std::move(innerError));
  554. return *this;
  555. }
  556. TError& TError::operator <<= (const std::vector<TError>& innerErrors) &
  557. {
  558. MutableInnerErrors()->insert(
  559. MutableInnerErrors()->end(),
  560. innerErrors.begin(),
  561. innerErrors.end());
  562. return *this;
  563. }
  564. TError& TError::operator <<= (std::vector<TError>&& innerErrors) &
  565. {
  566. MutableInnerErrors()->insert(
  567. MutableInnerErrors()->end(),
  568. std::make_move_iterator(innerErrors.begin()),
  569. std::make_move_iterator(innerErrors.end()));
  570. return *this;
  571. }
  572. TError& TError::operator <<= (TAnyMergeableDictionaryRef attributes) &
  573. {
  574. MutableAttributes()->MergeFrom(attributes);
  575. return *this;
  576. }
  577. ////////////////////////////////////////////////////////////////////////////////
  578. bool operator == (const TError& lhs, const TError& rhs)
  579. {
  580. if (!lhs.MutableOriginAttributes() && !rhs.MutableOriginAttributes()) {
  581. return true;
  582. }
  583. // NB(arkady-e1ppa): Origin attributes equality comparison is
  584. // bit-wise but garbage bits are zeroed so it shouldn't matter.
  585. return
  586. lhs.GetCode() == rhs.GetCode() &&
  587. lhs.GetMessage() == rhs.GetMessage() &&
  588. *lhs.MutableOriginAttributes() == *rhs.MutableOriginAttributes() &&
  589. lhs.Attributes() == rhs.Attributes() &&
  590. lhs.InnerErrors() == rhs.InnerErrors();
  591. }
  592. ////////////////////////////////////////////////////////////////////////////////
  593. void AppendIndent(TStringBuilderBase* builer, int indent)
  594. {
  595. builer->AppendChar(' ', indent);
  596. }
  597. void AppendAttribute(TStringBuilderBase* builder, const std::string& key, const std::string& value, int indent)
  598. {
  599. AppendIndent(builder, indent + 4);
  600. if (value.find('\n') == std::string::npos) {
  601. builder->AppendFormat("%-15s %s", key, value);
  602. } else {
  603. builder->AppendString(key);
  604. std::string indentedValue = "\n" + value;
  605. SubstGlobal(indentedValue, "\n", "\n" + std::string(static_cast<size_t>(indent + 8), ' '));
  606. // Now first line in indentedValue is empty and every other line is indented by 8 spaces.
  607. builder->AppendString(indentedValue);
  608. }
  609. builder->AppendChar('\n');
  610. }
  611. void AppendError(TStringBuilderBase* builder, const TError& error, int indent)
  612. {
  613. auto isStringTextYson = [] (TStringBuf str) {
  614. return
  615. str &&
  616. std::ssize(str) != 0 &&
  617. str.front() == '\"';
  618. };
  619. auto isBoolTextYson = [] (TStringBuf str) {
  620. return
  621. str == "%false" ||
  622. str == "%true";
  623. };
  624. if (error.IsOK()) {
  625. builder->AppendString("OK");
  626. return;
  627. }
  628. AppendIndent(builder, indent);
  629. builder->AppendString(error.GetMessage());
  630. builder->AppendChar('\n');
  631. if (error.GetCode() != NYT::EErrorCode::Generic) {
  632. AppendAttribute(builder, "code", NYT::ToString(static_cast<int>(error.GetCode())), indent);
  633. }
  634. // Pretty-print origin.
  635. const auto* originAttributes = error.MutableOriginAttributes();
  636. YT_ASSERT(originAttributes);
  637. if (error.HasOriginAttributes()) {
  638. AppendAttribute(
  639. builder,
  640. "origin",
  641. NYT::NDetail::FormatOrigin(*originAttributes),
  642. indent);
  643. } else if (IsErrorSanitizerEnabled() && originAttributes->Host.operator bool()) {
  644. AppendAttribute(
  645. builder,
  646. "host",
  647. std::string{originAttributes->Host},
  648. indent);
  649. }
  650. if (error.HasDatetime()) {
  651. AppendAttribute(
  652. builder,
  653. "datetime",
  654. Format("%v", error.GetDatetime()),
  655. indent);
  656. }
  657. for (const auto& [key, value] : error.Attributes().ListPairs()) {
  658. if (isStringTextYson(value)) {
  659. AppendAttribute(builder, key, NDetail::ConvertFromTextYsonString<std::string>(value), indent);
  660. } else if (isBoolTextYson(value)) {
  661. AppendAttribute(builder, key, std::string(FormatBool(NDetail::ConvertFromTextYsonString<bool>(value))), indent);
  662. } else {
  663. AppendAttribute(builder, key, value, indent);
  664. }
  665. }
  666. for (const auto& innerError : error.InnerErrors()) {
  667. builder->AppendChar('\n');
  668. AppendError(builder, innerError, indent + 2);
  669. }
  670. }
  671. ////////////////////////////////////////////////////////////////////////////////
  672. void FormatValue(TStringBuilderBase* builder, const TError& error, TStringBuf /*spec*/)
  673. {
  674. AppendError(builder, error, 0);
  675. }
  676. ////////////////////////////////////////////////////////////////////////////////
  677. void TraverseError(const TError& error, const TErrorVisitor& visitor, int depth)
  678. {
  679. visitor(error, depth);
  680. for (const auto& inner : error.InnerErrors()) {
  681. TraverseError(inner, visitor, depth + 1);
  682. }
  683. }
  684. ////////////////////////////////////////////////////////////////////////////////
  685. const char* TErrorException::what() const noexcept
  686. {
  687. if (CachedWhat_.empty()) {
  688. CachedWhat_ = ToString(Error_);
  689. }
  690. return CachedWhat_.data();
  691. }
  692. ////////////////////////////////////////////////////////////////////////////////
  693. } // namespace NYT