exception.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright Takatoshi Kondo 2015
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #if !defined(MQTT_EXCEPTION_HPP)
  7. #define MQTT_EXCEPTION_HPP
  8. #include <exception>
  9. #include <sstream>
  10. #include <boost/system/error_code.hpp>
  11. #include <boost/assert.hpp>
  12. #include <mqtt/namespace.hpp>
  13. #include <mqtt/utf8encoded_strings.hpp>
  14. namespace MQTT_NS {
  15. struct protocol_error : std::exception {
  16. char const* what() const noexcept override final {
  17. return "protocol error";
  18. }
  19. };
  20. struct malformed_packet_error : std::exception {
  21. char const* what() const noexcept override final {
  22. return "malformed packet error";
  23. }
  24. };
  25. struct remaining_length_error : std::exception {
  26. char const* what() const noexcept override final {
  27. return "remaining length error";
  28. }
  29. };
  30. struct variable_length_error : std::exception {
  31. char const* what() const noexcept override final {
  32. return "variable length error";
  33. }
  34. };
  35. struct utf8string_length_error : std::exception {
  36. char const* what() const noexcept override final {
  37. return "utf8string length error";
  38. }
  39. };
  40. struct utf8string_contents_error : std::exception {
  41. utf8string_contents_error(utf8string::validation r):r(r) {}
  42. char const* what() const noexcept override final {
  43. if (r == utf8string::validation::ill_formed) {
  44. return "utf8string ill_formed";
  45. }
  46. else {
  47. BOOST_ASSERT(r == utf8string::validation::well_formed_with_non_charactor);
  48. return "utf8string well_formed_with_non_charactor";
  49. }
  50. }
  51. utf8string::validation r;
  52. };
  53. struct will_message_length_error : std::exception {
  54. char const* what() const noexcept override final {
  55. return "will message length error";
  56. }
  57. };
  58. struct password_length_error : std::exception {
  59. char const* what() const noexcept override final {
  60. return "password length error";
  61. }
  62. };
  63. struct bytes_transferred_error : std::exception {
  64. bytes_transferred_error(std::size_t expected, std::size_t actual) {
  65. std::stringstream ss;
  66. ss << "bytes transferred error. expected: " << expected << " actual: " << actual;
  67. msg = ss.str();
  68. }
  69. char const* what() const noexcept override final {
  70. return msg.data();
  71. }
  72. std::string msg;
  73. };
  74. struct read_bytes_transferred_error : bytes_transferred_error {
  75. read_bytes_transferred_error(std::size_t expected, std::size_t actual)
  76. :bytes_transferred_error(expected, actual) {
  77. msg = "[read] " + msg;
  78. }
  79. };
  80. struct write_bytes_transferred_error : bytes_transferred_error {
  81. write_bytes_transferred_error(std::size_t expected, std::size_t actual)
  82. :bytes_transferred_error(expected, actual) {
  83. msg = "[write] " + msg;
  84. }
  85. };
  86. struct packet_id_exhausted_error : std::exception {
  87. char const* what() const noexcept override final {
  88. return "packet_id exhausted error";
  89. }
  90. };
  91. struct property_parse_error : std::exception {
  92. char const* what() const noexcept override final {
  93. return "property parse error";
  94. }
  95. };
  96. struct property_length_error : std::exception {
  97. char const* what() const noexcept override final {
  98. return "property length error";
  99. }
  100. };
  101. struct restore_type_error : std::exception {
  102. char const* what() const noexcept override final {
  103. return "restore type error";
  104. }
  105. };
  106. struct packet_size_error : std::exception {
  107. char const* what() const noexcept override final {
  108. return "packet size error";
  109. }
  110. };
  111. } // namespace MQTT_NS
  112. #endif // MQTT_EXCEPTION_HPP