exception.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /////////////////////////////////////////////////////////////////////////////
  2. /// @file exception.h
  3. /// Declaration of MQTT exception class
  4. /// @date May 1, 2013
  5. /// @author Frank Pagliughi
  6. /////////////////////////////////////////////////////////////////////////////
  7. /*******************************************************************************
  8. * Copyright (c) 2013-2019 Frank Pagliughi <fpagliughi@mindspring.com>
  9. *
  10. * All rights reserved. This program and the accompanying materials
  11. * are made available under the terms of the Eclipse Public License v1.0
  12. * and Eclipse Distribution License v1.0 which accompany this distribution.
  13. *
  14. * The Eclipse Public License is available at
  15. * http://www.eclipse.org/legal/epl-v10.html
  16. * and the Eclipse Distribution License is available at
  17. * http://www.eclipse.org/org/documents/edl-v10.php.
  18. *
  19. * Contributors:
  20. * Frank Pagliughi - initial implementation and documentation
  21. *******************************************************************************/
  22. #ifndef __mqtt_exception_h
  23. #define __mqtt_exception_h
  24. #include "MQTTAsync.h"
  25. #include "mqtt/types.h"
  26. #include <vector>
  27. #include <memory>
  28. #include <exception>
  29. #include <stdexcept>
  30. namespace mqtt {
  31. using bad_cast = std::bad_cast;
  32. /////////////////////////////////////////////////////////////////////////////
  33. /**
  34. * Base mqtt::exception.
  35. * This wraps the error codes which originate from the underlying C library.
  36. */
  37. class exception : public std::runtime_error
  38. {
  39. protected:
  40. /** The error return code from the C library */
  41. int rc_;
  42. /** The reason code from the server */
  43. ReasonCode reasonCode_;
  44. /** The error message from the C library */
  45. string msg_;
  46. public:
  47. /**
  48. * Creates an MQTT exception.
  49. * @param rc The error return code from the C library.
  50. */
  51. explicit exception(int rc)
  52. : exception(rc, error_str(rc)) {}
  53. /**
  54. * Creates an MQTT exception.
  55. * @param rc The error return code from the C library.
  56. * @param reasonCode The reason code from the server response.
  57. */
  58. explicit exception(int rc, ReasonCode reasonCode)
  59. : exception(rc, reasonCode, error_str(rc)) {}
  60. /**
  61. * Creates an MQTT exception.
  62. * @param rc The error return code from the C library.
  63. * @param msg The text message for the error.
  64. */
  65. exception(int rc, const string& msg)
  66. : std::runtime_error(printable_error(rc, ReasonCode::SUCCESS, msg)),
  67. rc_(rc), reasonCode_(ReasonCode::SUCCESS), msg_(msg) {}
  68. /**
  69. * Creates an MQTT exception.
  70. * @param rc The error return code from the C library.
  71. * @param reasonCode The reason code from the server
  72. * @param msg The text message for the error.
  73. */
  74. exception(int rc, ReasonCode reasonCode, const string& msg)
  75. : std::runtime_error(printable_error(rc, reasonCode, msg)),
  76. rc_(rc), reasonCode_(reasonCode), msg_(msg) {}
  77. /**
  78. * Gets an error message from an error code.
  79. * @param rc The error code from the C lib
  80. * @return A string explanation of the error
  81. */
  82. static string error_str(int rc) {
  83. const char *msg = ::MQTTAsync_strerror(rc);
  84. return msg ? string(msg) : string();
  85. }
  86. /**
  87. * Gets a string describing the MQTT v5 reason code.
  88. * @param reasonCode The MQTT v5 reason code.
  89. * @return A string describing the reason code.
  90. */
  91. static string reason_code_str(int reasonCode) {
  92. if (reasonCode != MQTTPP_V3_CODE) {
  93. auto msg = ::MQTTReasonCode_toString(MQTTReasonCodes(reasonCode));
  94. if (msg) return string(msg);
  95. }
  96. return string();
  97. }
  98. /**
  99. * Gets a detailed error message for an error code.
  100. * @param rc The error code from the C lib
  101. * @param msg An optional additional message. If none is provided, the
  102. * error_str message is used.
  103. * @return A string error message that includes the error code and an
  104. * explanation message.
  105. */
  106. static string printable_error(int rc, int reasonCode=ReasonCode::SUCCESS,
  107. const string& msg=string()) {
  108. string s = "MQTT error [" + std::to_string(rc) + "]";
  109. if (!msg.empty())
  110. s += string(": ") + msg;
  111. if (reasonCode != MQTTPP_V3_CODE && reasonCode != ReasonCode::SUCCESS)
  112. s += string(". Reason: ") + reason_code_str(reasonCode);
  113. return s;
  114. }
  115. /**
  116. * Returns the return code for this exception.
  117. */
  118. int get_return_code() const { return rc_; }
  119. /**
  120. * Gets a string of the error code.
  121. * @return A string of the error code.
  122. */
  123. string get_error_str() const { return error_str(rc_); }
  124. /**
  125. * Returns the reason code for this exception.
  126. * For MQTT v3 connections, this is actually the return code.
  127. */
  128. int get_reason_code() const {
  129. return reasonCode_ == MQTTPP_V3_CODE ? rc_ : reasonCode_;
  130. }
  131. /**
  132. * Gets a string for the reason code.
  133. * @return A string for the reason code.
  134. */
  135. string get_reason_code_str() const {
  136. return reason_code_str(reasonCode_);
  137. }
  138. /**
  139. * Returns the error message for this exception.
  140. */
  141. string get_message() const { return msg_; }
  142. /**
  143. * Gets a string representation of this exception.
  144. * @return A string representation of this exception.
  145. */
  146. string to_string() const { return string(what()); }
  147. };
  148. /////////////////////////////////////////////////////////////////////////////
  149. class missing_response : public std::runtime_error
  150. {
  151. public:
  152. missing_response(const string& rsp)
  153. : std::runtime_error("Missing "+rsp+" response") {}
  154. };
  155. /////////////////////////////////////////////////////////////////////////////
  156. /**
  157. * This exception is thrown by the implementor of the persistence interface
  158. * if there is a problem reading or writing persistent data.
  159. */
  160. class persistence_exception : public exception
  161. {
  162. public:
  163. /**
  164. * Creates an MQTT persistence exception.
  165. */
  166. persistence_exception() : exception(MQTTCLIENT_PERSISTENCE_ERROR) {}
  167. /**
  168. * Creates an MQTT persistence exception.
  169. * @param code The error code from the C library.
  170. */
  171. explicit persistence_exception(int code) : exception(code) {}
  172. /**
  173. * Creates an MQTT persistence exception.
  174. * @param msg The text message for the error.
  175. */
  176. explicit persistence_exception(const string& msg)
  177. : exception(MQTTCLIENT_PERSISTENCE_ERROR, msg) {}
  178. /**
  179. * Creates an MQTT persistence exception.
  180. * @param code The error code
  181. * @param msg The text message for the error.
  182. */
  183. persistence_exception(int code, const string& msg)
  184. : exception(code, msg) {}
  185. };
  186. /////////////////////////////////////////////////////////////////////////////
  187. /**
  188. * Thrown when a client is not authorized to perform an operation, or if
  189. * there is a problem with the security configuration.
  190. */
  191. class security_exception : public exception
  192. {
  193. public:
  194. /**
  195. * Creates an MQTT security exception
  196. * @param code The error code.
  197. */
  198. explicit security_exception(int code) : exception(code) {}
  199. /**
  200. * Creates an MQTT security exception
  201. * @param code The error code.
  202. * @param msg The text message for the error.
  203. */
  204. security_exception(int code, const string& msg) : exception(code, msg) {}
  205. };
  206. /////////////////////////////////////////////////////////////////////////////
  207. // end namespace mqtt
  208. }
  209. #endif // __mqtt_token_h