123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- #ifndef __mqtt_exception_h
- #define __mqtt_exception_h
- #include "MQTTAsync.h"
- #include "mqtt/types.h"
- #include <iostream>
- #include <vector>
- #include <memory>
- #include <exception>
- #include <stdexcept>
- namespace mqtt {
- using bad_cast = std::bad_cast;
- class exception : public std::runtime_error
- {
- protected:
-
- int rc_;
-
- ReasonCode reasonCode_;
-
- string msg_;
- public:
-
- explicit exception(int rc)
- : exception(rc, error_str(rc)) {}
-
- explicit exception(int rc, ReasonCode reasonCode)
- : exception(rc, reasonCode, error_str(rc)) {}
-
- exception(int rc, const string& msg)
- : std::runtime_error(printable_error(rc, ReasonCode::SUCCESS, msg)),
- rc_(rc), reasonCode_(ReasonCode::SUCCESS), msg_(msg) {}
-
- exception(int rc, ReasonCode reasonCode, const string& msg)
- : std::runtime_error(printable_error(rc, reasonCode, msg)),
- rc_(rc), reasonCode_(reasonCode), msg_(msg) {}
-
- static string error_str(int rc) {
- const char *msg = ::MQTTAsync_strerror(rc);
- return msg ? string(msg) : string();
- }
-
- static string reason_code_str(int reasonCode) {
- if (reasonCode != MQTTPP_V3_CODE) {
- auto msg = ::MQTTReasonCode_toString(MQTTReasonCodes(reasonCode));
- if (msg) return string(msg);
- }
- return string();
- }
-
- static string printable_error(int rc, int reasonCode=ReasonCode::SUCCESS,
- const string& msg=string()) {
- string s = "MQTT error [" + std::to_string(rc) + "]";
- if (!msg.empty())
- s += string(": ") + msg;
- if (reasonCode != MQTTPP_V3_CODE && reasonCode != ReasonCode::SUCCESS)
- s += string(". Reason: ") + reason_code_str(reasonCode);
- return s;
- }
-
- int get_return_code() const { return rc_; }
-
- string get_error_str() const { return error_str(rc_); }
-
- int get_reason_code() const {
- return reasonCode_ == MQTTPP_V3_CODE ? rc_ : reasonCode_;
- }
-
- string get_reason_code_str() const {
- return reason_code_str(reasonCode_);
- }
-
- string get_message() const { return msg_; }
-
- string to_string() const { return string(what()); }
- };
- inline std::ostream& operator<<(std::ostream& os, const exception& exc) {
- os << exc.what();
- return os;
- }
- class missing_response : public exception
- {
- public:
-
- missing_response(const string& rsp)
- : exception(MQTTASYNC_FAILURE, "Missing "+rsp+" response") {}
- };
- class timeout_error : public exception
- {
- public:
-
- timeout_error() : exception(MQTTASYNC_FAILURE, "Timeout") {}
- };
- class persistence_exception : public exception
- {
- public:
-
- persistence_exception() : exception(MQTTCLIENT_PERSISTENCE_ERROR) {}
-
- explicit persistence_exception(int code) : exception(code) {}
-
- explicit persistence_exception(const string& msg)
- : exception(MQTTCLIENT_PERSISTENCE_ERROR, msg) {}
-
- persistence_exception(int code, const string& msg)
- : exception(code, msg) {}
- };
- class security_exception : public exception
- {
- public:
-
- explicit security_exception(int code) : exception(code) {}
-
- security_exception(int code, const string& msg) : exception(code, msg) {}
- };
- }
- #endif
|