00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef __MOZOPC_EXCEPTION__H__
00012 #define __MOZOPC_EXCEPTION__H__
00013
00014
00015 #include <boost/format.hpp>
00016 #include <stdexcept>
00017 #include <string>
00018 #include <vector>
00019
00020
00021 namespace Common
00022 {
00023
00024 struct ErrorData
00025 {
00026 unsigned ErrorCode;
00027 const char* ErrorMessage;
00028
00029 ErrorData()
00030 : ErrorCode(0)
00031 , ErrorMessage(NULL)
00032 {
00033 }
00034
00035 ErrorData(unsigned code, const char* msg)
00036 : ErrorCode(code)
00037 , ErrorMessage(msg)
00038 {
00039 }
00040 };
00041
00042 class Error : public std::exception
00043 {
00044 public:
00045 Error();
00046 explicit Error(unsigned lineNum, const char* fileName, unsigned errorCode, const char* msg);
00047 virtual ~Error() throw();
00048
00049 unsigned GetLineNum() const;
00050 std::string GetFileName() const;
00051 unsigned GetCode() const;
00052 std::string GetMessage() const;
00053 std::string GetFullMessage() const;
00054
00055 Error& AddError(const Error& subError);
00056
00057 public:
00058 virtual const char* what() const throw();
00059
00060 private:
00061 unsigned LineNum;
00062 std::string FileName;
00063 unsigned Code;
00064 std::string Message;
00065 mutable std::string FullMessage;
00066 std::vector<Error> SubErrors;
00067 };
00068
00069
00070 inline Error CreateError(
00071 unsigned lineNum,
00072 const char* filename,
00073 unsigned errorCode,
00074 const char* msg)
00075 {
00076 return Common::Error(lineNum, filename, errorCode, msg);
00077 }
00078
00079 template <typename T1>
00080 inline Error CreateError(
00081 unsigned lineNum,
00082 const char* fileName,
00083 unsigned errorCode,
00084 const char* msg,
00085 const T1& param1)
00086 {
00087 const std::string& resultMessage = str(boost::format(msg) % param1);
00088 return Error(lineNum, fileName, errorCode, resultMessage.c_str());
00089 }
00090
00091 template <typename T1>
00092 inline Error CreateError(
00093 unsigned lineNum,
00094 const char* fileName,
00095 unsigned errorCode,
00096 const char* msg,
00097 const T1& param1,
00098 const Common::Error& subError)
00099 {
00100 const std::string& resultMessage = str(boost::format(msg) % param1);
00101 Error resultError(lineNum, fileName, errorCode, resultMessage.c_str());
00102 resultError.AddError(subError);
00103 return resultError;
00104 }
00105
00106 template <typename T1, typename T2>
00107 inline Error CreateError(
00108 unsigned lineNum,
00109 const char* fileName,
00110 unsigned errorCode,
00111 const char* msg,
00112 const T1& param1,
00113 const T2& param2)
00114 {
00115 const std::string& resultMessage = str(boost::format(msg) % param1 % param2);
00116 return Error(lineNum, fileName, errorCode, resultMessage.c_str());
00117 }
00118
00119
00120 template <typename T1, typename T2>
00121 inline Error CreateError(
00122 unsigned lineNum,
00123 const char* fileName,
00124 unsigned errorCode,
00125 const char* msg,
00126 const T1& param1,
00127 const T2& param2,
00128 const Common::Error& subError)
00129 {
00130 const std::string& resultMessage = str(boost::format(msg) % param1 % param2);
00131 Error resultError(lineNum, fileName, errorCode, resultMessage.c_str());
00132 resultError.AddError(subError);
00133 return resultError;
00134 }
00135
00136 template <typename T1, typename T2, typename T3>
00137 inline Error CreateError(
00138 unsigned lineNum,
00139 const char* fileName,
00140 unsigned errorCode,
00141 const char* msg,
00142 const T1& param1,
00143 const T2& param2,
00144 const T3& param3)
00145 {
00146 const std::string& resultMessage = str(boost::format(msg) % param1 % param2 % param3);
00147 return Error(lineNum, fileName, errorCode, resultMessage.c_str());
00148 }
00149
00150 template <typename T1, typename T2, typename T3>
00151 inline Error CreateError(
00152 unsigned lineNum,
00153 const char* fileName,
00154 unsigned errorCode,
00155 const char* msg,
00156 const T1& param1,
00157 const T2& param2,
00158 const T3& param3,
00159 const Common::Error& subError)
00160 {
00161 const std::string& resultMessage = str(boost::format(msg) % param1 % param2 % param3);
00162 Error resultError(lineNum, fileName, errorCode, resultMessage.c_str());
00163 resultError.AddError(subError);
00164 return resultError;
00165 }
00166
00167 template <typename T1, typename T2, typename T3, typename T4>
00168 inline Error CreateError(
00169 unsigned lineNum,
00170 const char* fileName,
00171 unsigned errorCode,
00172 const char* msg,
00173 const T1& param1,
00174 const T2& param2,
00175 const T3& param3,
00176 const T4& param4)
00177 {
00178 const std::string& resultMessage = str(boost::format(msg) % param1 % param2 % param3 % param4);
00179 return Error(lineNum, fileName, errorCode, resultMessage.c_str());
00180 }
00181
00182 template <typename T1, typename T2, typename T3, typename T4>
00183 inline Error CreateError(
00184 unsigned lineNum,
00185 const char* fileName,
00186 unsigned errorCode,
00187 const char* msg,
00188 const T1& param1,
00189 const T2& param2,
00190 const T3& param3,
00191 const T4& param4,
00192 const Common::Error& subError)
00193 {
00194 const std::string& resultMessage = str(boost::format(msg) % param1 % param2 % param3 % param4);
00195 Error resultError(lineNum, fileName, errorCode, resultMessage.c_str());
00196 resultError.AddError(subError);
00197 return resultError;
00198 }
00199
00200
00201 }
00202
00203 #define ERROR_CODE(module_id, code) (module_id << 16 | (code & 8))
00204
00205 #define THROW_COMMON_ERROR(code, message) throw ::Common::CreateError(__LINE__, __FILE__, code, message)
00206 #define CREATE_COMMON_ERROR(code, message) Common::CreateError(__LINE__, __FILE__, code, message)
00207 #define THROW_ERROR(data) throw ::Common::CreateError(__LINE__, __FILE__, data.ErrorCode, data.ErrorMessage)
00208 #define CREATE_ERROR(data) ::Common::CreateError(__LINE__, __FILE__, data.ErrorCode, data.ErrorMessage)
00209
00211 #define CREATE_ERROR1(data, param1) (::Common::CreateError(__LINE__, __FILE__, data.ErrorCode, data.ErrorMessage, param1))
00212 #define THROW_ERROR1(data, param1) throw CREATE_ERROR1(data, param1);
00213
00214 #define CREATE_ERROR2(data, param1, param2) ::Common::CreateError(__LINE__, __FILE__, data.ErrorCode, data.ErrorMessage, param1, param2)
00215 #define THROW_ERROR2(data, param1, param2) throw CREATE_ERROR2(data, param1, param2)
00216
00217 #define CREATE_ERROR3(data, param1, param2, param3) ::Common::CreateError(__LINE__, __FILE__, data.ErrorCode, data.ErrorMessage, param1, param2, param3)
00218 #define THROW_ERROR3(data, param1, param2, param3) throw CREATE_ERROR3(data, param1, param2, param3)
00219
00220 #define CREATE_ERROR4(data, param1, param2, param3, param4) ::Common::CreateError(__LINE__, __FILE__, data.ErrorCode, data.ErrorMessage, param1, param2, param3, param4)
00221 #define THROW_ERROR4(data, param1, param2, param3, param4) throw CREATE_ERROR4(data, param1, param2, param3, param4)
00222
00223
00224 #include "errors.h"
00225
00226 #define THROW_OS_ERROR(UserMsg) (throw ::Common::CreateError(__LINE__, __FILE__, errno, "%1% %2%.", UserMsg, strerror(errno)))
00227
00228 #define BEGIN_TRY_BLOCK try{
00229 #define END_TRY_BLOCK(handler)\
00230 }\
00231 catch (const ::Common::Error& err)\
00232 {\
00233 return handler(err);\
00234 }\
00235 catch (const std::exception& exc)\
00236 {\
00237 return handler(CREATE_ERROR1(StdException, exc.what()));\
00238 }
00239
00240 #endif // __MOZOPC_EXCEPTION__H__