|
#if !defined (_SPPMKERROR_H)
#define _SPPMKERROR_H
#include <exception>
#include "Logger.h"
class SppMkError
{
_bstr_t m_ErrorText;
HRESULT m_ErrorCode;
int m_nLine;
const char *m_szFile;
void FormatErrorText(const char *Format, va_list argList)
{
m_ErrorText = L"";
static const int BUF_SIZE = 4096;
char buf[BUF_SIZE];
m_ErrorText = L"";
if (_vsnprintf(buf, BUF_SIZE, Format, argList) < 0)
buf[BUF_SIZE-1] = L'\0';
m_ErrorText = buf;
}
public:
enum E
{
eNoError = 0x0000,
eFileNotFound = 0x0001,
eStdException = 0x0002,
eBoolTest = 0x0003,
eZeroTest = 0x0004,
eInvalidParam = 0x0005,
};
SppMkError(HRESULT ErrorCode = 0, const _bstr_t &ErrorText = ""):
m_ErrorCode(ErrorCode), m_ErrorText(ErrorText), m_nLine(-1), m_szFile("File not available")
{}
SppMkError(int nLine, const char *szFile):
m_ErrorCode(0), m_ErrorText(L""), m_nLine(nLine), m_szFile(szFile)
{}
SppMkError& Make(HRESULT ErrorCode, const char *Format, ...)
{
m_ErrorCode = FAILED(ErrorCode) ?
ErrorCode : MakeError(static_cast<E>(ErrorCode));
if(NULL != Format)
{
va_list argList;
va_start(argList, Format);
FormatErrorText(Format, argList);
va_end(argList);
}
LOG(Logger::cError, "%s(%d): throwing SppMkError 0x%x (%S)",
m_szFile, m_nLine, GetErrorCode(), static_cast<WCHAR*>(GetErrorText()));
return *this;
}
HRESULT GetErrorCode() const { return m_ErrorCode; }
_bstr_t GetErrorText() const { return m_ErrorText; }
static HRESULT MakeError(E ErrorCode) { return eNoError == ErrorCode ? 0 : MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, ErrorCode); }
};
#define MK_VAR erAABBCCDDEEFF
#define MK_TRY SppMkError MK_VAR;\
try {
#define MK_THROW throw SppMkError(__LINE__, __FILE__).Make
#define MK_CATCH } catch(SppMkError &er) { \
LOG(Logger::cError, "%s(%d): caught SppMkError 0x%x (%S)", __FILE__, __LINE__, er.GetErrorCode(), static_cast<WCHAR*>(er.GetErrorText()));\
MK_VAR = er;\
}
#define MK_RETURN MK_CATCH;\
return MK_VAR.GetErrorCode();
#define MK_MSGRETURN MK_CATCH;\
if(FAILED(MK_VAR.GetErrorCode())) {\
::AfxMessageBox(static_cast<char*>(MK_VAR.GetErrorText()), MB_OK | MB_ICONERROR);\
}\
return MK_VAR.GetErrorCode();
#define TEST_HR(exp) { HRESULT hr = exp; if(FAILED(hr)) MK_THROW(hr, "%s failed with hr 0x%x", #exp, hr); }
#define TEST_BOOL(exp) { if(!(exp)) MK_THROW(SppMkError::eBoolTest, _bstr_t(#exp) + _bstr_t(": BOOL test failed")); }
#define TEST_ZERO(exp) { if(exp) MK_THROW(SppMkError::eZeroTest, _bstr_t(#exp) + _bstr_t(": ZERO test failed")); }
#endif // _SPPMKERROR_H
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.