extended_error.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #pragma once
  2. //
  3. // Copyright (c) 2021 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. namespace rotor {
  11. struct extended_error_t;
  12. /** \brief intrusive pointer to exteneded error type */
  13. using extended_error_ptr_t = intrusive_ptr_t<extended_error_t>;
  14. /** \struct extended_error_t
  15. * \brief Holds string context, error_code and the pointer to the following error.
  16. *
  17. *
  18. * This is extension over std::error_code, to make it possible to identify the
  19. * context of the error (usually it is actor identity), and make it possible to
  20. * construct the chain of failures, that's why there is a smart pointer to the
  21. * next error.
  22. *
  23. */
  24. struct extended_error_t : arc_base_t<extended_error_t> {
  25. /** \brief error context, ususally actor identity */
  26. std::string context;
  27. /** \brief abstract error code, describing occured error */
  28. std::error_code ec;
  29. /** \brief pointer to the root error */
  30. extended_error_ptr_t next;
  31. /** \brief extened error constructor */
  32. extended_error_t(const std::string &context_, const std::error_code &ec_,
  33. const extended_error_ptr_t &next_ = {}) noexcept
  34. : context{context_}, ec{ec_}, next{next_} {}
  35. /** \brief human-readeable detailed description of the error
  36. *
  37. * First, it stringifies own error in accordance with the context.
  38. *
  39. * Second, it recursively ask details on all following errors, appedning them
  40. * into the result. The result string is returned.
  41. */
  42. std::string message() const noexcept;
  43. /**
  44. * \brief returns root (inner-most) extended error
  45. */
  46. extended_error_ptr_t root() const noexcept;
  47. };
  48. /** \brief constructs smart pointer to the extened error */
  49. extended_error_ptr_t make_error(const std::string &context_, const std::error_code &ec_,
  50. const extended_error_ptr_t &next_ = {}) noexcept;
  51. } // namespace rotor