extended_error.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #pragma once
  2. //
  3. // Copyright (c) 2021-2024 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 "message.h"
  9. #include <string>
  10. #include <system_error>
  11. #if defined(_MSC_VER)
  12. #pragma warning(push)
  13. #pragma warning(disable : 4251)
  14. #endif
  15. namespace rotor {
  16. struct extended_error_t;
  17. struct message_stringifier_t;
  18. /** \brief intrusive pointer to extended error type */
  19. using extended_error_ptr_t = intrusive_ptr_t<extended_error_t>;
  20. /** \struct extended_error_t
  21. * \brief Holds string context, error_code and the pointer to the following error.
  22. *
  23. *
  24. * This is extension over std::error_code, to make it possible to identify the
  25. * context of the error (usually it is actor identity), and make it possible to
  26. * construct the chain of failures, that's why there is a smart pointer to the
  27. * next error.
  28. *
  29. */
  30. struct ROTOR_API extended_error_t : arc_base_t<extended_error_t> {
  31. /** \brief error context, usually actor identity */
  32. std::string context;
  33. /** \brief abstract error code, describing occurred error */
  34. std::error_code ec;
  35. /** \brief pointer to the parent error */
  36. extended_error_ptr_t next;
  37. /** \brief pointer request caused error */
  38. message_ptr_t request;
  39. /** \brief extened error constructor */
  40. extended_error_t(const std::string &context_, const std::error_code &ec_, const extended_error_ptr_t &next_ = {},
  41. const message_ptr_t &request_ = {}) noexcept
  42. : context{context_}, ec{ec_}, next{next_}, request{request_} {}
  43. /** \brief human-readable detailed description of the error
  44. *
  45. * First, it stringifies own error in accordance with the context.
  46. *
  47. * Second, it recursively ask details on all following errors, appending them
  48. * into the result. The result string is returned.
  49. */
  50. std::string message(const message_stringifier_t *stringifier = nullptr) const noexcept;
  51. /**
  52. * \brief returns root (inner-most) extended error
  53. */
  54. extended_error_ptr_t root() const noexcept;
  55. };
  56. /** \brief constructs smart pointer to the extened error */
  57. ROTOR_API extended_error_ptr_t make_error(const std::string &context_, const std::error_code &ec_,
  58. const extended_error_ptr_t &next_ = {},
  59. const message_ptr_t &request_ = {}) noexcept;
  60. } // namespace rotor
  61. #if defined(_MSC_VER)
  62. #pragma warning(pop)
  63. #endif