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

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


/**************************************************************************************************/
/*!
\file CommonErrors.h

\attention
(c) Jacky Dai 2011~2016
All Rights Reserved.
*/

/**************************************************************************************************/

#ifndef XX_COMMON_ERRORS_H
#define XX_COMMON_ERRORS_H

/*** Include Files ********************************************************************************/

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

namespace XXSpace
{
//basic mask code
const unsigned int COMPONENT_MASK = 0x8000;

//component code
const unsigned int COMPONENT_LOGIN = 0x0001;
const unsigned int COMPONENT_DATAMANAGER = 0x0002;

//
//0x0000XXXX:XXXX means component code
//0xXXXX0000:XXXX means error code of component

//Define some marcos/functions here
#define SYSTEM_ERROR_FLAG (0x4000)
#define SYSTEM_ERROR(code) ((code | SYSTEM_ERROR_FLAG) << 16)
#define ERROR_IS_SYSTEM_ERROR(err) (((err >> 16) & SYSTEM_ERROR_FLAG) != 0)

#define COMPONENT_ERROR(component, code) ((code << 16) | (component & ~(COMPONENT_MASK)))
#define CODE_FROM_ERROR(err) (static_cast<short>(err >> 16))

//All of the error code is here
typedef enum
{
// Hex | Decimal | Descriptio
//Basic error
ERR_OK = 0x00000000, //<< 0x00000000 | 0 | No error.
ERR_SYSTEM = SYSTEM_ERROR(1), //<< 0x00010000 | 65536 | General system error.
ERR_TIMEOUT = SYSTEM_ERROR(2), //<< 0x00020000 | 131072 | Time out.

//login component
LOGIN_ERROR_UNKNOWN_ERROR = COMPONENT_ERROR(COMPONENT_LOGIN, 1), //<< 0x00010001 |65537 | Unrecognized error.
LOGIN_ERROR_UNKNOWN_USER_NAME = COMPONENT_ERROR(COMPONENT_LOGIN, 2), //<< 0x00020001 |131073 | Unrecognized user name, it is case sensitive.

//data manager component
DATAMANAGER_ERROR_UNKNOWN_ERROR = COMPONENT_ERROR(COMPONENT_DATAMANAGER, 1), //<< 0x00010002 |65538 | Unrecognized error.
DATAMANAGER_ERROR_MISSING_CONNECTION = COMPONENT_ERROR(COMPONENT_DATAMANAGER, 2) //<< 0x00020002 |131074 | Connection missed.
} XXCommonError;


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

Class with commands for translating error codes to useful strings.

*/
/**************************************************************************************************/
class ErrorHelper
{
public:
static const char* getErrorName(XXCommonError error);
static const char* getErrorDescription(XXCommonError error);
};

} // XXSpace

#endif //XX_COMMON_ERRORS_H



/**************************************************************************************************/
/*!
\file CommonErrors.cpp

\attention
(c) Jacky Dai 2011~2016
All Rights Reserved.
*/

/**************************************************************************************************/

/*** Include Files ********************************************************************************/
#include "CommonErrors.h"

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

namespace XXSpace
{
/*** Public Methods ******************************************************************************/
const char* ErrorHelper::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* ErrorHelper::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


One sample for this architecture, it's easy to control, isn't it? Any problems, please let me know.

#include <Windows.h>
#include "CommonErrors.h"
#include <iostream>
using namespace std;


/**************************************************************************************************/
/*!
\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* GetLoginStubErrorName(Errors error);
const char* getErrorDescription(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* LoginStub::GetLoginStubErrorName(Errors error)
{
return XXSpace::ErrorHelper::getErrorName(static_cast<XXSpace::XXCommonError>(error));
}

const char* LoginStub::getErrorDescription(Errors error)
{
return XXSpace::ErrorHelper::getErrorDescription(static_cast<XXSpace::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;

Errors Test();
const char* GetDataManagerStubErrorName(Errors error);
const char* getErrorDescription(Errors error);
};

DataManagerStub::Errors DataManagerStub::Test()
{
return DATAMANAGER_ERROR_UNKNOWN_ERROR;
}

const char* DataManagerStub::GetDataManagerStubErrorName(Errors error)
{
return XXSpace::ErrorHelper::getErrorName(static_cast<XXSpace::XXCommonError>(error));
}

const char* DataManagerStub::getErrorDescription(Errors error)
{
return XXSpace::ErrorHelper::getErrorDescription(static_cast<XXSpace::XXCommonError>(error));
}


//
void main()
{
LoginStub cLogin;
LoginStub::Errors loginError = cLogin.DoLogin(NULL, NULL);
cout << hex << loginError << " = ";
cout << cLogin.GetLoginStubErrorName(loginError) << endl;
cout << cLogin.getErrorDescription(loginError) << endl;

DataManagerStub dm;
DataManagerStub::Errors dmError = dm.Test();
cout << hex << dmError << " = ";
cout << dm.GetDataManagerStubErrorName(dmError) << endl;
cout << dm.getErrorDescription(dmError) << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值