extended_error.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #pragma once
  2. //
  3. // Copyright (c) 2021-2022 Ivan Baidakou (basiliscos) (the dot dmol at gmail dot com)
  4. //
  5. // Distributed under the MIT Software License
  6. //
  7. #include "arc.hpp"
  8. #include <string>
  9. #include <system_error>
  10. #if defined(_MSC_VER)
  11. #pragma warning(push)
  12. #pragma warning(disable : 4251)
  13. #endif
  14. namespace rotor {
  15. struct extended_error_t;
  16. /** \brief intrusive pointer to exteneded error type */
  17. using extended_error_ptr_t = intrusive_ptr_t<extended_error_t>;
  18. /** \struct extended_error_t
  19. * \brief Holds string context, error_code and the pointer to the following error.
  20. *
  21. *
  22. * This is extension over std::error_code, to make it possible to identify the
  23. * context of the error (usually it is actor identity), and make it possible to
  24. * construct the chain of failures, that's why there is a smart pointer to the
  25. * next error.
  26. *
  27. */
  28. struct ROTOR_API extended_error_t : arc_base_t<extended_error_t> {
  29. /** \brief error context, ususally actor identity */
  30. std::string context;
  31. /** \brief abstract error code, describing occured error */
  32. std::error_code ec;
  33. /** \brief pointer to the root error */
  34. extended_error_ptr_t next;
  35. /** \brief extened error constructor */
  36. extended_error_t(const std::string &context_, const std::error_code &ec_,
  37. const extended_error_ptr_t &next_ = {}) noexcept
  38. : context{context_}, ec{ec_}, next{next_} {}
  39. /** \brief human-readeable detailed description of the error
  40. *
  41. * First, it stringifies own error in accordance with the context.
  42. *
  43. * Second, it recursively ask details on all following errors, appedning them
  44. * into the result. The result string is returned.
  45. */
  46. std::string message() const noexcept;
  47. /**
  48. * \brief returns root (inner-most) extended error
  49. */
  50. extended_error_ptr_t root() const noexcept;
  51. };
  52. /** \brief constructs smart pointer to the extened error */
  53. ROTOR_API extended_error_ptr_t make_error(const std::string &context_, const std::error_code &ec_,
  54. const extended_error_ptr_t &next_ = {}) noexcept;
  55. } // namespace rotor
  56. #if defined(_MSC_VER)
  57. #pragma warning(pop)
  58. #endif