Loading...
Searching...
No Matches
exception.h
Go to the documentation of this file.
/*******************************************************************************
* 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 {
using bad_cast = std::bad_cast;
class exception : public std::runtime_error
{
protected:
int rc_;
ReasonCode reasonCode_;
string msg_;
static ReasonCode reason_code(int rc, ReasonCode reasonCode) {
if (reasonCode == ReasonCode::SUCCESS && rc >= ReasonCode::UNSPECIFIED_ERROR)
reasonCode = ReasonCode(rc);
return reasonCode;
}
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) : exception(rc, ReasonCode::SUCCESS, msg) {}
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} {}
static string error_str(int rc) {
const char* msg = ::MQTTAsync_strerror(rc);
return msg ? string(msg) : string();
}
static string reason_code_str(int reasonCode) {
auto msg = ::MQTTReasonCode_toString(MQTTReasonCodes(reasonCode));
return (msg) ? string{msg} : string{};
}
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;
}
int get_return_code() const { return rc_; }
string get_error_str() const { return error_str(rc_); }
int get_reason_code() const { return 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) {}
};
} // namespace mqtt
#endif // __mqtt_exception_h
mqtt::exception
Definition exception.h:48
mqtt::exception::exception
exception(int rc)
Definition exception.h:69
mqtt::exception::reason_code_str
static string reason_code_str(int reasonCode)
Definition exception.h:108
mqtt::exception::to_string
string to_string() const
Definition exception.h:160
mqtt::exception::error_str
static string error_str(int rc)
Definition exception.h:99
mqtt::exception::get_return_code
int get_return_code() const
Definition exception.h:136
mqtt::exception::rc_
int rc_
Definition exception.h:51
mqtt::exception::msg_
string msg_
Definition exception.h:55
mqtt::exception::reasonCode_
ReasonCode reasonCode_
Definition exception.h:53
mqtt::exception::exception
exception(int rc, ReasonCode reasonCode)
Definition exception.h:75
mqtt::exception::reason_code
static ReasonCode reason_code(int rc, ReasonCode reasonCode)
Definition exception.h:58
mqtt::exception::get_reason_code
int get_reason_code() const
Definition exception.h:146
mqtt::exception::get_reason_code_str
string get_reason_code_str() const
Definition exception.h:151
mqtt::exception::printable_error
static string printable_error(int rc, ReasonCode reasonCode=ReasonCode::SUCCESS, const string &msg=string())
Definition exception.h:121
mqtt::exception::get_message
string get_message() const
Definition exception.h:155
mqtt::exception::exception
exception(int rc, const string &msg)
Definition exception.h:82
mqtt::exception::get_error_str
string get_error_str() const
Definition exception.h:141
mqtt::exception::exception
exception(int rc, ReasonCode reasonCode, const string &msg)
Definition exception.h:89
mqtt::missing_response
Definition exception.h:180
mqtt::missing_response::missing_response
missing_response(const string &rsp)
Definition exception.h:186
mqtt::persistence_exception
Definition exception.h:211
mqtt::persistence_exception::persistence_exception
persistence_exception(const string &msg)
Definition exception.h:226
mqtt::persistence_exception::persistence_exception
persistence_exception(int code)
Definition exception.h:221
mqtt::persistence_exception::persistence_exception
persistence_exception()
Definition exception.h:216
mqtt::persistence_exception::persistence_exception
persistence_exception(int code, const string &msg)
Definition exception.h:233
mqtt::security_exception
Definition exception.h:243
mqtt::security_exception::security_exception
security_exception(int code)
Definition exception.h:249
mqtt::security_exception::security_exception
security_exception(int code, const string &msg)
Definition exception.h:255
mqtt::timeout_error
Definition exception.h:196
mqtt::timeout_error::timeout_error
timeout_error()
Definition exception.h:201
mqtt
Definition async_client.h:60
mqtt::ReasonCode
ReasonCode
Definition reason_code.h:39
mqtt::SUCCESS
@ SUCCESS
Definition reason_code.h:40
mqtt::UNSPECIFIED_ERROR
@ UNSPECIFIED_ERROR
Definition reason_code.h:50
mqtt::bad_cast
std::bad_cast bad_cast
Definition exception.h:39
mqtt::string
std::string string
Definition types.h:43
mqtt::operator<<
std::ostream & operator<<(std::ostream &os, const buffer_ref< T > &buf)
Definition buffer_ref.h:286
types.h