首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

[C++][Error Code]怎么定义Error Code框架

2012-10-05 
[C++][Error Code]如何定义Error Code框架对于不同的项目而言,error code返回机制是不一样的,但是能够很好

[C++][Error Code]如何定义Error Code框架
对于不同的项目而言,error code返回机制是不一样的,但是能够很好的管理好不同模块间的Error Code并不是一件简单的事情,下面有一种比较简便的方法如下:

/**************************************************************************************************//*!    \file CommonErrors.h    \attention        (c) Jacky Dai. All Rights Reserved.*//**************************************************************************************************/#ifndef XX_COMMON_ERRORS_H#define XX_COMMON_ERRORS_H/*** Include Files ********************************************************************************//*** Defines/Macros/Constants/Typedefs ************************************************************/namespace XXSpace{//basic mask codeconst unsigned int COMPONENT_MASK       = 0x8000;//component codeconst unsigned int COMPONENT_LOGIN            = 0x0001;const unsigned int COMPONENT_DATAMANAGER      = 0x0002;//Define some marcos/functions here#define SYSTEM_ERROR_FLAG (0x4000)#define ERROR_IS_SYSTEM_ERROR(err) (((err >> 16) & SYSTEM_ERROR_FLAG) != 0)#define SYSTEM_ERROR(code) ((code | SYSTEM_ERROR_FLAG) << 16)#define COMPONENT_ERROR(component, code) ((code << 16) | (component & ~(COMPONENT_MASK)))//All of the error code is heretypedef enum{                                                                                //   Hex        | Decimal       | Descriptio    //Basic error    ERR_OK                                       = ERR_OK,                      //<< 0x00000000 |  0            | No error.    ERR_SYSTEM                                   = SYSTEM_ERROR(1),             //<< 0x00001000 | 65536         | General system error.    ERR_TIMEOUT                                  = SYSTEM_ERROR(2),             //<< 0x00002000 | 131072        | Time out.        //login component    LOGIN_ERROR_UNKNOWN_ERROR                    = COMPONENT_ERROR(COMPONENT_LOGIN, 1),     //<< 0x27803 |161795   | Unrecognized error.    LOGIN_ERROR_UNKNOWN_USER_NAME                = COMPONENT_ERROR(COMPONENT_LOGIN, 2),     //<< 0x47803 |292867   | Unrecognized user name, it is case sensitive.        //data manager component    DATAMANAGER_ERROR_UNKNOWN_ERROR              = COMPONENT_ERROR(COMPONENT_DATAMANAGER, 1), //<< 0x37803 |227331   | Unrecognized error.    DATAMANAGER_ERROR_MISSING_CONNECTION         = COMPONENT_ERROR(COMPONENT_DATAMANAGER, 2)  //<< 0x37803 |227331   | Connection missed.} XXCommonError;/**************************************************************************************************//*!    \class ErrorHelp    Class with commands for translating error codes to useful strings.*//**************************************************************************************************/class ErrorHelp{public:    static const char* getErrorName(XXCommonError error);    static const char* getErrorDescription(XXCommonError error);};/////////////////////////////////////////////////////////////////////////////////////////////////////////*** Public Methods ******************************************************************************/const char* ErrorHelp::getErrorName(XXCommonError error){    const char *result = "UKNOWN";    switch (error)    {        case ERR_OK: result = "ERR_OK"; break;        case ERR_SYSTEM: result = "ERR_SYSTEM"; break;        case ERR_TIMEOUT: result = "ERR_TIMEOUT"; break;        case LOGIN_ERROR_UNKNOWN_ERROR: result = "LOGIN_ERROR_UNKNOWN_ERROR"; break;        case LOGIN_ERROR_UNKNOWN_USER_NAME: result = "LOGIN_ERROR_UNKNOWN_USER_NAME", break;        case DATAMANAGER_ERROR_UNKNOWN_ERROR: result = "DATAMANAGER_ERROR_UNKNOWN_ERROR", break;        case DATAMANAGER_ERROR_MISSING_CONNECTION: result = "DATAMANAGER_ERROR_MISSING_CONNECTION", break;        default: break;    }    return result;}const char* ErrorHelp::getErrorDescription(XXCommonError error){    const char *result = "UKNOWN";    switch (error)    {        case ERR_OK: result = "No error."; break;        case ERR_SYSTEM: result = "General system error."; break;        case ERR_TIMEOUT: result = "Time out."; break;        case LOGIN_ERROR_UNKNOWN_ERROR: result = "Unrecognized error."; break;        case LOGIN_ERROR_UNKNOWN_USER_NAME: result = "Unrecognized user name, it is case sensitive.", break;        case DATAMANAGER_ERROR_UNKNOWN_ERROR: result = "Unrecognized error.", break;        case DATAMANAGER_ERROR_MISSING_CONNECTION: result = "Connection missed.", break;        default: break;    }    return result;}} // XXSpace#endif //XX_COMMON_ERRORS_H


One sample for this architecture, it's easy to control, isn't it? Any problems, please let me know.
#include "CommonErrors.h"/**************************************************************************************************//*!    \class XXStub    Stub class for the XXXX.*//**************************************************************************************************/class Login;class LoginStub : public Login{public:    typedef enum    {        ERR_OK                        = XXSpace::ERR_OK,        ERR_SYSTEM                    = XXSpace::ERR_SYSTEM,        ERR_TIMEOUT                   = XXSpace::ERR_TIMEOUT,        LOGIN_ERROR_UNKNOWN_ERROR     = XXSpace::LOGIN_ERROR_UNKNOWN_ERROR,        LOGIN_ERROR_UNKNOWN_USER_NAME = XXSpace::LOGIN_ERROR_UNKNOWN_USER_NAME    } Errors;        Errors DoLogin(const char* userName, const char* password);    const char* GetErrorName(Errors error);};LoginStub::Errors LoginStub::DoLogin(const char* userName, const char* password){    if( userName==NULL     || password==NULL )    {        return LOGIN_ERROR_UNKNOWN_USER_NAME;    }                return ERR_OK;}const char* LoginStubGetErrorName(LoginStub::Errors error){    return ErrorHelp::getErrorName(static_cast<XXCommonError> error);}class DataManager;class DataManagerStub : public DataManager{public:    typedef enum    {        ERR_OK                               = XXSpace::ERR_OK,        ERR_SYSTEM                           = XXSpace::ERR_SYSTEM,        ERR_TIMEOUT                          = XXSpace::ERR_TIMEOUT,        DATAMANAGER_ERROR_UNKNOWN_ERROR      = XXSpace::DATAMANAGER_ERROR_UNKNOWN_ERROR,        DATAMANAGER_ERROR_MISSING_CONNECTION = XXSpace::DATAMANAGER_ERROR_MISSING_CONNECTION    } Errors;    };

热点排行