`

[C++][Error Code]如何定义Error Code框架 2

 
阅读更多
相对前面一次定义的Error Code框架,这个有点类似我们平时经常用的一种方式,大致如下:


首先把所有Error Code定义到一个文件中去,
/*************************************************************************************************/
/*!
    \file CommonDef.h

    \attention
        (c) Jacky Dai. 2013~2016 
        All Rights Reserved.
*/
/*************************************************************************************************/
#ifndef COMMONDEF_H
#define COMMONDEF_H

/*** Defines/Macros/Constants/Typedefs ***********************************************************/

//Include Head files
//////////////////////////////////////////////////////////////////////////

#ifdef __cplusplus
extern "C" {
#endif

// Error message definition
//////////////////////////////////////////////////////////////////////////
//Basic Error Code
#define  ERR_OK                        0x00000000           //Succeed
#define  ERR_BASIC_BASE                0x00001000
#define  ERR_INVALID_PARAMETER         ERR_BASIC_BASE+0x0001     //The parameter contains incorrect value, Pointer is NULL or buffer size is 0.
#define  ERR_FILE_NOT_EXIST            ERR_BASIC_BASE+0x0002     //The assigned file does nos exist.
#define  ERR_INVALID_FILE_PATH_NAME    ERR_BASIC_BASE+0x0003     //The path name is invalid, we cannot write package to the folder.

//Catalog Error Code
#define  ERR_CATALOG_BASE              0x00002000
#define  ERR_CATA_DATA_TOO_MUCH        ERR_CATALOG_BASE+0x0001  // The max size of catalog is MAX_CATALOG_SIZE can be saved
#define  ERR_CATA_DATA_IS_EMPTY        ERR_CATALOG_BASE+0x0002  // There is no catalog data


//Key Data Error Code
#define  ERR_KEY_DATA_BASE             0x00003000
#define  ERR_SET_PUBLIC_KEY            ERR_KEY_DATA_BASE+0x0001  //Fail to set assigned public key data to signer.
#define  ERR_SET_PRIVATE_KEY           ERR_KEY_DATA_BASE+0x0002  //Fail to set assigned private key data to signer.
#define  ERR_GET_KEY_LENGTH            ERR_KEY_DATA_BASE+0x0003  //Fail to get key length through key data.

//Certificate Error Code
#define  ERR_CERTIFICATE_BASE             0x00004000
#define  ERR_CERT_DATE_INVALID_CHARACTER  ERR_CERTIFICATE_BASE+0x0001 //Date buffer is out of range '0'~'9'.

//Reserved Error Code for HSM Callbacks
#define  ERR_HSM_BASE                   0x00008000
#define  ERR_HSM_CONNECT                ERR_HSM_BASE+0x0001    //Fail to connect to HSM
#define  ERR_HSM_UNKNOWN                ERR_HSM_BASE+0x0002    //Unknown Error of HSM

//Reserved Error Code for key converter
#define  ERR_KEY_CVT_BASE                0x00011000
#define  ERR_KEY_CVT_CERTIFICATE_CONTEXT ERR_KEY_CVT_BASE+0x0001
#define  ERR_KEY_CVT_KEY_CONTAINER       ERR_KEY_CVT_BASE+0x0002

#ifdef __cplusplus
}
#endif

#endif//COMMONDEF_H


然后解封所有ErrorCode
/**************************************************************************************************/
/*!
    \file ErrorHelper.h

    \attention
        (c) Jacky Dai. 2013~2016
        All Rights Reserved.
*/
/**************************************************************************************************/
#ifndef ERROR_HELPER_H
#define ERROR_HELPER_H

/*** Include Files ********************************************************************************/
#include <Windows.h>
/*** Defines/Macros/Constants/Typedefs ************************************************************/

/**************************************************************************************************/
/*!
    \class ErrorHelper

    Class with commands for translating error codes to useful strings.

*/
/**************************************************************************************************/
class ErrorHelper
{
public:
    static LPCSTR getErrorName(DWORD dwErrorCode);
    static LPCSTR getErrorDescription(DWORD dwErrorCode);

#ifdef _DEBUG
    static VOID getErrorTest(VOID);
#endif
};

#endif //ERROR_HELP_H


/*** Include Files ********************************************************************************/
#include "ErrorHelper.h"
#include <iostream>
#include <iomanip>
#include "CommonDef.h"

using namespace std;
////////////////////////////////////////////////////////////////////////////////////////////////////////
/*** Public Methods ******************************************************************************/
LPCSTR ErrorHelper::getErrorName(DWORD dwErrorCode)
{
    const char *result = "UNKNOWN";

    switch (dwErrorCode)
    {
    case ERR_OK: result = "ERR_OK"; break;

    //Basic Error Code
    case  ERR_INVALID_PARAMETER: result = "ERR_INVALID_PARAMETER"; break;
    case  ERR_FILE_NOT_EXIST: result = "ERR_FILE_NOT_EXIST"; break;
    case  ERR_INVALID_FILE_PATH_NAME: result = "ERR_INVALID_FILE_PATH_NAME"; break;

    //Catalog Error Code
    case  ERR_CATA_DATA_TOO_MUCH: result = "ERR_CATA_DATA_TOO_MUCH"; break;
    case  ERR_CATA_DATA_IS_EMPTY: result = "ERR_CATA_DATA_IS_EMPTY"; break;

    //Key Data Error Code
    case  ERR_SET_PUBLIC_KEY: result = "ERR_SET_PUBLIC_KEY"; break;
    case  ERR_SET_PRIVATE_KEY: result = "ERR_SET_PRIVATE_KEY"; break;
    case  ERR_GET_KEY_LENGTH: result = "ERR_GET_KEY_LENGTH"; break;

    //Certificate Error Code
    case  ERR_CERT_DATE_INVALID_CHARACTER: result = "ERR_CERT_DATE_INVALID_CHARACTER"; break;

    //Reserved Error Code for HSM Callbacks
    case ERR_HSM_CONNECT: result = "ERR_HSM_CONNECT"; break;
    case ERR_HSM_UNKNOWN: result = "ERR_HSM_UNKNOWN"; break;

    default: break;
    }

    return result;
}

LPCSTR ErrorHelper::getErrorDescription(DWORD dwErrorCode)
{
    const char *result = "UNKNOWN";

    switch (dwErrorCode)
    {
    case ERR_OK: result = "No error."; break;

    //Basic Error Code
    case ERR_INVALID_PARAMETER: result = "The parameter contains incorrect value, Pointer is NULL or buffer size is 0."; break;
    case ERR_FILE_NOT_EXIST: result = "The assigned file does nos exist."; break;
    case ERR_INVALID_FILE_PATH_NAME: result = "The path name is invalid, we cannot write package to the folder."; break;

    //Catalog Error Code
    case  ERR_CATA_DATA_TOO_MUCH: result = "The max size of catalog is MAX_CATALOG_SIZE can be saved."; break;
    case  ERR_CATA_DATA_IS_EMPTY: result = "There is no catalog data."; break;

    //Key Data Error Code
    case  ERR_SET_PUBLIC_KEY: result = "Fail to set assigned public key data to signer."; break;
    case  ERR_SET_PRIVATE_KEY: result = "Fail to set assigned private key data to signer."; break;
    case  ERR_GET_KEY_LENGTH: result = "Fail to get key length through key data."; break;

    //Certificate Error Code
    case  ERR_CERT_DATE_INVALID_CHARACTER: result = "Date buffer is out of range '0'~'9'."; break;

    //Reserved Error Code for HSM Callbacks
    case ERR_HSM_CONNECT: result = "Fail to connect to HSM."; break;
    case ERR_HSM_UNKNOWN: result = "Unknown Error of HSM."; break;

    default: break;
    }

    return result;
}

#ifdef _DEBUG

VOID ErrorHelper::getErrorTest(VOID)
{
    DWORD dwErrCode = ERR_OK;
    cout << "ErrorCode = 0x" << setw(8) << setfill('0') << hex << dwErrCode << endl;
    cout << ErrorHelper::getErrorName(dwErrCode) << endl;
    cout << ErrorHelper::getErrorDescription(dwErrCode) << endl;
    cout << endl;
    system("PAUSE");

    //Basic Error Code
    dwErrCode = ERR_BASIC_BASE;
    for (DWORD i = dwErrCode+1; i<=ERR_PACKING_FOLDER_NOT_EXIST; i++ )
    {
        cout << "ErrorCode = 0x" << setw(8) << setfill('0') << hex << i << endl;
        cout << ErrorHelper::getErrorName(i) << endl;
        cout << ErrorHelper::getErrorDescription(i) << endl;
        cout << endl;
    }
    system("PAUSE");

    //Catalog Error Code
    dwErrCode = ERR_CATALOG_BASE;
    for (DWORD i = dwErrCode+1; i<=ERR_CATA_INVALID_DATA_FILE; i++ )
    {
        cout << "ErrorCode = 0x" << setw(8) << setfill('0') << hex << i << endl;
        cout << ErrorHelper::getErrorName(i) << endl;
        cout << ErrorHelper::getErrorDescription(i) << endl;
        cout << endl;
    }
    system("PAUSE");

    //Key Data Error Code
    dwErrCode = ERR_KEY_DATA_BASE;
    for (DWORD i = dwErrCode+1; i<=ERR_INVALID_PRV_KEY_SIZE; i++ )
    {
        cout << "ErrorCode = 0x" << setw(8) << setfill('0') << hex << i << endl;
        cout << ErrorHelper::getErrorName(i) << endl;
        cout << ErrorHelper::getErrorDescription(i) << endl;
        cout << endl;
    }
    system("PAUSE");

    //Certificate Error Code
    dwErrCode = ERR_CERTIFICATE_BASE;
    for (DWORD i = dwErrCode+1; i<=ERR_CERT_DATE_EXPIRED_VS_EFFECTIVE; i++ )
    {
        cout << "ErrorCode = 0x" << setw(8) << setfill('0') << hex << i << endl;
        cout << ErrorHelper::getErrorName(i) << endl;
        cout << ErrorHelper::getErrorDescription(i) << endl;
        cout << endl;
    }
    system("PAUSE");

    //Reserved Error Code for HSM Callbacks
    dwErrCode = ERR_HSM_BASE;
    for (DWORD i = dwErrCode+1; i<=ERR_HSM_UNKNOWN; i++ )
    {
        cout << "ErrorCode = 0x" << setw(8) << setfill('0') << hex << i << endl;
        cout << ErrorHelper::getErrorName(i) << endl;
        cout << ErrorHelper::getErrorDescription(i) << endl;
        cout << endl;
    }
    system("PAUSE");
}
#endif
分享到:
评论

相关推荐

    [C++][Error Code]如何定义Error Code框架

    这篇博客 "[C++][Error Code]如何定义Error Code框架" 可能会详细讲解如何在C++中构建这样的框架。 首先,我们来讨论一下在C++中定义错误代码的一般方法。C++标准库提供了`std::error_code`类,它是用于表示特定...

    解决createprocess error code 740

    标题 "解决createprocess error code 740" 指的是在尝试运行一个应用程序时遇到的一个特定错误,其中 `CreateProcess` 是 Windows API 中用于创建新进程的函数。错误代码 740 表示 "请求的操作需要提升的权限",意味...

    dlna C++ source code

    在这个“dlna C++ source code”中,我们很显然将探讨如何使用C++编程语言实现DLNA规范的相关功能。 首先,我们需要了解DLNA的核心概念。DLNA标准主要由以下组件构成: 1. **设备(Devices)**:DLNA网络中的任何...

    【Visual C++】Code_Note_2

    博文的配套源码 《【 Visual C++】游戏开发笔记之二——最简单的directx,vc窗口的编写 》 ,原文地址为http://blog.csdn.net/zhmxy555/article/details/7318634, 源码编译环境为vs2010. 我的博客地址是...

    Demo - Replace Error Code With Exception

    "Demo - Replace Error Code With Exception" 这个主题着重于将传统的错误码机制替换为异常处理机制,以提高代码的可读性、可维护性和异常处理的效率。 错误码通常是通过返回特定的整数值来指示函数或方法执行期间...

    no bugs!delivering error-free code in c & c++

    no bugs!delivering error-free code in c & c++

    WIN32_ERROR code

    2. **1 - ERROR_INVALID_FUNCTION** - 描述:不正确的函数。 - 含义:调用了一个不存在或无效的函数。这通常发生在函数名拼写错误、库文件缺失或函数版本不匹配的情况下。 3. **2 - ERROR_FILE_NOT_FOUND** - ...

    symbian 错误对应表error_code

    在Symbian操作系统中,错误处理是通过一个称为`error_code`的机制来实现的。这个机制为开发者提供了一种标准化的方式来识别和处理程序运行时出现的问题。`error_code`是一个整数值,它对应于特定的错误信息或状态,...

    c++_code_php_

    2. **C++11及更新标准**:现代C++(如C++11, C++14, C++17)引入了许多新特性,如右值引用、lambda表达式、自动类型推断(`auto`关键字)等,这些可能在代码中被使用。 3. **STL**:标准模板库(Standard Template ...

    ECC.rar_ecc code_ecc error code_error correction_hd dvd

    错误纠正码(Error Correction Code,ECC)是数据通信和存储系统中的一种关键技术,用于检测和纠正传输或存储过程中可能出现的错误。ECC在高清DVD(HD DVD)等媒体存储设备中扮演着至关重要的角色,确保了数据的可靠...

    C++标准库_The C++ Standard Library_英文版.pdf )

    The C++ Standard Library provides a set of common classes and interfaces that greatly extend the core C++ language. Josuttis' book not only provides comprehensive documentation of each library ...

    pycharm安装所需软件Microsoft Visual C++ 14.0安装包

    error: Microsoft Visual C++ 14.0 is required. Get it with "Build Tools for Visual Studio": https://visualstudio.microsoft.com/downloads/ 安装twisted包的时候提示microsoft visual c++ 14.0 is required的...

    C# 捕获C/C++异常的例子

    在这里,我们首先调用了`MyCppFunction`,如果C++代码抛出了异常,我们检查`ErrorCode`是否非零。如果是,我们创建一个新的`ApplicationException`,将C++异常的信息包含其中,并重新抛出。这样,C#的异常处理机制就...

    《C++现代化程序设计》code

    《C++现代化程序设计》是一本深入探讨C++现代编程技术的书籍,其"real_sample_code"压缩包包含了书中各个章节的实际示例代码。这些代码旨在帮助读者理解并实践C++的最新特性和最佳实践,从而提升编程技能。在本文中...

    NXP codewarrior 编译报错解析合集 Build Tools Message Reference

    例如,C/C++ Error 10100表示“未声明的标识符”,这通常意味着在代码中引用了一个尚未定义的变量或函数。同样,Error 10101和10102分别对应于“预期的表达式”和“预期的分号”,这些都是语法错误,提示开发者检查...

    C++11学习总结.doc

    ERRORCODE OnLineDebug::_func(std::function&lt;ERRORCODE(uint32_t&)&gt; func_) noexcept { // ... (代码省略) } ERRORCODE OnLineDebug::_CheckCfgUUID(uint32_t& cmdid) { // ... (代码省略) } ``` 在 C++11 的...

    iFFT.rar_BPSK c++ code_ifft

    标题 "iFFT.rar_BPSK c++ code_ifft" 暗示了这是一个与数字信号处理相关的项目,具体是使用C++实现BPSK(二进制相移键控)调制并涉及到快速傅里叶逆变换(iFFT)。描述中提到的"error correct coding"表明这是关于...

    cocos2dx 3.4 c++ zip压缩包热更新和解压源码

    void onError(ErrorCode errorCode); void onProgress(int percent); void onSuccess(); void setLuaCallbacks(const std::string& callbackMap); private: std::string *_versionLoadedCallback = nullptr;...

Global site tag (gtag.js) - Google Analytics