paho-mqtt-cpp
MQTT C++ Client for POSIX and Windows
exception.h
Go to the documentation of this file.
1 
8 /*******************************************************************************
9  * Copyright (c) 2013-2019 Frank Pagliughi <fpagliughi@mindspring.com>
10  *
11  * All rights reserved. This program and the accompanying materials
12  * are made available under the terms of the Eclipse Public License v2.0
13  * and Eclipse Distribution License v1.0 which accompany this distribution.
14  *
15  * The Eclipse Public License is available at
16  * http://www.eclipse.org/legal/epl-v20.html
17  * and the Eclipse Distribution License is available at
18  * http://www.eclipse.org/org/documents/edl-v10.php.
19  *
20  * Contributors:
21  * Frank Pagliughi - initial implementation and documentation
22  *******************************************************************************/
23 
24 #ifndef __mqtt_exception_h
25 #define __mqtt_exception_h
26 
27 #include "MQTTAsync.h"
28 #include "mqtt/types.h"
29 #include <iostream>
30 #include <vector>
31 #include <memory>
32 #include <exception>
33 #include <stdexcept>
34 
35 namespace mqtt {
36 
39 
41 
46 class exception : public std::runtime_error
47 {
48 protected:
50  int rc_;
54  string msg_;
55 
56 public:
61  explicit exception(int rc)
62  : exception(rc, error_str(rc)) {}
68  explicit exception(int rc, ReasonCode reasonCode)
69  : exception(rc, reasonCode, error_str(rc)) {}
75  exception(int rc, const string& msg)
76  : std::runtime_error(printable_error(rc, ReasonCode::SUCCESS, msg)),
77  rc_(rc), reasonCode_(ReasonCode::SUCCESS), msg_(msg) {}
84  exception(int rc, ReasonCode reasonCode, const string& msg)
85  : std::runtime_error(printable_error(rc, reasonCode, msg)),
86  rc_(rc), reasonCode_(reasonCode), msg_(msg) {}
92  static string error_str(int rc) {
93  const char *msg = ::MQTTAsync_strerror(rc);
94  return msg ? string(msg) : string();
95  }
101  static string reason_code_str(int reasonCode) {
102  if (reasonCode != MQTTPP_V3_CODE) {
103  auto msg = ::MQTTReasonCode_toString(MQTTReasonCodes(reasonCode));
104  if (msg) return string(msg);
105  }
106  return string();
107  }
117  static string printable_error(int rc, int reasonCode=ReasonCode::SUCCESS,
118  const string& msg=string()) {
119  string s = "MQTT error [" + std::to_string(rc) + "]";
120  if (!msg.empty())
121  s += string(": ") + msg;
122  if (reasonCode != MQTTPP_V3_CODE && reasonCode != ReasonCode::SUCCESS)
123  s += string(". Reason: ") + reason_code_str(reasonCode);
124  return s;
125  }
129  int get_return_code() const { return rc_; }
134  string get_error_str() const { return error_str(rc_); }
139  int get_reason_code() const {
141  }
146  string get_reason_code_str() const {
148  }
152  string get_message() const { return msg_; }
157  string to_string() const { return string(what()); }
158 };
159 
166 inline std::ostream& operator<<(std::ostream& os, const exception& exc) {
167  os << exc.what();
168  return os;
169 }
170 
172 
177 {
178 public:
183  missing_response(const string& rsp)
184  : exception(MQTTASYNC_FAILURE, "Missing "+rsp+" response") {}
185 };
186 
187 
189 
193 class timeout_error : public exception
194 {
195 public:
199  timeout_error() : exception(MQTTASYNC_FAILURE, "Timeout") {}
200 };
201 
203 
209 {
210 public:
214  persistence_exception() : exception(MQTTCLIENT_PERSISTENCE_ERROR) {}
219  explicit persistence_exception(int code) : exception(code) {}
224  explicit persistence_exception(const string& msg)
225  : exception(MQTTCLIENT_PERSISTENCE_ERROR, msg) {}
231  persistence_exception(int code, const string& msg)
232  : exception(code, msg) {}
233 };
234 
236 
242 {
243 public:
248  explicit security_exception(int code) : exception(code) {}
254  security_exception(int code, const string& msg) : exception(code, msg) {}
255 };
256 
258 // end namespace mqtt
259 }
260 
261 #endif // __mqtt_token_h
Definition: exception.h:47
exception(int rc)
Definition: exception.h:61
static string reason_code_str(int reasonCode)
Definition: exception.h:101
string to_string() const
Definition: exception.h:157
static string error_str(int rc)
Definition: exception.h:92
static string printable_error(int rc, int reasonCode=ReasonCode::SUCCESS, const string &msg=string())
Definition: exception.h:117
int get_return_code() const
Definition: exception.h:129
int rc_
Definition: exception.h:50
string msg_
Definition: exception.h:54
ReasonCode reasonCode_
Definition: exception.h:52
exception(int rc, ReasonCode reasonCode)
Definition: exception.h:68
int get_reason_code() const
Definition: exception.h:139
string get_reason_code_str() const
Definition: exception.h:146
string get_message() const
Definition: exception.h:152
exception(int rc, const string &msg)
Definition: exception.h:75
string get_error_str() const
Definition: exception.h:134
exception(int rc, ReasonCode reasonCode, const string &msg)
Definition: exception.h:84
Definition: exception.h:177
missing_response(const string &rsp)
Definition: exception.h:183
Definition: exception.h:209
persistence_exception(const string &msg)
Definition: exception.h:224
persistence_exception(int code)
Definition: exception.h:219
persistence_exception()
Definition: exception.h:214
persistence_exception(int code, const string &msg)
Definition: exception.h:231
Definition: exception.h:242
security_exception(int code)
Definition: exception.h:248
security_exception(int code, const string &msg)
Definition: exception.h:254
Definition: exception.h:194
timeout_error()
Definition: exception.h:199
Definition: async_client.h:49
ReasonCode
Definition: types.h:57
@ SUCCESS
Definition: types.h:58
@ MQTTPP_V3_CODE
Definition: types.h:103
std::bad_cast bad_cast
Definition: exception.h:38
string to_string(const char *cstr)
Definition: types.h:175
std::string string
Definition: types.h:40
std::ostream & operator<<(std::ostream &os, const buffer_ref< T > &buf)
Definition: buffer_ref.h:279