[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框架 2)