[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(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
#include "CommonErrors.h"
#include
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(error));
}

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

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

你可能感兴趣的:(C++,个人开源项目,Console,App)