mns_exception.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #ifndef MNS_SDK_EXCEPTION
  2. #define MNS_SDK_EXCEPTION
  3. #include <exception>
  4. #include <string>
  5. namespace mns
  6. {
  7. namespace sdk
  8. {
  9. #ifndef FUNCTION_NAME
  10. #ifdef __PRETTY_FUNCTION__
  11. #define FUNCTION_NAME __PRETTY_FUNCTION__
  12. #elif defined(__FUNCTION__)
  13. #define FUNCTION_NAME __FUNCTION__
  14. #elif defined(__func__)
  15. #define FUNCTION_NAME __func__
  16. #else
  17. #define FUNCTION_NAME ""
  18. #endif
  19. #endif
  20. #define MNS_THROW(ExClass, msg) \
  21. do \
  22. { \
  23. ExClass e(msg); \
  24. e.Init(__FILE__, FUNCTION_NAME, __LINE__); \
  25. throw e; \
  26. } \
  27. while(false)
  28. #define MNS_DEFINE_EXCEPTION(ExClass, Base) \
  29. ExClass(const std::string& msg = "")throw() \
  30. : Base(msg) \
  31. {} \
  32. ~ExClass() throw() {} \
  33. /*override*/ std::string GetClassName() const \
  34. { \
  35. return #ExClass; \
  36. }
  37. struct ErrorInfo
  38. {
  39. std::string errorMessage;
  40. std::string code;
  41. std::string requestId;
  42. std::string hostId;
  43. int httpStatus;
  44. };
  45. class MNSExceptionBase : public std::exception
  46. {
  47. public:
  48. MNSExceptionBase(const std::string& msg = "") throw();
  49. virtual ~MNSExceptionBase() throw();
  50. void Init(const char* file, const char* func, int line);
  51. #ifndef _WIN32
  52. virtual std::string GetClassName() const;
  53. virtual std::string GetMessage() const;
  54. #endif
  55. virtual std::string GetExceptionClassName() const;
  56. virtual std::string GetExceptionMessage() const;
  57. const char* what() const throw();
  58. const std::string& ToString() const;
  59. std::string GetStackTrace() const;
  60. protected:
  61. std::string mMsg;
  62. const char* mFile;
  63. const char* mFunc;
  64. int mLine;
  65. private:
  66. enum { MAX_STACK_TRACE_SIZE = 50 };
  67. void* mStackTrace[MAX_STACK_TRACE_SIZE];
  68. size_t mStackTraceSize;
  69. mutable std::string mWhat;
  70. };
  71. class MNSServerException : public MNSExceptionBase
  72. {
  73. public:
  74. MNSServerException(const ErrorInfo& errorInfo) throw()
  75. : MNSExceptionBase(errorInfo.errorMessage)
  76. {
  77. mErrorInfo.errorMessage = errorInfo.errorMessage;
  78. mErrorInfo.code = errorInfo.code;
  79. mErrorInfo.requestId = errorInfo.requestId;
  80. mErrorInfo.hostId = errorInfo.hostId;
  81. mErrorInfo.httpStatus = errorInfo.httpStatus;
  82. }
  83. ~MNSServerException() throw(){}
  84. std::string GetClassName() const
  85. {
  86. return "MNSServerException";
  87. }
  88. const std::string& GetRequestId() const
  89. {
  90. return mErrorInfo.requestId;
  91. }
  92. const std::string& GetErrorCode() const
  93. {
  94. return mErrorInfo.code;
  95. }
  96. const std::string& GetHostId() const
  97. {
  98. return mErrorInfo.hostId;
  99. }
  100. const int GetHttpStatus() const
  101. {
  102. return mErrorInfo.httpStatus;
  103. }
  104. const std::string& GetErrorMessage() const
  105. {
  106. return mErrorInfo.errorMessage;
  107. }
  108. ErrorInfo mErrorInfo;
  109. };
  110. }
  111. }
  112. #endif