diff --git a/include/tins/exceptions.h b/include/tins/exceptions.h index 1b726b6..d23b00a 100644 --- a/include/tins/exceptions.h +++ b/include/tins/exceptions.h @@ -35,13 +35,22 @@ namespace Tins { /** - * \brief Exception thrown when an option is not found. + * \brief Base class for all libtins exceptions. */ -class option_not_found : public std::runtime_error { +class exception_base : public std::runtime_error { public: - option_not_found() + exception_base() : std::runtime_error(std::string()) { } + exception_base(const std::string& message) + : std::runtime_error(message) { } +}; + +/** + * \brief Exception thrown when an option is not found. + */ +class option_not_found : exception_base { +public: // try to avoid allocations by doing this. const char* what() const throw() { return "Option not found"; @@ -51,11 +60,8 @@ public: /** * \brief Exception thrown when a malformed packet is parsed. */ -class malformed_packet : public std::runtime_error { +class malformed_packet : public exception_base { public: - malformed_packet() - : std::runtime_error(std::string()) { } - const char* what() const throw() { return "Malformed packet"; } @@ -64,11 +70,8 @@ public: /** * \brief Exception thrown when a PDU is not found when using PDU::rfind_pdu. */ -class pdu_not_found : public std::runtime_error { +class pdu_not_found : public exception_base { public: - pdu_not_found() - : std::runtime_error(std::string()) { } - const char* what() const throw() { return "PDU not found"; } @@ -78,11 +81,8 @@ public: * \brief Exception thrown when PDU::send requires a valid interface, * but an invalid is used. */ -class invalid_interface : public std::runtime_error { +class invalid_interface : public exception_base { public: - invalid_interface() - : std::runtime_error(std::string()) { } - const char* what() const throw() { return "Invalid interface"; } @@ -91,10 +91,8 @@ public: /** * \brief Exception thrown when a field is not present in frame. */ -class field_not_present : public std::runtime_error { +class field_not_present : public exception_base { public: - field_not_present() - : std::runtime_error(std::string()) { } const char* what() const throw() { return "Field not present"; } @@ -103,35 +101,35 @@ public: /** * \brief Exception thrown when PacketSender fails to open a socket. */ -class socket_open_error : public std::runtime_error { +class socket_open_error : public exception_base { public: socket_open_error(const std::string &msg) - : std::runtime_error(msg) { } + : exception_base(msg) { } }; /** * \brief Exception thrown when PacketSender fails to close a socket. */ -class socket_close_error : public std::runtime_error { +class socket_close_error : exception_base { public: socket_close_error(const std::string &msg) - : std::runtime_error(msg) { } + : exception_base(msg) { } }; /** * \brief Exception thrown when PacketSender fails to write on a socket. */ -class socket_write_error : public std::runtime_error { +class socket_write_error : public exception_base { public: socket_write_error(const std::string &msg) - : std::runtime_error(msg) { } + : exception_base(msg) { } }; /** * \brief Exception thrown when an invalid socket type is provided * to PacketSender. */ -class invalid_socket_type : public std::exception { +class invalid_socket_type : public exception_base { public: const char *what() const throw() { return "The provided socket type is invalid"; @@ -142,7 +140,7 @@ public: * \brief Exception thrown when an unkown link layer PDU type is * found while sniffing. */ -class unknown_link_type : public std::exception { +class unknown_link_type : public exception_base { public: const char *what() const throw() { return "The sniffed link layer PDU type is unknown"; @@ -152,7 +150,7 @@ public: /** * \brief Exception thrown when a malformed option is found. */ -class malformed_option : public std::exception { +class malformed_option : public exception_base { public: const char *what() const throw() { return "Malformed option"; @@ -162,7 +160,7 @@ public: /** * \brief Exception thrown when a call to tins_cast fails. */ -class bad_tins_cast : public std::exception { +class bad_tins_cast : public exception_base { public: const char *what() const throw() { return "Bad Tins cast"; @@ -173,7 +171,7 @@ public: * \brief Exception thrown when sniffing a protocol that * has been disabled at compile time. */ -class protocol_disabled : public std::exception { +class protocol_disabled : public exception_base { public: const char *what() const throw() { return "Protocol disabled"; @@ -184,7 +182,7 @@ public: * \brief Exception thrown when a payload is too large to fit * into a PDUOption. */ -class option_payload_too_large : public std::exception { +class option_payload_too_large : public exception_base { public: const char *what() const throw() { return "Option payload too large"; @@ -196,7 +194,7 @@ namespace WPA2 { /** * \brief Exception thrown when an invalid WPA2 handshake is found. */ - class invalid_handshake : public std::exception { + class invalid_handshake : public exception_base { public: const char *what() const throw() { return "Invalid WPA2 handshake";