diff options
Diffstat (limited to 'src/Common')
-rw-r--r-- | src/Common/Exception.cpp | 34 | ||||
-rw-r--r-- | src/Common/Exception.h | 26 |
2 files changed, 42 insertions, 18 deletions
diff --git a/src/Common/Exception.cpp b/src/Common/Exception.cpp index 002ba82..dc04f7d 100644 --- a/src/Common/Exception.cpp +++ b/src/Common/Exception.cpp @@ -19,25 +19,41 @@ #include "Exception.h" +#include <cstring> +#include <gnutls/gnutls.h> + namespace Mad { namespace Common { -const char* Exception::strerror() const { +std::string Exception::strerror() const { + std::string ret; + + if(!where.empty()) + ret = where + ": "; + switch(errorCode) { case SUCCESS: - return "Success"; + return ret + "Success"; case UNEXPECTED_PACKET: - return "An unexpected packet was received"; + return ret + "An unexpected packet was received"; case INVALID_ACTION: - return "The action is invalid"; + return ret + "The action is invalid"; + case NOT_AVAILABLE: + return ret + "Not available"; + case NOT_FINISHED: + return ret + "Not finished"; + case INTERNAL_ERRNO: + return ret + "Internal error: " + std::strerror(subCode); + case INTERNAL_GNUTLS: + return ret + "GnuTLS error: " + gnutls_strerror(subCode); + case INVALID_ADDRESS: + return ret + "Invalid address"; case ALREADY_IDENTIFIED: - return "The host is already identified"; + return ret + "The host is already identified"; case UNKNOWN_DAEMON: - return "The daemon is unknown"; - case DAEMON_INACTIVE: - return "The daemon is inactive"; + return ret + "The daemon is unknown"; default: - return "Unknown error"; + return ret + "Unknown error"; } } diff --git a/src/Common/Exception.h b/src/Common/Exception.h index 25bc56c..9862f9b 100644 --- a/src/Common/Exception.h +++ b/src/Common/Exception.h @@ -20,31 +20,39 @@ #ifndef MAD_COMMON_EXCEPTION_H_ #define MAD_COMMON_EXCEPTION_H_ +#include <string> + namespace Mad { namespace Common { class Exception { public: enum ErrorCode { - SUCCESS = 0x0000, UNEXPECTED_PACKET = 0x0001, INVALID_ACTION = 0x0002, - NOT_FINISHED = 0x0010, - ALREADY_IDENTIFIED = 0x0020, UNKNOWN_DAEMON = 0x0021, DAEMON_INACTIVE = 0x0022 + SUCCESS = 0x0000, UNEXPECTED_PACKET = 0x0001, INVALID_ACTION = 0x0002, NOT_AVAILABLE = 0x0003, NOT_FINISHED = 0x0004, + INTERNAL_ERRNO = 0x0010, INTERNAL_GNUTLS = 0x0011, + INVALID_ADDRESS = 0x0020, + ALREADY_IDENTIFIED = 0x0030, UNKNOWN_DAEMON = 0x0031 }; private: + std::string where; + ErrorCode errorCode; - unsigned long subCode; - unsigned long subSubCode; + long subCode; + long subSubCode; public: - Exception(ErrorCode errorCode0, unsigned long subCode0 = 0, unsigned long subSubCode0 = 0) : errorCode(errorCode0), subCode(subCode0), subSubCode(subSubCode0) {} + Exception(const std::string &where0, ErrorCode errorCode0, long subCode0 = 0, long subSubCode0 = 0) + : where(where0), errorCode(errorCode0), subCode(subCode0), subSubCode(subSubCode0) {} + Exception(ErrorCode errorCode0, long subCode0 = 0, long subSubCode0 = 0) : errorCode(errorCode0), subCode(subCode0), subSubCode(subSubCode0) {} virtual ~Exception() {} + const std::string& getWhere() const {return where;} ErrorCode getErrorCode() const {return errorCode;} - unsigned long getSubCode() const {return subCode;} - unsigned long getSubSubCode() const {return subSubCode;} + long getSubCode() const {return subCode;} + long getSubSubCode() const {return subSubCode;} - const char* strerror() const; + std::string strerror() const; }; } |