configor_exception.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright (c) 2018-2020 configor - Nomango
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. #pragma once
  21. #include <stdexcept> // std::runtime_error, std::exception_ptr, std::rethrow_exception
  22. #include <string> // std::string
  23. #ifndef CONFIGOR_ASSERT
  24. #include <cassert> // assert
  25. #define CONFIGOR_ASSERT(...) assert(__VA_ARGS__)
  26. #endif
  27. namespace configor
  28. {
  29. //
  30. // exceptions
  31. //
  32. class configor_exception : public std::runtime_error
  33. {
  34. public:
  35. explicit configor_exception(const char* message)
  36. : std::runtime_error(message)
  37. {
  38. }
  39. explicit configor_exception(const std::string& message)
  40. : std::runtime_error(message)
  41. {
  42. }
  43. };
  44. class configor_type_error : public configor_exception
  45. {
  46. public:
  47. explicit configor_type_error(const std::string& message)
  48. : configor_exception("value type error: " + message)
  49. {
  50. }
  51. };
  52. class configor_invalid_key : public configor_exception
  53. {
  54. public:
  55. explicit configor_invalid_key(const std::string& message)
  56. : configor_exception("invalid value key error: " + message)
  57. {
  58. }
  59. };
  60. class configor_invalid_iterator : public configor_exception
  61. {
  62. public:
  63. explicit configor_invalid_iterator(const std::string& message)
  64. : configor_exception("invalid value iterator error: " + message)
  65. {
  66. }
  67. };
  68. class configor_deserialization_error : public configor_exception
  69. {
  70. public:
  71. explicit configor_deserialization_error(const std::string& message)
  72. : configor_exception("value deserialization error: " + message)
  73. {
  74. }
  75. };
  76. class configor_serialization_error : public configor_exception
  77. {
  78. public:
  79. explicit configor_serialization_error(const std::string& message)
  80. : configor_exception("value serialization error: " + message)
  81. {
  82. }
  83. };
  84. //
  85. // error handler
  86. //
  87. enum class error_policy
  88. {
  89. strict = 1, // throw exceptions
  90. record = 2, // record error message
  91. ignore = 3, // ignore all errors
  92. };
  93. class error_handler
  94. {
  95. public:
  96. virtual void handle(std::exception_ptr eptr) = 0;
  97. };
  98. template <error_policy _Policy>
  99. class error_handler_with;
  100. template <>
  101. class error_handler_with<error_policy::strict> : public error_handler
  102. {
  103. public:
  104. virtual void handle(std::exception_ptr eptr) override
  105. {
  106. std::rethrow_exception(eptr);
  107. }
  108. };
  109. template <>
  110. class error_handler_with<error_policy::ignore> : public error_handler
  111. {
  112. public:
  113. virtual void handle(std::exception_ptr eptr) override
  114. {
  115. // DO NOTHING
  116. }
  117. };
  118. template <>
  119. class error_handler_with<error_policy::record> : public error_handler
  120. {
  121. public:
  122. virtual void handle(std::exception_ptr eptr) override
  123. {
  124. try
  125. {
  126. if (eptr)
  127. {
  128. std::rethrow_exception(eptr);
  129. }
  130. }
  131. catch (const configor_exception& e)
  132. {
  133. this->error = e.what();
  134. }
  135. }
  136. std::string error;
  137. };
  138. } // namespace configor