error.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  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(TString message)
  34. : Code_(NYT::EErrorCode::Generic)
  35. , Message_(std::move(message))
  36. {
  37. OriginAttributes_.Capture();
  38. }
  39. TImpl(TErrorCode code, TString 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 TString& GetMessage() const noexcept
  60. {
  61. return Message_;
  62. }
  63. TString* 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. TString 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 std::exception& ex)
  188. {
  189. if (auto simpleException = dynamic_cast<const TSimpleException*>(&ex)) {
  190. *this = TError(NYT::EErrorCode::Generic, TRuntimeFormat{simpleException->GetMessage()});
  191. // NB: clang-14 is incapable of capturing structured binding variables
  192. // so we force materialize them via this function call.
  193. auto addAttribute = [this] (const auto& key, const auto& value) {
  194. std::visit([&] (const auto& actual) {
  195. *this <<= TErrorAttribute(key, actual);
  196. }, value);
  197. };
  198. for (const auto& [key, value] : simpleException->GetAttributes()) {
  199. addAttribute(key, value);
  200. }
  201. try {
  202. if (simpleException->GetInnerException()) {
  203. std::rethrow_exception(simpleException->GetInnerException());
  204. }
  205. } catch (const std::exception& innerEx) {
  206. *this <<= TError(innerEx);
  207. }
  208. } else if (const auto* errorEx = dynamic_cast<const TErrorException*>(&ex)) {
  209. *this = errorEx->Error();
  210. } else {
  211. *this = TError(NYT::EErrorCode::Generic, TRuntimeFormat{ex.what()});
  212. }
  213. YT_VERIFY(!IsOK());
  214. }
  215. TError::TErrorOr(TString message, TDisableFormat)
  216. : Impl_(std::make_unique<TImpl>(std::move(message)))
  217. { }
  218. TError::TErrorOr(TErrorCode code, TString message, TDisableFormat)
  219. : Impl_(std::make_unique<TImpl>(code, std::move(message)))
  220. { }
  221. TError& TError::operator = (const TError& other)
  222. {
  223. if (other.IsOK()) {
  224. Impl_.reset();
  225. } else {
  226. Impl_ = std::make_unique<TImpl>(*other.Impl_);
  227. }
  228. return *this;
  229. }
  230. TError& TError::operator = (TError&& other) noexcept
  231. {
  232. Impl_ = std::move(other.Impl_);
  233. return *this;
  234. }
  235. TError TError::FromSystem()
  236. {
  237. return FromSystem(LastSystemError());
  238. }
  239. TError TError::FromSystem(int error)
  240. {
  241. return TError(TErrorCode(LinuxErrorCodeBase + error), TRuntimeFormat{LastSystemErrorText(error)}) <<
  242. TErrorAttribute("errno", error);
  243. }
  244. TError TError::FromSystem(const TSystemError& error)
  245. {
  246. return FromSystem(error.Status());
  247. }
  248. TErrorCode TError::GetCode() const
  249. {
  250. if (!Impl_) {
  251. return NYT::EErrorCode::OK;
  252. }
  253. return Impl_->GetCode();
  254. }
  255. TError& TError::SetCode(TErrorCode code)
  256. {
  257. MakeMutable();
  258. Impl_->SetCode(code);
  259. return *this;
  260. }
  261. TErrorCode TError::GetNonTrivialCode() const
  262. {
  263. if (!Impl_) {
  264. return NYT::EErrorCode::OK;
  265. }
  266. if (GetCode() != NYT::EErrorCode::Generic) {
  267. return GetCode();
  268. }
  269. for (const auto& innerError : InnerErrors()) {
  270. auto innerCode = innerError.GetNonTrivialCode();
  271. if (innerCode != NYT::EErrorCode::Generic) {
  272. return innerCode;
  273. }
  274. }
  275. return GetCode();
  276. }
  277. THashSet<TErrorCode> TError::GetDistinctNonTrivialErrorCodes() const
  278. {
  279. THashSet<TErrorCode> result;
  280. TraverseError(*this, [&result] (const TError& error, int /*depth*/) {
  281. if (auto errorCode = error.GetCode(); errorCode != NYT::EErrorCode::OK) {
  282. result.insert(errorCode);
  283. }
  284. });
  285. return result;
  286. }
  287. const TString& TError::GetMessage() const
  288. {
  289. if (!Impl_) {
  290. static const TString Result;
  291. return Result;
  292. }
  293. return Impl_->GetMessage();
  294. }
  295. TError& TError::SetMessage(TString message)
  296. {
  297. MakeMutable();
  298. *Impl_->MutableMessage() = std::move(message);
  299. return *this;
  300. }
  301. bool TError::HasOriginAttributes() const
  302. {
  303. if (!Impl_) {
  304. return false;
  305. }
  306. return Impl_->HasOriginAttributes();
  307. }
  308. bool TError::HasDatetime() const
  309. {
  310. if (!Impl_) {
  311. return false;
  312. }
  313. return Impl_->HasDatetime();
  314. }
  315. TInstant TError::GetDatetime() const
  316. {
  317. if (!Impl_) {
  318. return {};
  319. }
  320. return Impl_->GetDatetime();
  321. }
  322. TProcessId TError::GetPid() const
  323. {
  324. if (!Impl_) {
  325. return 0;
  326. }
  327. return Impl_->GetPid();
  328. }
  329. NThreading::TThreadId TError::GetTid() const
  330. {
  331. if (!Impl_) {
  332. return NThreading::InvalidThreadId;
  333. }
  334. return Impl_->GetTid();
  335. }
  336. TStringBuf TError::GetThreadName() const
  337. {
  338. if (!Impl_) {
  339. static TString empty;
  340. return empty;
  341. }
  342. return Impl_->GetThreadName();
  343. }
  344. bool TError::HasAttributes() const noexcept
  345. {
  346. if (!Impl_) {
  347. return false;
  348. }
  349. return Impl_->HasAttributes();
  350. }
  351. const TErrorAttributes& TError::Attributes() const
  352. {
  353. if (!Impl_) {
  354. static TErrorAttributes empty;
  355. return empty;
  356. }
  357. return Impl_->Attributes();
  358. }
  359. TErrorAttributes* TError::MutableAttributes()
  360. {
  361. MakeMutable();
  362. return Impl_->MutableAttributes();
  363. }
  364. const std::vector<TError>& TError::InnerErrors() const
  365. {
  366. if (!Impl_) {
  367. static const std::vector<TError> Result;
  368. return Result;
  369. }
  370. return Impl_->InnerErrors();
  371. }
  372. std::vector<TError>* TError::MutableInnerErrors()
  373. {
  374. MakeMutable();
  375. return Impl_->MutableInnerErrors();
  376. }
  377. TOriginAttributes* TError::MutableOriginAttributes() const noexcept
  378. {
  379. if (!Impl_) {
  380. return nullptr;
  381. }
  382. return Impl_->MutableOriginAttributes();
  383. }
  384. void TError::UpdateOriginAttributes()
  385. {
  386. if (!Impl_) {
  387. return;
  388. }
  389. Impl_->UpdateOriginAttributes();
  390. }
  391. const TString InnerErrorsTruncatedKey("inner_errors_truncated");
  392. TError TError::Truncate(
  393. int maxInnerErrorCount,
  394. i64 stringLimit,
  395. const THashSet<TStringBuf>& attributeWhitelist) const &
  396. {
  397. if (!Impl_) {
  398. return TError();
  399. }
  400. auto truncateInnerError = [=, &attributeWhitelist] (const TError& innerError) {
  401. return innerError.Truncate(maxInnerErrorCount, stringLimit, attributeWhitelist);
  402. };
  403. auto truncateAttributes = [stringLimit, &attributeWhitelist] (const TErrorAttributes& attributes, TErrorAttributes* mutableAttributes) {
  404. for (const auto& [key, value] : attributes.ListPairs()) {
  405. if (std::ssize(value) > stringLimit && !attributeWhitelist.contains(key)) {
  406. mutableAttributes->SetValue(
  407. key,
  408. NYT::ToErrorAttributeValue("...<attribute truncated>..."));
  409. } else {
  410. mutableAttributes->SetValue(
  411. key,
  412. value);
  413. }
  414. }
  415. };
  416. auto result = std::make_unique<TImpl>();
  417. result->SetCode(GetCode());
  418. *result->MutableMessage()
  419. = std::move(TruncateString(GetMessage(), stringLimit, ErrorMessageTruncatedSuffix));
  420. if (Impl_->HasAttributes()) {
  421. truncateAttributes(Impl_->Attributes(), result->MutableAttributes());
  422. }
  423. *result->MutableOriginAttributes() = Impl_->OriginAttributes();
  424. const auto& innerErrors = InnerErrors();
  425. auto& copiedInnerErrors = *result->MutableInnerErrors();
  426. if (std::ssize(innerErrors) <= maxInnerErrorCount) {
  427. for (const auto& innerError : innerErrors) {
  428. copiedInnerErrors.push_back(truncateInnerError(innerError));
  429. }
  430. } else {
  431. result->MutableAttributes()->SetValue(InnerErrorsTruncatedKey, NYT::ToErrorAttributeValue(true));
  432. // NB(arkady-e1ppa): We want to always keep the last inner error,
  433. // so we make room for it and do not check if it is whitelisted.
  434. for (int idx = 0; idx < std::ssize(innerErrors) - 1; ++idx) {
  435. const auto& innerError = innerErrors[idx];
  436. if (
  437. IsWhitelisted(innerError, attributeWhitelist) ||
  438. std::ssize(copiedInnerErrors) < maxInnerErrorCount - 1)
  439. {
  440. copiedInnerErrors.push_back(truncateInnerError(innerError));
  441. }
  442. }
  443. copiedInnerErrors.push_back(truncateInnerError(innerErrors.back()));
  444. }
  445. return TError(std::move(result));
  446. }
  447. TError TError::Truncate(
  448. int maxInnerErrorCount,
  449. i64 stringLimit,
  450. const THashSet<TStringBuf>& attributeWhitelist) &&
  451. {
  452. if (!Impl_) {
  453. return TError();
  454. }
  455. auto truncateInnerError = [=, &attributeWhitelist] (TError& innerError) {
  456. innerError = std::move(innerError).Truncate(maxInnerErrorCount, stringLimit, attributeWhitelist);
  457. };
  458. auto truncateAttributes = [stringLimit, &attributeWhitelist] (TErrorAttributes* attributes) {
  459. for (const auto& [key, value] : attributes->ListPairs()) {
  460. if (std::ssize(value) > stringLimit && !attributeWhitelist.contains(key)) {
  461. attributes->SetValue(
  462. key,
  463. NYT::ToErrorAttributeValue("...<attribute truncated>..."));
  464. }
  465. }
  466. };
  467. TruncateStringInplace(Impl_->MutableMessage(), stringLimit, ErrorMessageTruncatedSuffix);
  468. if (Impl_->HasAttributes()) {
  469. truncateAttributes(Impl_->MutableAttributes());
  470. }
  471. if (std::ssize(InnerErrors()) <= maxInnerErrorCount) {
  472. for (auto& innerError : *MutableInnerErrors()) {
  473. truncateInnerError(innerError);
  474. }
  475. } else {
  476. auto& innerErrors = ApplyWhitelist(*MutableInnerErrors(), attributeWhitelist, maxInnerErrorCount);
  477. MutableAttributes()->SetValue(InnerErrorsTruncatedKey, NYT::ToErrorAttributeValue(true));
  478. for (auto& innerError : innerErrors) {
  479. truncateInnerError(innerError);
  480. }
  481. }
  482. return std::move(*this);
  483. }
  484. bool TError::IsOK() const
  485. {
  486. if (!Impl_) {
  487. return true;
  488. }
  489. return Impl_->IsOK();
  490. }
  491. TError TError::Wrap() const &
  492. {
  493. return *this;
  494. }
  495. TError TError::Wrap() &&
  496. {
  497. return std::move(*this);
  498. }
  499. Y_WEAK TString GetErrorSkeleton(const TError& /*error*/)
  500. {
  501. // Proper implementation resides in yt/yt/library/error_skeleton/skeleton.cpp.
  502. THROW_ERROR_EXCEPTION("Error skeleton implementation library is not linked; consider PEERDIR'ing yt/yt/library/error_skeleton");
  503. }
  504. TString TError::GetSkeleton() const
  505. {
  506. return GetErrorSkeleton(*this);
  507. }
  508. std::optional<TError> TError::FindMatching(TErrorCode code) const
  509. {
  510. return FindMatching([&] (TErrorCode errorCode) {
  511. return code == errorCode;
  512. });
  513. }
  514. std::optional<TError> TError::FindMatching(const THashSet<TErrorCode>& codes) const
  515. {
  516. return FindMatching([&] (TErrorCode code) {
  517. return codes.contains(code);
  518. });
  519. }
  520. TError::TErrorOr(std::unique_ptr<TImpl> impl)
  521. : Impl_(std::move(impl))
  522. { }
  523. void TError::MakeMutable()
  524. {
  525. if (!Impl_) {
  526. Impl_ = std::make_unique<TImpl>();
  527. }
  528. }
  529. ////////////////////////////////////////////////////////////////////////////////
  530. TError& TError::operator <<= (const TErrorAttribute& attribute) &
  531. {
  532. MutableAttributes()->SetAttribute(attribute);
  533. return *this;
  534. }
  535. TError& TError::operator <<= (const std::vector<TErrorAttribute>& attributes) &
  536. {
  537. for (const auto& attribute : attributes) {
  538. MutableAttributes()->SetAttribute(attribute);
  539. }
  540. return *this;
  541. }
  542. TError& TError::operator <<= (const TError& innerError) &
  543. {
  544. MutableInnerErrors()->push_back(innerError);
  545. return *this;
  546. }
  547. TError& TError::operator <<= (TError&& innerError) &
  548. {
  549. MutableInnerErrors()->push_back(std::move(innerError));
  550. return *this;
  551. }
  552. TError& TError::operator <<= (const std::vector<TError>& innerErrors) &
  553. {
  554. MutableInnerErrors()->insert(
  555. MutableInnerErrors()->end(),
  556. innerErrors.begin(),
  557. innerErrors.end());
  558. return *this;
  559. }
  560. TError& TError::operator <<= (std::vector<TError>&& innerErrors) &
  561. {
  562. MutableInnerErrors()->insert(
  563. MutableInnerErrors()->end(),
  564. std::make_move_iterator(innerErrors.begin()),
  565. std::make_move_iterator(innerErrors.end()));
  566. return *this;
  567. }
  568. TError& TError::operator <<= (TAnyMergeableDictionaryRef attributes) &
  569. {
  570. MutableAttributes()->MergeFrom(attributes);
  571. return *this;
  572. }
  573. ////////////////////////////////////////////////////////////////////////////////
  574. bool operator == (const TError& lhs, const TError& rhs)
  575. {
  576. if (!lhs.MutableOriginAttributes() && !rhs.MutableOriginAttributes()) {
  577. return true;
  578. }
  579. // NB(arkady-e1ppa): Origin attributes equality comparison is
  580. // bit-wise but garbage bits are zeroed so it shouldn't matter.
  581. return
  582. lhs.GetCode() == rhs.GetCode() &&
  583. lhs.GetMessage() == rhs.GetMessage() &&
  584. *lhs.MutableOriginAttributes() == *rhs.MutableOriginAttributes() &&
  585. lhs.Attributes() == rhs.Attributes() &&
  586. lhs.InnerErrors() == rhs.InnerErrors();
  587. }
  588. ////////////////////////////////////////////////////////////////////////////////
  589. void AppendIndent(TStringBuilderBase* builer, int indent)
  590. {
  591. builer->AppendChar(' ', indent);
  592. }
  593. void AppendAttribute(TStringBuilderBase* builder, const std::string& key, const std::string& value, int indent)
  594. {
  595. AppendIndent(builder, indent + 4);
  596. if (value.find('\n') == std::string::npos) {
  597. builder->AppendFormat("%-15s %s", key, value);
  598. } else {
  599. builder->AppendString(key);
  600. std::string indentedValue = "\n" + value;
  601. SubstGlobal(indentedValue, "\n", "\n" + std::string(static_cast<size_t>(indent + 8), ' '));
  602. // Now first line in indentedValue is empty and every other line is indented by 8 spaces.
  603. builder->AppendString(indentedValue);
  604. }
  605. builder->AppendChar('\n');
  606. }
  607. void AppendError(TStringBuilderBase* builder, const TError& error, int indent)
  608. {
  609. auto isStringTextYson = [] (TStringBuf str) {
  610. return
  611. str &&
  612. std::ssize(str) != 0 &&
  613. str.front() == '\"';
  614. };
  615. auto isBoolTextYson = [] (TStringBuf str) {
  616. return
  617. str == "%false" ||
  618. str == "%true";
  619. };
  620. if (error.IsOK()) {
  621. builder->AppendString("OK");
  622. return;
  623. }
  624. AppendIndent(builder, indent);
  625. builder->AppendString(error.GetMessage());
  626. builder->AppendChar('\n');
  627. if (error.GetCode() != NYT::EErrorCode::Generic) {
  628. AppendAttribute(builder, "code", NYT::ToString(static_cast<int>(error.GetCode())), indent);
  629. }
  630. // Pretty-print origin.
  631. const auto* originAttributes = error.MutableOriginAttributes();
  632. YT_ASSERT(originAttributes);
  633. if (error.HasOriginAttributes()) {
  634. AppendAttribute(
  635. builder,
  636. "origin",
  637. NYT::NDetail::FormatOrigin(*originAttributes),
  638. indent);
  639. } else if (IsErrorSanitizerEnabled() && originAttributes->Host.operator bool()) {
  640. AppendAttribute(
  641. builder,
  642. "host",
  643. TString{originAttributes->Host},
  644. indent);
  645. }
  646. if (error.HasDatetime()) {
  647. AppendAttribute(
  648. builder,
  649. "datetime",
  650. Format("%v", error.GetDatetime()),
  651. indent);
  652. }
  653. for (const auto& [key, value] : error.Attributes().ListPairs()) {
  654. if (isStringTextYson(value)) {
  655. AppendAttribute(builder, key, NDetail::ConvertFromTextYsonString<std::string>(value), indent);
  656. } else if (isBoolTextYson(value)) {
  657. AppendAttribute(builder, key, std::string(FormatBool(NDetail::ConvertFromTextYsonString<bool>(value))), indent);
  658. } else {
  659. AppendAttribute(builder, key, value, indent);
  660. }
  661. }
  662. for (const auto& innerError : error.InnerErrors()) {
  663. builder->AppendChar('\n');
  664. AppendError(builder, innerError, indent + 2);
  665. }
  666. }
  667. ////////////////////////////////////////////////////////////////////////////////
  668. void FormatValue(TStringBuilderBase* builder, const TError& error, TStringBuf /*spec*/)
  669. {
  670. AppendError(builder, error, 0);
  671. }
  672. ////////////////////////////////////////////////////////////////////////////////
  673. void TraverseError(const TError& error, const TErrorVisitor& visitor, int depth)
  674. {
  675. visitor(error, depth);
  676. for (const auto& inner : error.InnerErrors()) {
  677. TraverseError(inner, visitor, depth + 1);
  678. }
  679. }
  680. ////////////////////////////////////////////////////////////////////////////////
  681. const char* TErrorException::what() const noexcept
  682. {
  683. if (CachedWhat_.empty()) {
  684. CachedWhat_ = ToString(Error_);
  685. }
  686. return CachedWhat_.data();
  687. }
  688. ////////////////////////////////////////////////////////////////////////////////
  689. } // namespace NYT