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