錯誤碼設計


1. 獲取錯誤碼描述信息

2. 獲取錯誤碼字符串表示

 

/**統一格式:A-BB-CC
    A:錯誤級別,如1代表系統級錯誤,2代表服務級錯誤;
    B:項目或模塊名稱;
    C:具體錯誤編號*/

/**錯誤代碼說明
    (20502)
    2: 服務級錯誤(1為系統級錯誤)
    05: 服務模塊代碼
    02: 具體錯誤代碼*/
#define UV_ERRNO_MAP(XX)                                                  \
    XX(    0, UNKNOWN,                "unknown error")                    \
    XX(    1, OK,                     "success")                          \
    XX(10201, SYS_EOF,                "end of file")                      \
    XX(10202, SYS_INVALID_SOCKET,     "invalid socket fd")                \
    XX(10401, CLI_INVALID_PASSWORD,   "invalid password")

/** 枚舉 */
#define UV_ERRNO_GEN(val, name, s) UV_##name = val,
typedef enum {
    UV_ERRNO_MAP(UV_ERRNO_GEN)
    UV_MAX_ERRORS
} uv_result_code;
#undef UV_ERRNO_GEN

#define UV_STRERROR_GEN(val, name, s) case UV_##name : return s;
/** 獲取錯誤碼描述 */
const char* uv_code_desc(uv_result_code code) {
    switch (code) {
        UV_ERRNO_MAP(UV_STRERROR_GEN)
    default:
        return "unknown error";
    }
}
#undef UV_STRERROR_GEN

#define UV_ERR_NAME_GEN(val, name, s) case UV_##name : return #name;
/** 獲取錯誤碼名稱 */
const char* uv_code_name(uv_result_code code) {
    switch (code) {
        UV_ERRNO_MAP(UV_ERR_NAME_GEN)
    default:
        return "unknown name";
    }
}
#undef UV_ERR_NAME_GEN

int main(int argc, char* argv[])
{
    uv_result_code code = uv_result_code::UV_SYS_EOF;
    const char* name = uv_code_name(code);
    const char* desc = uv_code_desc(code);

    return 0;
}

 

參考:  

https://github.com/libuv/libuv/blob/v1.x/src/uv-common.c

https://www.zhihu.com/question/24091286


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM