PageRenderTime 30ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/mordor/exception.h

http://github.com/mozy/mordor
C Header | 146 lines | 117 code | 27 blank | 2 comment | 0 complexity | 879451aea42e1d64e52d7d753e6f9175 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. #ifndef __MORDOR_EXCEPTION_H__
  2. #define __MORDOR_EXCEPTION_H__
  3. // Copyright (c) 2009 - Mozy, Inc.
  4. #include "predef.h"
  5. #include <stdexcept>
  6. #include <string>
  7. #include <vector>
  8. #include <boost/exception/all.hpp>
  9. #include "version.h"
  10. #ifdef WINDOWS
  11. #include <windows.h>
  12. #include <winerror.h>
  13. #else
  14. #include <errno.h>
  15. #endif
  16. namespace Mordor {
  17. typedef boost::error_info<struct tag_backtrace, std::vector<void *> > errinfo_backtrace;
  18. #ifdef WINDOWS
  19. typedef boost::error_info<struct tag_lasterror, DWORD> errinfo_lasterror;
  20. std::string to_string( errinfo_lasterror const & e );
  21. typedef errinfo_lasterror errinfo_nativeerror;
  22. #else
  23. typedef boost::errinfo_errno errinfo_nativeerror;
  24. #endif
  25. std::string to_string( const std::vector<void *> bt );
  26. std::string to_string( errinfo_backtrace const &bt );
  27. std::vector<void *> backtrace(int framesToSkip = 0);
  28. void removeTopFrames(boost::exception &ex, int framesToSkip = 0);
  29. #define MORDOR_THROW_EXCEPTION(x) \
  30. throw ::boost::enable_current_exception(::boost::enable_error_info(x)) \
  31. << ::boost::throw_function(BOOST_CURRENT_FUNCTION) \
  32. << ::boost::throw_file(__FILE__) \
  33. << ::boost::throw_line((int)__LINE__) \
  34. << ::Mordor::errinfo_backtrace(::Mordor::backtrace())
  35. void rethrow_exception(boost::exception_ptr const & ep);
  36. struct Exception : virtual boost::exception, virtual std::exception {};
  37. struct StreamException : virtual Exception {};
  38. struct UnexpectedEofException : virtual StreamException {};
  39. struct WriteBeyondEofException : virtual StreamException {};
  40. struct BufferOverflowException : virtual StreamException {};
  41. struct NativeException : virtual Exception {};
  42. #ifdef WINDOWS
  43. // error_t is a struct so that operator <<(ostream, error_t) is not ambiguous
  44. struct error_t
  45. {
  46. error_t(DWORD v = 0u) : value(v) {}
  47. operator DWORD() { return value; }
  48. DWORD value;
  49. };
  50. #else
  51. struct error_t
  52. {
  53. error_t(int v = 0) : value(v) {}
  54. operator int() { return value; }
  55. int value;
  56. };
  57. #endif
  58. struct OperationNotSupportedException : virtual NativeException {};
  59. struct FileNotFoundException : virtual NativeException {};
  60. struct AccessDeniedException : virtual NativeException {};
  61. struct BadHandleException : virtual NativeException {};
  62. struct OperationAbortedException : virtual NativeException {};
  63. struct BrokenPipeException : virtual NativeException {};
  64. struct SharingViolation : virtual NativeException {};
  65. struct UnresolvablePathException : virtual NativeException {};
  66. struct IsDirectoryException : virtual UnresolvablePathException {};
  67. struct IsNotDirectoryException : virtual UnresolvablePathException {};
  68. struct TooManySymbolicLinksException : virtual UnresolvablePathException {};
  69. struct OutOfDiskSpaceException : virtual NativeException {};
  70. struct InvalidUnicodeException : virtual NativeException {};
  71. struct WrongProtocolTypeException : virtual NativeException {};
  72. error_t lastError();
  73. void lastError(error_t error);
  74. std::ostream &operator <<(std::ostream &os, error_t error);
  75. void throwExceptionFromLastError(error_t lastError);
  76. #define MORDOR_THROW_EXCEPTION_FROM_LAST_ERROR() \
  77. try { \
  78. ::Mordor::throwExceptionFromLastError(::Mordor::lastError()); \
  79. } catch (::boost::exception &ex) { \
  80. ex << ::boost::throw_function(BOOST_CURRENT_FUNCTION) \
  81. << ::boost::throw_file(__FILE__) \
  82. << ::boost::throw_line((int)__LINE__) \
  83. << ::Mordor::errinfo_backtrace(::Mordor::backtrace()); \
  84. throw; \
  85. }
  86. #define MORDOR_THROW_EXCEPTION_FROM_LAST_ERROR_API(api) \
  87. try { \
  88. ::Mordor::throwExceptionFromLastError(::Mordor::lastError()); \
  89. } catch (::boost::exception &ex) { \
  90. ex << ::boost::throw_function(BOOST_CURRENT_FUNCTION) \
  91. << ::boost::throw_file(__FILE__) \
  92. << ::boost::throw_line((int)__LINE__) \
  93. << ::boost::errinfo_api_function(api) \
  94. << ::Mordor::errinfo_backtrace(::Mordor::backtrace()); \
  95. throw; \
  96. }
  97. #define MORDOR_THROW_EXCEPTION_FROM_ERROR(error) \
  98. try { \
  99. ::Mordor::throwExceptionFromLastError(error); \
  100. } catch (::boost::exception &ex) { \
  101. ex << ::boost::throw_function(BOOST_CURRENT_FUNCTION) \
  102. << ::boost::throw_file(__FILE__) \
  103. << ::boost::throw_line((int)__LINE__) \
  104. << ::Mordor::errinfo_backtrace(::Mordor::backtrace()); \
  105. throw; \
  106. }
  107. #define MORDOR_THROW_EXCEPTION_FROM_ERROR_API(error, api) \
  108. try { \
  109. ::Mordor::throwExceptionFromLastError(error); \
  110. } catch (::boost::exception &ex) { \
  111. ex << ::boost::throw_function(BOOST_CURRENT_FUNCTION) \
  112. << ::boost::throw_file(__FILE__) \
  113. << ::boost::throw_line((int)__LINE__) \
  114. << ::boost::errinfo_api_function(api) \
  115. << ::Mordor::errinfo_backtrace(::Mordor::backtrace()); \
  116. throw; \
  117. }
  118. }
  119. #endif