exception.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #pragma once
  2. #include "attributes.h"
  3. #include <util/generic/hash.h>
  4. #include <exception>
  5. namespace NYT {
  6. ////////////////////////////////////////////////////////////////////////////////
  7. // This is poor man's version of NYT::TErrorException to be used in
  8. // a limited subset of core libraries that are needed to implement NYT::TError.
  9. class TSimpleException
  10. : public std::exception
  11. {
  12. public:
  13. using TAttributes = THashMap<
  14. TExceptionAttribute::TKey,
  15. TExceptionAttribute::TValue>;
  16. template <class TValue>
  17. static constexpr bool CNestable = requires (TSimpleException& ex, TValue&& operand) {
  18. { ex <<= std::forward<TValue>(operand) } -> std::same_as<TSimpleException&>;
  19. };
  20. explicit TSimpleException(TString message);
  21. TSimpleException(
  22. const std::exception& exception,
  23. TString message);
  24. const std::exception_ptr& GetInnerException() const;
  25. const char* what() const noexcept override;
  26. const TString& GetMessage() const;
  27. const TAttributes& GetAttributes() const &;
  28. TAttributes&& GetAttributes() &&;
  29. TSimpleException& operator<<= (TExceptionAttribute&& attribute) &;
  30. TSimpleException& operator<<= (std::vector<TExceptionAttribute>&& attributes) &;
  31. TSimpleException& operator<<= (TAttributes&& attributes) &;
  32. TSimpleException& operator<<= (const TExceptionAttribute& attribute) &;
  33. TSimpleException& operator<<= (const std::vector<TExceptionAttribute>& attributes) &;
  34. TSimpleException& operator<<= (const TAttributes& attributes) &;
  35. // NB: clang is incapable of parsing such requirements (which refer back to the class) out-of-line.
  36. // To keep this overload from winning in resolution
  37. // when constraint actually fails, we define method right here.
  38. template <class TValue>
  39. requires CNestable<TValue>
  40. TSimpleException&& operator<< (TValue&& value) &&
  41. {
  42. return std::move(*this <<= std::forward<TValue>(value));
  43. }
  44. template <class TValue>
  45. requires CNestable<TValue>
  46. TSimpleException operator<< (TValue&& value) const &
  47. {
  48. return TSimpleException(*this) << std::forward<TValue>(value);
  49. }
  50. private:
  51. const std::exception_ptr InnerException_;
  52. const TString Message_;
  53. const TString What_;
  54. TAttributes Attributes_;
  55. };
  56. ////////////////////////////////////////////////////////////////////////////////
  57. } // namespace NYT