PahoMqttCpp
MQTT C++ Client for POSIX and Windows
Loading...
Searching...
No Matches
exception.h
Go to the documentation of this file.
1
7
8/*******************************************************************************
9 * Copyright (c) 2013-2024 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 <exception>
28#include <iostream>
29#include <memory>
30#include <stdexcept>
31#include <vector>
32
33#include "MQTTAsync.h"
34#include "mqtt/types.h"
35
36namespace mqtt {
37
39using bad_cast = std::bad_cast;
40
42
47class exception : public std::runtime_error
48{
49protected:
51 int rc_;
55 string msg_;
56
58 static ReasonCode reason_code(int rc, ReasonCode reasonCode) {
59 if (reasonCode == ReasonCode::SUCCESS && rc >= ReasonCode::UNSPECIFIED_ERROR)
60 reasonCode = ReasonCode(rc);
61 return reasonCode;
62 }
63
64public:
69 explicit exception(int rc) : exception(rc, error_str(rc)) {}
75 explicit exception(int rc, ReasonCode reasonCode)
76 : exception(rc, reasonCode, error_str(rc)) {}
77
82 exception(int rc, const string& msg) : exception(rc, ReasonCode::SUCCESS, msg) {}
89 exception(int rc, ReasonCode reasonCode, const string& msg)
90 : std::runtime_error(printable_error(rc, reasonCode, msg)),
91 rc_{rc},
92 reasonCode_{reason_code(rc, reasonCode)},
93 msg_{msg} {}
94
99 static string error_str(int rc) {
100 const char* msg = ::MQTTAsync_strerror(rc);
101 return msg ? string(msg) : string();
102 }
103
108 static string reason_code_str(int reasonCode) {
109 auto msg = ::MQTTReasonCode_toString(MQTTReasonCodes(reasonCode));
110 return (msg) ? string{msg} : string{};
111 }
112
121 static string printable_error(
122 int rc, ReasonCode reasonCode = ReasonCode::SUCCESS, const string& msg = string()
123 ) {
124 reasonCode = reason_code(rc, reasonCode);
125
126 string s = "MQTT error [" + std::to_string(rc) + "]";
127 if (!msg.empty())
128 s += string(": ") + msg;
129 if (reasonCode != ReasonCode::SUCCESS)
130 s += string(". ") + reason_code_str(reasonCode);
131 return s;
132 }
133
136 int get_return_code() const { return rc_; }
141 string get_error_str() const { return error_str(rc_); }
146 int get_reason_code() const { return reasonCode_; }
155 string get_message() const { return msg_; }
160 string to_string() const { return string(what()); }
161};
162
169inline std::ostream& operator<<(std::ostream& os, const exception& exc) {
170 os << exc.what();
171 return os;
172}
173
175
180{
181public:
186 missing_response(const string& rsp)
187 : exception(MQTTASYNC_FAILURE, "Missing " + rsp + " response") {}
188};
189
191
196{
197public:
201 timeout_error() : exception(MQTTASYNC_FAILURE, "Timeout") {}
202};
203
205
211{
212public:
216 persistence_exception() : exception(MQTTCLIENT_PERSISTENCE_ERROR) {}
221 explicit persistence_exception(int code) : exception(code) {}
226 explicit persistence_exception(const string& msg)
227 : exception(MQTTCLIENT_PERSISTENCE_ERROR, msg) {}
228
233 persistence_exception(int code, const string& msg) : exception(code, msg) {}
234};
235
237
243{
244public:
249 explicit security_exception(int code) : exception(code) {}
255 security_exception(int code, const string& msg) : exception(code, msg) {}
256};
257
259} // namespace mqtt
260
261#endif // __mqtt_exception_h
Definition exception.h:48
exception(int rc)
Definition exception.h:69
static string reason_code_str(int reasonCode)
Definition exception.h:108
string to_string() const
Definition exception.h:160
static string error_str(int rc)
Definition exception.h:99
int get_return_code() const
Definition exception.h:136
int rc_
Definition exception.h:51
string msg_
Definition exception.h:55
ReasonCode reasonCode_
Definition exception.h:53
exception(int rc, ReasonCode reasonCode)
Definition exception.h:75
static ReasonCode reason_code(int rc, ReasonCode reasonCode)
Definition exception.h:58
int get_reason_code() const
Definition exception.h:146
string get_reason_code_str() const
Definition exception.h:151
static string printable_error(int rc, ReasonCode reasonCode=ReasonCode::SUCCESS, const string &msg=string())
Definition exception.h:121
string get_message() const
Definition exception.h:155
exception(int rc, const string &msg)
Definition exception.h:82
string get_error_str() const
Definition exception.h:141
exception(int rc, ReasonCode reasonCode, const string &msg)
Definition exception.h:89
missing_response(const string &rsp)
Definition exception.h:186
persistence_exception(const string &msg)
Definition exception.h:226
persistence_exception(int code)
Definition exception.h:221
persistence_exception()
Definition exception.h:216
persistence_exception(int code, const string &msg)
Definition exception.h:233
security_exception(int code)
Definition exception.h:249
security_exception(int code, const string &msg)
Definition exception.h:255
timeout_error()
Definition exception.h:201
Definition async_client.h:60
ReasonCode
Definition reason_code.h:39
@ SUCCESS
Definition reason_code.h:40
@ UNSPECIFIED_ERROR
Definition reason_code.h:50
std::bad_cast bad_cast
Definition exception.h:39
std::string string
Definition types.h:43
std::ostream & operator<<(std::ostream &os, const buffer_ref< T > &buf)
Definition buffer_ref.h:286