I have been writing a light-weight framework to wrap the Windows API for personal projects and some fun. What I think is a good design method is that each class in the framework manages its own error information whenever something goes wrong. I have a simple error class like so:
class Error
{
public:
struct ErrorData
{
DWORD sysErrCode;
tstring sysErrStr;
SYSTEMTIME localTime;
tstring errMsg;
tstring funcCall;
tstring parentClass;
};
void getErrorData(ErrorData *pErrorData);
Error(const tstring parentClass);
void setErrorData(const tstring errMsg, const tstring funcCall, const bool getSysErr = false);
private:
ErrorData errorData;
void getSystemError(DWORD &sysErrCode, tstring &sysErrStr);
};
What I’m stuck on is how to incorporate this class into the other classes. Inheritance sounds wrong because a dialog class is not an Error class. Composition sounds better, a dialog class can have an Error class. But then I’m stuck writing a getter function for every class to retrieve the Error class’s information. Although that would only take a short time to write, there has got to be a better design method. I would rather not have to copy and paste those functions in every class either.
Any ideas would be appreciated!
Thank you.
Have you considered using exceptions and
throwing yourErrorDataclass instead? That seems better than either inheritance or composition.It almost looks like
getSystemErrorshould be a namespace free-function instead of a member (it looks like everything it needs is passed into its parameters).