diff --git a/srcs/libs/include/user_errno.h b/srcs/libs/include/user_errno.h index 853cb7a..3a1bacf 100644 --- a/srcs/libs/include/user_errno.h +++ b/srcs/libs/include/user_errno.h @@ -10,82 +10,139 @@ extern "C" { #define USED_USER_ERRNO +#define DEF_ERR_CODE(ERR_CODE) \ + ERR_CODE(ERR_SUCCESS, "成功") \ + ERR_CODE(ERR_INPUT_PARAMS, "输入参数错误") \ + ERR_CODE(ERR_UN_SUPPORT, "不支持的操作") \ + ERR_CODE(ERR_CALL_SHELL, "调用Shell命令失败") \ + ERR_CODE(ERR_ITEM_EXISTS, "该内容已经存在") \ + ERR_CODE(ERR_ITEM_UNEXISTS, "该内容不存在") \ + ERR_CODE(ERR_SYS_INIT, "系统中断") \ + ERR_CODE(ERR_OPEN_FILE, "打开文件失败") \ + ERR_CODE(ERR_READ_FILE, "读取文件失败") \ + ERR_CODE(ERR_FILE_NOT_EXISTS, "文件不存在") \ + ERR_CODE(ERR_GET_FILE_SIZE, "获取文件大小失败") \ + ERR_CODE(ERR_COPY_FILE, "复制文件失败") \ + ERR_CODE(ERR_MALLOC_MEMORY, "分配内存失败") \ + ERR_CODE(ERR_EVP_KEY_SIZE, "秘钥大小不正确") \ + ERR_CODE(ERR_UNSUP_EVP_TYPE, "不支持的加解密算法") \ + ERR_CODE(ERR_EVP_INIT_KEY, "初始化秘钥失败") \ + ERR_CODE(ERR_EVP_UPDATE, "加解密数据失败") \ + ERR_CODE(ERR_EVP_FINALE, "错误的加解密结果") \ + ERR_CODE(ERR_EVP_CREATE_CTX, "初始化加解密失败") \ + ERR_CODE(ERR_AES128_KEYGEN, "AES128秘钥失败") \ + ERR_CODE(ERR_EVP_ENCRYPTION, "加密失败") \ + ERR_CODE(ERR_EVP_DECRYPTION, "解密失败") \ + ERR_CODE(ERR_CONFIG_INIT, "配置文件初始化失败") \ + ERR_CODE(ERR_UNCOMPATIBILITY_TYPE, "未兼容的类型") \ + ERR_CODE(ERR_STRING_TO_NUMBER, "字符串转数字失败") \ + ERR_CODE(ERR_UNKNOWN_CFG_ID, "未识别的配置项") \ + ERR_CODE(ERR_ZLOG_INIT, "日志系统初始化失败") \ + ERR_CODE(ERR_SYS_GET_CPU_INFO, "获取CPU信息失败") \ + ERR_CODE(ERR_SYS_NOT_FOUND_CPU, "找不到CPU信息") \ + ERR_CODE(ERR_SYS_DISK_GET_INFO, "获取磁盘空间占用信息失败") \ + ERR_CODE(ERR_SYS_IPMI_UNSUP, "服务器IPMI接口不支持") \ + ERR_CODE(ERR_SYS_SENSOR_GET_INFO, "获取传感器信息失败") \ + ERR_CODE(ERR_DB_CONNECT, "数据库连接失败") \ + ERR_CODE(ERR_MQ_CREATE_MQ, "创建消息队列失败") \ + ERR_CODE(ERR_MQ_CREATE_REP, "创建REP消息队列失败") \ + ERR_CODE(ERR_MQ_BIND_SOCKET, "消息队列BIND Socket失败") \ + ERR_CODE(ERR_MQ_CONN_SERVER, "消息队列连接服务器失败") \ + ERR_CODE(ERR_MQ_SEND_MSG, "消息队列发送消息失败") \ + ERR_CODE(ERR_JSON_CREAT_OBJ, "创建JSON对象失败") \ + ERR_CODE(ERR_CREATE_NETIF, "创建网络接口失败") \ + ERR_CODE(ERR_CREATE_PPPOE_NETIF, "创建PPPoE网络接口失败") \ + ERR_CODE(ERR_CREATE_PPP_SESSION, "创建PPP连接失败") \ + ERR_CODE(ERR_MISC_GET_IPADDR, "获取网卡IP地址失败") \ + ERR_CODE(ERR_MISC_GET_NETMASK, "获取网卡子网掩码失败") \ + ERR_CODE(ERR_MISC_GET_GATEWAY, "获取网卡网关地址失败") \ + ERR_CODE(ERR_MISC_GET_MACADDR, "获取网卡MAC地址失败") \ + ERR_CODE(ERR_MENU_EXIT, "菜单执行完后自动退出") + +#define GENERATE_ENUM(ENUM, x) ENUM, + typedef enum { - ERR_SUCCESS = 0, - ERR_INPUT_PARAMS = 1, - ERR_UN_SUPPORT = 3, - ERR_CALL_SHELL = 4, - ERR_ITEM_EXISTS = 5, - ERR_ITEM_UNEXISTS = 6, - ERR_SYS_INIT = 7, - - // 文件读取相关错误 - ERR_OPEN_FILE = 100, - ERR_READ_FILE = 101, - ERR_FILE_NOT_EXISTS = 102, - ERR_GET_FILE_SIZE = 103, - ERR_COPY_FILE = 104, - - // 内存相关错误 - ERR_MALLOC_MEMORY = 200, - - // SSL EVP 加解密相关错误 - ERR_EVP_KEY_SIZE = 200, - ERR_UNSUP_EVP_TYPE = 201, - ERR_EVP_INIT_KEY = 202, - ERR_EVP_UPDATE = 203, - ERR_EVP_FINALE = 204, - ERR_EVP_CREATE_CTX = 205, - ERR_AES128_KEYGEN = 206, - ERR_EVP_ENCRYPTION = 207, - ERR_EVP_DECRYPTION = 208, - - // 业务相关错误 - // 配置相关业务 - ERR_CONFIG_INIT = 1000, - ERR_UNCOMPATIBILITY_TYPE = 1001, - ERR_STRING_TO_NUMBER = 1002, - ERR_UNKNOWN_CFG_ID = 1003, - - // 日志系统相关业务 - ERR_ZLOG_INIT = 2001, - - // 硬件信息相关业务 - ERR_SYS_GET_CPU_INFO = 2100, - ERR_SYS_NOT_FOUND_CPU = 2101, - ERR_SYS_DISK_GET_INFO = 2102, - ERR_SYS_IPMI_UNSUP = 2103, - ERR_SYS_SENSOR_GET_INFO = 2104, - - // 数据库相关业务 - ERR_DB_CONNECT = 2200, - - // 消息队列相关错误 - ERR_MQ_CREATE_MQ = 2300, - ERR_MQ_CREATE_REP = 2301, - ERR_MQ_BIND_SOCKET = 2302, - ERR_MQ_CONN_SERVER = 2304, - ERR_MQ_SEND_MSG = 2305, - - // JSON 序列化相关错误 - ERR_JSON_CREAT_OBJ = 2400, - - // PPPoE 相关 - ERR_CREATE_NETIF = 2500, - ERR_CREATE_PPPOE_NETIF = 2501, - ERR_CREATE_PPP_SESSION = 2502, - - // MISC 相关 - ERR_MISC_GET_IPADDR = 2600, - ERR_MISC_GET_NETMASK = 2601, - ERR_MISC_GET_GATEWAY = 2602, - ERR_MISC_GET_MACADDR = 2603, - - // 菜单相关功能 - ERR_MENU_EXIT = 2700, - + DEF_ERR_CODE(GENERATE_ENUM) } USER_ERRNO; +//typedef enum { +// ERR_SUCCESS = 0, +// ERR_INPUT_PARAMS = 1, +// ERR_UN_SUPPORT = 3, +// ERR_CALL_SHELL = 4, +// ERR_ITEM_EXISTS = 5, +// ERR_ITEM_UNEXISTS = 6, +// ERR_SYS_INIT = 7, +// +// // 文件读取相关错误 +// ERR_OPEN_FILE = 100, +// ERR_READ_FILE = 101, +// ERR_FILE_NOT_EXISTS = 102, +// ERR_GET_FILE_SIZE = 103, +// ERR_COPY_FILE = 104, +// +// // 内存相关错误 +// ERR_MALLOC_MEMORY = 200, +// +// // SSL EVP 加解密相关错误 +// ERR_EVP_KEY_SIZE = 200, +// ERR_UNSUP_EVP_TYPE = 201, +// ERR_EVP_INIT_KEY = 202, +// ERR_EVP_UPDATE = 203, +// ERR_EVP_FINALE = 204, +// ERR_EVP_CREATE_CTX = 205, +// ERR_AES128_KEYGEN = 206, +// ERR_EVP_ENCRYPTION = 207, +// ERR_EVP_DECRYPTION = 208, +// +// // 业务相关错误 +// // 配置相关业务 +// ERR_CONFIG_INIT = 1000, +// ERR_UNCOMPATIBILITY_TYPE = 1001, +// ERR_STRING_TO_NUMBER = 1002, +// ERR_UNKNOWN_CFG_ID = 1003, +// +// // 日志系统相关业务 +// ERR_ZLOG_INIT = 2001, +// +// // 硬件信息相关业务 +// ERR_SYS_GET_CPU_INFO = 2100, +// ERR_SYS_NOT_FOUND_CPU = 2101, +// ERR_SYS_DISK_GET_INFO = 2102, +// ERR_SYS_IPMI_UNSUP = 2103, +// ERR_SYS_SENSOR_GET_INFO = 2104, +// +// // 数据库相关业务 +// ERR_DB_CONNECT = 2200, +// +// // 消息队列相关错误 +// ERR_MQ_CREATE_MQ = 2300, +// ERR_MQ_CREATE_REP = 2301, +// ERR_MQ_BIND_SOCKET = 2302, +// ERR_MQ_CONN_SERVER = 2304, +// ERR_MQ_SEND_MSG = 2305, +// +// // JSON 序列化相关错误 +// ERR_JSON_CREAT_OBJ = 2400, +// +// // PPPoE 相关 +// ERR_CREATE_NETIF = 2500, +// ERR_CREATE_PPPOE_NETIF = 2501, +// ERR_CREATE_PPP_SESSION = 2502, +// +// // MISC 相关 +// ERR_MISC_GET_IPADDR = 2600, +// ERR_MISC_GET_NETMASK = 2601, +// ERR_MISC_GET_GATEWAY = 2602, +// ERR_MISC_GET_MACADDR = 2603, +// +// // 菜单相关功能 +// ERR_MENU_EXIT = 2700, +// +//} USER_ERRNO; + +const char *getErrorEnumString(int errCode); +const char *getErrorEnumDesc(int errCode); #ifdef __cplusplus } #endif diff --git a/srcs/libs/misc/err.c b/srcs/libs/misc/err.c new file mode 100644 index 0000000..ba0a71d --- /dev/null +++ b/srcs/libs/misc/err.c @@ -0,0 +1,17 @@ +// +// Created by xajhuang on 2022/12/5. +// +#include + +#define MAX_DESC_LENGTH (256) +#define GENERATE_STRING(STRING, desc) {#STRING, desc}, + +static const char *g_enumStrVal[][MAX_DESC_LENGTH] = {DEF_ERR_CODE(GENERATE_STRING)}; + +const char *getErrorEnumString(int errCode) { + return g_enumStrVal[errCode][0]; +} + +const char *getErrorEnumDesc(int errCode) { + return g_enumStrVal[errCode][1]; +}