From 2f055e8894c6012243eea6a02ae91ba7395bccae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Korina=20=C5=A0imi=C4=8Devi=C4=87?= Date: Fri, 23 Feb 2024 13:29:56 +0100 Subject: [PATCH] Remove connection error codes Summary: related to T13767 Reviewers: ivica Reviewed By: ivica Subscribers: miljen, iljazovic Differential Revision: https://repo.mireo.local/D28117 --- include/async_mqtt5/error.hpp | 210 ---------------------------------- test/unit/error.cpp | 53 --------- 2 files changed, 263 deletions(-) diff --git a/include/async_mqtt5/error.hpp b/include/async_mqtt5/error.hpp index 2f09c43..5f80191 100644 --- a/include/async_mqtt5/error.hpp +++ b/include/async_mqtt5/error.hpp @@ -144,187 +144,6 @@ inline std::ostream& operator<<(std::ostream& os, const error& err) { } // end namespace client - -namespace connection { - -/** -* \brief Defines error codes related to MQTT client connection. -* -* \details Encapsulates errors encountered during the process of establishing a connection. -*/ -enum class error : int { - /** Connection has been successfully established */ - success = 0, - - /** An error occured during TLS handshake */ - tls_handshake_error = 1, - - /** An error occured during WebSocket handshake */ - websocket_handshake_error = 2, - - // CONNACK - /** The Server does not wish to reveal the reason for the failure */ - unspecified_error = 128, - - /** Data within the CONNECT packet could not be correctly parsed */ - malformed_packet = 129, - - /** Data in the CONNECT packet does not conform to this specification */ - protocol_error = 130, - - /** The CONNECT is valid but is not accepted by this Server */ - implementation_specific_error = 131, - - /** The Server does not support the version of the MQTT protocol requested by the Client */ - unsupported_protocol_version = 132, - - /** The Client Identifier is a valid string but is not allowed by the Server */ - client_identifier_not_valid = 133, - - /** The Server does not accept the User Name or Password specified by the Client */ - bad_username_or_password = 134, - - /** The Client is not authorized to connect */ - not_authorized = 135, - - /** The MQTT Server is not available */ - server_unavailable = 136, - - /** The Server is busy, try again later */ - server_busy = 137, - - /** This Client has been banned by administrative action */ - banned = 138, - - /** The authentication method is not supported or does not match the one currently in use. */ - bad_authentication_method = 140, - - /** The Will Topic Name is not malformed, but is not accepted by this Server */ - topic_name_invalid = 144, - - /** The CONNECT packet exceeded the maximum permissible size */ - packet_too_large = 149, - - /** An implementation or administrative imposed limit has been exceeded */ - quota_exceeded = 151, - - /** The Will Payload does not match the specified Payload Format Indicator */ - payload_format_invalid = 153, - - /** The Server does not support retained messages, and Will Retain was set to 1 */ - retain_not_supported = 154, - - /** The Server does not support the QoS set in Will QoS */ - qos_not_supported = 155, - - /** The Client should temporarily use another server */ - use_another_server = 156, - - /** The Client should permanently use another server */ - server_moved = 157, - - /** The connection rate limit has been exceeded */ - connection_rate_exceeded = 159 -}; - -inline std::string connection_error_to_string(error err) { - switch (err) { - case error::success: - return "Connection has been successfully established"; - case error::tls_handshake_error: - return "Connection failed: An error occured during TLS handshake"; - case error::websocket_handshake_error: - return "Connection failed: An error occured during WebSocket handshake"; - case error::unspecified_error: - return "Connection failed: The Server does not wish to reveal " - "the reason for the failure or none of the codes apply"; - case error::malformed_packet: - return "Connection failed: Data within the CONNECT packet " - "could not be correctly parsed"; - case error::protocol_error: - return "Connection failed: Data in the CONNECT packet does" - " not conform to this specification"; - case error::implementation_specific_error: - return "Connection failed : The CONNECT is valid but " - "is not accepted by this Server"; - case error::unsupported_protocol_version: - return "Connection failed: The Server does not support the " - "version of the MQTT protocol requested by the Client"; - case error::client_identifier_not_valid: - return "Connection failed: The Client Identifier is a valid " - "string but is not allowed by the Server"; - case error::bad_username_or_password: - return "Connection failed: The Server does not accept the User Name " - " or Password specified by the Client"; - case error::not_authorized: - return "Connection failed: The Client is not authorized to connect"; - case error::server_unavailable: - return "Connection failed: The MQTT Server is not available"; - case error::server_busy: - return "Connection failed: The Server is busy, try again later"; - case error::banned: - return "Connection failed: This Client has been banned " - "by administrative action, contact the server administrator"; - case error::bad_authentication_method: - return "Connection failed: The authentication method is not supported " - "or does not match the authentication method currently in use"; - case error::topic_name_invalid: - return "Connection failed: The Will Topic Name is not malformed, " - "but is not accepted by this Server"; - case error::packet_too_large: - return "Connection failed: The CONNECT packet exceeded the maximum " - " permissible size"; - case error::quota_exceeded: - return "Connection failed: An implementation or administrative " - "imposed limit has been exceeded"; - case error::payload_format_invalid: - return "Connection failed: The Will Payload does not match " - "the specified Payload Format Indicator"; - case error::retain_not_supported: - return "Connection failed: The Server does not support " - "retained messages, and Will Retain was set to 1"; - case error::qos_not_supported: - return "Connection failed: The Server does not support " - "the QoS set in Will QoS"; - case error::use_another_server: - return "Connection failed: The Client should temporarily " - "use another server"; - case error::server_moved: - return "Connection failed: The Client should permanently " - "use another server"; - case error::connection_rate_exceeded: - return "Connection failed: The connection rate limit " - "has been exceeded"; - default: - return "Unknown connection error"; - } -} - -struct connection_ec_category : public boost::system::error_category { - const char* name() const noexcept override { return "mqtt_connection_error"; } - std::string message(int ev) const noexcept override { - return connection_error_to_string(static_cast(ev)); - } -}; - -/// Returns the error category associated with \ref connection::error. -inline const connection_ec_category& get_error_code_category() { - static connection_ec_category cat; - return cat; -} - -/// Creates an \ref error_code from a \ref connection::error. -inline boost::system::error_code make_error_code(error r) { - return { static_cast(r), get_error_code_category() }; -} - -inline std::ostream& operator<<(std::ostream& os, const error& err) { - os << get_error_code_category().name() << ":" << static_cast(err); - return os; -} - -} // end namespace connection - } // end namespace async_mqtt5 namespace boost::system { @@ -332,36 +151,7 @@ namespace boost::system { template <> struct is_error_code_enum : std::true_type {}; -template <> -struct is_error_code_enum : std::true_type {}; - } // end namespace boost::system -namespace async_mqtt5 { - -inline bool is_not_recoverable(boost::system::error_code ec) { - using namespace connection; - return ec == boost::asio::error::no_recovery || - ec == error::tls_handshake_error || - ec == error::websocket_handshake_error || - ec == error::malformed_packet || - ec == error::implementation_specific_error || - ec == error::unsupported_protocol_version || - ec == error::client_identifier_not_valid || - ec == error::bad_username_or_password || - ec == error::not_authorized || - ec == error::banned || - ec == error::bad_authentication_method || - ec == error::topic_name_invalid || - ec == error::packet_too_large || - ec == error::quota_exceeded || - ec == error::payload_format_invalid || - ec == error::retain_not_supported || - ec == error::qos_not_supported || - ec == error::use_another_server || - ec == error::server_moved; -} - -} // end namespace async_mqtt5 #endif // !ASYNC_MQTT5_ERROR_HPP diff --git a/test/unit/error.cpp b/test/unit/error.cpp index 5fcf225..03b9277 100644 --- a/test/unit/error.cpp +++ b/test/unit/error.cpp @@ -50,59 +50,6 @@ BOOST_FIXTURE_TEST_CASE(client_ec_to_stream, client_error_codes) { } } -struct connection_error_codes { - const connection::connection_ec_category& cat = connection::get_error_code_category(); - - const std::vector ecs = { - connection::error::success, - connection::error::tls_handshake_error, - connection::error::websocket_handshake_error, - connection::error::unspecified_error, - connection::error::malformed_packet, - connection::error::protocol_error, - connection::error::implementation_specific_error, - connection::error::unsupported_protocol_version, - connection::error::client_identifier_not_valid, - connection::error::bad_username_or_password, - connection::error::not_authorized, - connection::error::server_unavailable, - connection::error::server_busy, - connection::error::banned, - connection::error::bad_authentication_method, - connection::error::topic_name_invalid, - connection::error::packet_too_large, - connection::error::quota_exceeded, - connection::error::payload_format_invalid, - connection::error::retain_not_supported, - connection::error::qos_not_supported, - connection::error::use_another_server, - connection::error::server_moved, - connection::error::connection_rate_exceeded - }; -}; - -BOOST_FIXTURE_TEST_CASE(connection_ec_to_string, connection_error_codes) { - // Ensure that all branches of the switch/case are covered - BOOST_TEST(cat.name()); - - constexpr auto default_output = "Unknown connection error"; - for (auto ec : ecs) - BOOST_TEST(cat.message(static_cast(ec)) != default_output); - - // default branch - BOOST_TEST(cat.message(3) == default_output); -} - -BOOST_FIXTURE_TEST_CASE(connection_ec_to_stream, connection_error_codes) { - for (auto ec : ecs) { - std::ostringstream stream; - stream << ec; - std::string expected = std::string(cat.name()) + ":" + - std::to_string(static_cast(ec)); - BOOST_TEST(stream.str() == expected); - } -} - using namespace reason_codes; struct client_reason_codes { const std::vector rcs = {