exception.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "exception.h"
  2. #include <library/cpp/yt/assert/assert.h>
  3. namespace NYT {
  4. ////////////////////////////////////////////////////////////////////////////////
  5. namespace {
  6. template <class TRange>
  7. void AddAttributes(TSimpleException::TAttributes& attrs, TRange&& range)
  8. {
  9. for (auto&& [key, value] : range) {
  10. YT_VERIFY(attrs.emplace(std::move(key), std::move(value)).second);
  11. }
  12. }
  13. } // namespace
  14. ////////////////////////////////////////////////////////////////////////////////
  15. TSimpleException::TSimpleException(TString message)
  16. : Message_(std::move(message))
  17. , What_(Message_)
  18. { }
  19. TSimpleException::TSimpleException(
  20. const std::exception& exception,
  21. TString message)
  22. : InnerException_(std::current_exception())
  23. , Message_(std::move(message))
  24. , What_(Message_ + "\n" + exception.what())
  25. { }
  26. const std::exception_ptr& TSimpleException::GetInnerException() const
  27. {
  28. return InnerException_;
  29. }
  30. const char* TSimpleException::what() const noexcept
  31. {
  32. return What_.c_str();
  33. }
  34. const TString& TSimpleException::GetMessage() const
  35. {
  36. return Message_;
  37. }
  38. const TSimpleException::TAttributes& TSimpleException::GetAttributes() const &
  39. {
  40. return Attributes_;
  41. }
  42. TSimpleException::TAttributes&& TSimpleException::GetAttributes() &&
  43. {
  44. return std::move(Attributes_);
  45. }
  46. TSimpleException& TSimpleException::operator<<= (TExceptionAttribute&& attribute) &
  47. {
  48. YT_VERIFY(Attributes_.emplace(std::move(attribute.Key), std::move(attribute.Value)).second);
  49. return *this;
  50. }
  51. TSimpleException& TSimpleException::operator<<= (std::vector<TExceptionAttribute>&& attributes) &
  52. {
  53. AddAttributes(Attributes_, std::move(attributes));
  54. return *this;
  55. }
  56. TSimpleException& TSimpleException::operator<<= (TAttributes&& attributes) &
  57. {
  58. AddAttributes(Attributes_, std::move(attributes));
  59. return *this;
  60. }
  61. TSimpleException& TSimpleException::operator<<= (const TExceptionAttribute& attribute) &
  62. {
  63. YT_VERIFY(Attributes_.emplace(attribute.Key, attribute.Value).second);
  64. return *this;
  65. }
  66. TSimpleException& TSimpleException::operator<<= (const std::vector<TExceptionAttribute>& attributes) &
  67. {
  68. AddAttributes(Attributes_, attributes);
  69. return *this;
  70. }
  71. TSimpleException& TSimpleException::operator<<= (const TAttributes& attributes) &
  72. {
  73. AddAttributes(Attributes_, attributes);
  74. return *this;
  75. }
  76. ////////////////////////////////////////////////////////////////////////////////
  77. } // namespace NYT