File: exception.h

package info (click to toggle)
paho.mqtt.cpp 1.5.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,672 kB
  • sloc: cpp: 13,068; ansic: 113; sh: 55; makefile: 22
file content (261 lines) | stat: -rw-r--r-- 8,526 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/////////////////////////////////////////////////////////////////////////////
/// @file exception.h
/// Declaration of MQTT exception class
/// @date May 1, 2013
/// @author Frank Pagliughi
/////////////////////////////////////////////////////////////////////////////

/*******************************************************************************
 * Copyright (c) 2013-2024 Frank Pagliughi <fpagliughi@mindspring.com>
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * and Eclipse Distribution License v1.0 which accompany this distribution.
 *
 * The Eclipse Public License is available at
 *    http://www.eclipse.org/legal/epl-v20.html
 * and the Eclipse Distribution License is available at
 *   http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *    Frank Pagliughi - initial implementation and documentation
 *******************************************************************************/

#ifndef __mqtt_exception_h
#define __mqtt_exception_h

#include <exception>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <vector>

#include "MQTTAsync.h"
#include "mqtt/types.h"

namespace mqtt {

/** Bring std::bad_cast into the mqtt namespace */
using bad_cast = std::bad_cast;

/////////////////////////////////////////////////////////////////////////////

/**
 * Base mqtt::exception.
 * This wraps the error codes which originate from the underlying C library.
 */
class exception : public std::runtime_error
{
protected:
    /** The error return code from the C library */
    int rc_;
    /** The reason code from the server */
    ReasonCode reasonCode_;
    /** The error message from the C library */
    string msg_;

    /** See if the return code is actually a reason code error value */
    static ReasonCode reason_code(int rc, ReasonCode reasonCode) {
        if (reasonCode == ReasonCode::SUCCESS && rc >= ReasonCode::UNSPECIFIED_ERROR)
            reasonCode = ReasonCode(rc);
        return reasonCode;
    }

public:
    /**
     * Creates an MQTT exception.
     * @param rc The error return code from the C library.
     */
    explicit exception(int rc) : exception(rc, error_str(rc)) {}
    /**
     * Creates an MQTT exception.
     * @param rc The error return code from the C library.
     * @param reasonCode The reason code from the server response.
     */
    explicit exception(int rc, ReasonCode reasonCode)
        : exception(rc, reasonCode, error_str(rc)) {}
    /**
     * Creates an MQTT exception.
     * @param rc The error return code from the C library.
     * @param msg The text message for the error.
     */
    exception(int rc, const string& msg) : exception(rc, ReasonCode::SUCCESS, msg) {}
    /**
     * Creates an MQTT exception.
     * @param rc The error return code from the C library.
     * @param reasonCode The reason code from the server
     * @param msg The text message for the error.
     */
    exception(int rc, ReasonCode reasonCode, const string& msg)
        : std::runtime_error(printable_error(rc, reasonCode, msg)),
          rc_{rc},
          reasonCode_{reason_code(rc, reasonCode)},
          msg_{msg} {}
    /**
     * Gets an error message from an error code.
     * @param rc The error code from the C lib
     * @return A string explanation of the error
     */
    static string error_str(int rc) {
        const char* msg = ::MQTTAsync_strerror(rc);
        return msg ? string(msg) : string();
    }
    /**
     * Gets a string describing the MQTT v5 reason code.
     * @param reasonCode The MQTT v5 reason code.
     * @return A string describing the reason code.
     */
    static string reason_code_str(int reasonCode) {
        auto msg = ::MQTTReasonCode_toString(MQTTReasonCodes(reasonCode));
        return (msg) ? string{msg} : string{};
    }
    /**
     * Gets a detailed error message for an error code.
     * @param rc The error code from the C lib
     * @param reasonCode The MQTT v5 reason code
     * @param msg An optional additional message. If none is provided, the
     *  		  error_str message is used.
     * @return A string error message that includes the error code and an
     *  	   explanation message.
     */
    static string printable_error(
        int rc, ReasonCode reasonCode = ReasonCode::SUCCESS, const string& msg = string()
    ) {
        reasonCode = reason_code(rc, reasonCode);

        string s = "MQTT error [" + std::to_string(rc) + "]";
        if (!msg.empty())
            s += string(": ") + msg;
        if (reasonCode != ReasonCode::SUCCESS)
            s += string(". ") + reason_code_str(reasonCode);
        return s;
    }
    /**
     * Returns the return code for this exception.
     */
    int get_return_code() const { return rc_; }
    /**
     * Gets a string of the error code.
     * @return A string of the error code.
     */
    string get_error_str() const { return error_str(rc_); }
    /**
     * Returns the reason code for this exception.
     * For MQTT v3 connections, this is actually the return code.
     */
    int get_reason_code() const { return reasonCode_; }
    /**
     * Gets a string for the reason code.
     * @return A string for the reason code.
     */
    string get_reason_code_str() const { return reason_code_str(reasonCode_); }
    /**
     * Returns the error message for this exception.
     */
    string get_message() const { return msg_; }
    /**
     * Gets a string representation of this exception.
     * @return A string representation of this exception.
     */
    string to_string() const { return string(what()); }
};

/**
 * Stream inserter writes a fairly verbose message
 * @param os The stream.
 * @param exc The exception to write.
 * @return A reference to the stream.
 */
inline std::ostream& operator<<(std::ostream& os, const exception& exc) {
    os << exc.what();
    return os;
}

/////////////////////////////////////////////////////////////////////////////

/**
 * Exception thrown when an expected server response is missing.
 */
class missing_response : public exception
{
public:
    /**
     * Create a missing response error.
     * @param rsp A string for the type of response expected.
     */
    missing_response(const string& rsp)
        : exception(MQTTASYNC_FAILURE, "Missing " + rsp + " response") {}
};

/////////////////////////////////////////////////////////////////////////////

/**
 * A timeout exception, particularly from the synchronous client.
 */
class timeout_error : public exception
{
public:
    /**
     * Create a timeout error.
     */
    timeout_error() : exception(MQTTASYNC_FAILURE, "Timeout") {}
};

/////////////////////////////////////////////////////////////////////////////

/**
 * This exception is thrown by the implementor of the persistence interface
 * if there is a problem reading or writing persistent data.
 */
class persistence_exception : public exception
{
public:
    /**
     * Creates an MQTT persistence exception.
     */
    persistence_exception() : exception(MQTTCLIENT_PERSISTENCE_ERROR) {}
    /**
     * Creates an MQTT persistence exception.
     * @param code The error code from the C library.
     */
    explicit persistence_exception(int code) : exception(code) {}
    /**
     * Creates an MQTT persistence exception.
     * @param msg The text message for the error.
     */
    explicit persistence_exception(const string& msg)
        : exception(MQTTCLIENT_PERSISTENCE_ERROR, msg) {}
    /**
     * Creates an MQTT persistence exception.
     * @param code The error code
     * @param msg The text message for the error.
     */
    persistence_exception(int code, const string& msg) : exception(code, msg) {}
};

/////////////////////////////////////////////////////////////////////////////

/**
 * Thrown when a client is not authorized to perform an operation, or if
 * there is a problem with the security configuration.
 */
class security_exception : public exception
{
public:
    /**
     * Creates an MQTT security exception
     * @param code The error code.
     */
    explicit security_exception(int code) : exception(code) {}
    /**
     * Creates an MQTT security exception
     * @param code The error code.
     * @param msg The text message for the error.
     */
    security_exception(int code, const string& msg) : exception(code, msg) {}
};

/////////////////////////////////////////////////////////////////////////////
}  // namespace mqtt

#endif  // __mqtt_exception_h