1 /* 2 * extended exception 3 * Copyright (C) Daniel Jungmann 2007 <dsj@gmx.net> 4 */ 5 /** 6 * @file 7 * @ingroup error 8 * @brief extended exception with logging support 9 */ 10 11 #ifndef _EXTENDEDEXCEPTION_HPP_ 12 #define _EXTENDEDEXCEPTION_HPP_ 13 14 #include <exception> 15 #include <string> 16 #include <cassert> 17 #include <sstream> 18 #include "../platform.h" 19 #include "../errors.h" 20 21 namespace eternal_lands 22 { 23 24 class ExtendedException: public std::exception 25 { 26 private: 27 Uint32 m_number; 28 std::string m_description; 29 std::string m_type; 30 std::string m_file; 31 std::string m_function; 32 Uint32 m_line; 33 mutable std::string m_full_description; 34 35 public: 36 enum ExceptionCodes 37 { 38 ec_duplicate_item, 39 ec_file_not_found, 40 ec_item_not_found, 41 ec_io_error, 42 ec_invalid_parameter, 43 ec_opengl_error, 44 ec_zip_error, 45 ec_internal_error, 46 ec_not_implemented 47 }; 48 49 /** 50 * Default constructor. 51 */ ExtendedException()52 inline ExtendedException(): m_number(0), m_description(""), m_type(""), m_file(""), 53 m_function(""), m_line(0), m_full_description() {} 54 55 /** 56 * Default constructor. 57 */ 58 ExtendedException(const Uint32 number, const std::string &description, const char* type=nullptr); 59 60 /** 61 * Advanced constructor. 62 */ 63 ExtendedException(const Uint32 number, const std::string &description, const char* type, 64 const char* file, const char* function, const Uint32 line); 65 66 /** 67 * Copy constructor. 68 */ 69 ExtendedException(const ExtendedException &ee); 70 71 /** 72 * Assignment operator. 73 */ 74 ExtendedException& operator=(const ExtendedException &ee) = default; 75 76 /* 77 * Destructor, needed for compatibility with std::exception. 78 */ ~ExtendedException()79 virtual inline ~ExtendedException() throw() {} 80 81 /** 82 * Returns a string with the full description of this error. 83 */ 84 virtual const std::string& get_full_description() const; 85 86 /** 87 * Gets the error code. 88 */ get_number() const89 virtual Uint32 get_number() const throw() 90 { 91 return m_number; 92 } 93 94 /** 95 * Gets the source file name. 96 */ get_file() const97 virtual inline const std::string &get_file() const throw() 98 { 99 return m_file; 100 } 101 102 /** 103 * Gets the source function. 104 */ get_function() const105 virtual inline const std::string &get_function() const throw() 106 { 107 return m_function; 108 } 109 110 /** 111 * Gets the line number. 112 */ get_line() const113 virtual inline Uint32 get_line() const throw() 114 { 115 return m_line; 116 } 117 118 /** 119 * Returns a string with only the 'description' field of this exception. 120 * Use get_full_descriptionto get a full description of the error including 121 * line number, error number and what function threw the exception. 122 */ get_description() const123 virtual inline const std::string &get_description() const throw() 124 { 125 return m_description; 126 } 127 128 /** 129 * Override std::exception::what 130 */ what() const131 inline const char* what() const throw() 132 { 133 return get_full_description().c_str(); 134 } 135 136 }; 137 138 #define EXTENDED_EXCEPTION(num, description) \ 139 do \ 140 { \ 141 std::stringstream str; \ 142 \ 143 str << description; \ 144 \ 145 throw eternal_lands::ExtendedException(eternal_lands::num, str.str(), nullptr, \ 146 __FILE__, __FUNCTION__, __LINE__); \ 147 } \ 148 while (false) 149 150 #define CHECK_GL_EXCEPTION() \ 151 do \ 152 { \ 153 GLint gl_error; \ 154 \ 155 gl_error = glGetError(); \ 156 \ 157 if (gl_error != GL_NO_ERROR) \ 158 { \ 159 std::stringstream str; \ 160 \ 161 str << gluErrorString(gl_error); \ 162 \ 163 throw eternal_lands::ExceptionFactory::(eternal_lands::ExtendedException::ec_opengl_error, \ 164 str.str(), nullptr, __FILE__, __FUNCTION__, __LINE__); \ 165 } \ 166 } \ 167 while (false) 168 169 } // namespace eternal_lands 170 171 #define CATCH_AND_LOG_EXCEPTIONS \ 172 catch (std::exception &e) \ 173 { \ 174 LOG_ERROR("%s(): %s", __func__, e.what()); \ 175 } 176 177 #define CATCH_AND_LOG_EXCEPTIONS_WITH_RETURN(return_value) \ 178 catch (std::exception &e) \ 179 { \ 180 LOG_ERROR("%s(): %s", __func__, e.what()); \ 181 return return_value; \ 182 } 183 184 #endif // _EXTENDEDEXCEPTION_HPP_ 185