From e27e529e589ef89fbe010ebf7c5635ec2873f64f Mon Sep 17 00:00:00 2001 From: Determinant Date: Wed, 12 Jun 2019 19:14:40 -0400 Subject: WIP: error handling --- include/salticidae/util.h | 48 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) (limited to 'include/salticidae/util.h') diff --git a/include/salticidae/util.h b/include/salticidae/util.h index 19779b0..0ddf5be 100644 --- a/include/salticidae/util.h +++ b/include/salticidae/util.h @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -52,18 +53,57 @@ std::vector trim_all(const std::vector &ss); std::string vstringprintf(const char *fmt, va_list ap); std::string stringprintf(const char *fmt, ...); +enum SalticidaeErrorCode { + SALTI_NORMAL, + SALTI_ERROR_GENERIC, + SALTI_ERROR_ACCEPT, + SALTI_ERROR_LISTEN, + SALTI_ERROR_CONNECT, + SALTI_ERROR_PEER_ALREADY_EXISTS, + SALTI_ERROR_PEER_NOT_EXISTS, + SALTI_ERROR_NETADDR_INVALID, + SALTI_ERROR_OPTVAL_INVALID, + SALTI_ERROR_OPTNAME_ALREADY_EXISTS, + SALTI_ERROR_OPT_UNKNOWN_ACTION, + SALTI_ERROR_CONFIG_LINE_TOO_LONG, + SALTI_ERROR_OPT_INVALID +}; + +extern const char *SALTICIDAE_ERROR_STRINGS[]; + class SalticidaeError: public std::exception { std::string msg; + int code; + int oscode; + public: - SalticidaeError(); + SalticidaeError() : code(SALTI_NORMAL) {} template - SalticidaeError(const std::string &fmt, Args... args) { + SalticidaeError(const std::string &fmt, Args... args): code(SALTI_ERROR_GENERIC) { msg = stringprintf(fmt.c_str(), args...); } + SalticidaeError(int code, int oscode = 0): code(code), oscode(oscode) { + if (oscode) + msg = stringprintf("%s: %s", SALTICIDAE_ERROR_STRINGS[code], strerror(oscode)); + else + msg = SALTICIDAE_ERROR_STRINGS[code]; + } + operator std::string() const { return msg; } const char *what() const throw() override { return msg.c_str(); } + int get_code() const { return code; } + int get_oscode() const { return oscode; } +}; + + +struct ConnPoolError: public SalticidaeError { + using SalticidaeError::SalticidaeError; +}; + +class PeerNetworkError: public ConnPoolError { + using ConnPoolError::ConnPoolError; }; extern const char *TTY_COLOR_RED; @@ -217,7 +257,7 @@ class Config { try { val = stoi(strval, &idx); } catch (std::invalid_argument &) { - throw SalticidaeError("invalid integer"); + throw SalticidaeError(SALTI_ERROR_OPTVAL_INVALID); } } int &get() { return val; } @@ -237,7 +277,7 @@ class Config { try { val = stod(strval, &idx); } catch (std::invalid_argument &) { - throw SalticidaeError("invalid double"); + throw SalticidaeError(SALTI_ERROR_OPTVAL_INVALID); } } double &get() { return val; } -- cgit v1.2.3