00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef __PION_ERROR_HEADER__
00011 #define __PION_ERROR_HEADER__
00012
00013 #include <string>
00014 #include <sstream>
00015 #include <exception>
00016 #include <boost/version.hpp>
00017 #include <boost/throw_exception.hpp>
00018 #include <boost/exception/exception.hpp>
00019 #include <boost/exception/info.hpp>
00020 #include <boost/exception/error_info.hpp>
00021 #include <boost/exception/get_error_info.hpp>
00022 #include <pion/config.hpp>
00023
00024
00025 namespace pion {
00026
00027
00028
00029
00030
00031 class exception
00032 : public virtual std::exception, public virtual boost::exception
00033 {
00034 public:
00035 exception() {}
00036 exception(const std::string& msg) : m_what_msg(msg) {}
00037 exception(const char * const msg) : m_what_msg(msg) {}
00038 virtual ~exception() throw () {}
00039 virtual const char* what() const throw() {
00040 if (m_what_msg.empty()) update_what_msg();
00041 return m_what_msg.c_str();
00042 }
00043 protected:
00044 inline void set_what_msg(const char * const msg = NULL, const std::string * const arg1 = NULL, const std::string * const arg2 = NULL, const std::string * const arg3 = NULL) const {
00045 std::ostringstream tmp;
00046 #if BOOST_VERSION >= 104700
00047 tmp << ( msg ? msg : boost::units::detail::demangle(BOOST_EXCEPTION_DYNAMIC_TYPEID(*this).type_->name()) );
00048 #else
00049 tmp << ( msg ? msg : boost::units::detail::demangle(BOOST_EXCEPTION_DYNAMIC_TYPEID(*this).type_.name()) );
00050 #endif
00051 if (arg1 || arg2 || arg3) tmp << ':';
00052 if (arg1) tmp << ' ' << *arg1;
00053 if (arg2) tmp << ' ' << *arg2;
00054 if (arg3) tmp << ' ' << *arg3;
00055 m_what_msg = tmp.str();
00056 }
00057 virtual void update_what_msg() const { set_what_msg(); }
00058 mutable std::string m_what_msg;
00059 };
00060
00061
00068 template <class T>
00069 static inline std::string
00070 diagnostic_information( T const & e )
00071 {
00072 boost::exception const * const be = dynamic_cast<const boost::exception*>(&e);
00073 std::exception const * const se = dynamic_cast<const std::exception*>(&e);
00074 std::ostringstream tmp;
00075 tmp << (se ? se->what() : "unknown exception");
00076 if (be) {
00077 char const * const * fn=boost::get_error_info<boost::throw_function>(*be);
00078 char const * const * f=boost::get_error_info<boost::throw_file>(*be);
00079 if (fn) tmp << " at " << *fn;
00080 if (f) {
00081 tmp << " [" << *f;
00082 if( int const * l=boost::get_error_info<boost::throw_line>(*be) )
00083 tmp << ':' << *l;
00084 }
00085 }
00086 return tmp.str();
00087 }
00088
00089
00090 namespace error {
00091
00092
00093
00094
00095
00097 typedef boost::error_info<struct errinfo_arg_name_,std::string> errinfo_message;
00098
00100 typedef boost::error_info<struct errinfo_arg_name_,std::string> errinfo_arg_name;
00101
00103 typedef boost::error_info<struct errinfo_file_name_,std::string> errinfo_file_name;
00104
00106 typedef boost::error_info<struct errinfo_dir_name_,std::string> errinfo_dir_name;
00107
00109 typedef boost::error_info<struct errinfo_plugin_name_,std::string> errinfo_plugin_name;
00110
00112 typedef boost::error_info<struct errinfo_dir_name_,std::string> errinfo_symbol_name;
00113
00114
00115
00116
00117
00118
00120 class bad_arg : public pion::exception {
00121 virtual void update_what_msg() const {
00122 set_what_msg("bad argument", boost::get_error_info<errinfo_arg_name>(*this));
00123 }
00124 };
00125
00127 class bad_config : public pion::exception {
00128 virtual void update_what_msg() const {
00129 set_what_msg("config parser error", boost::get_error_info<errinfo_file_name>(*this));
00130 }
00131 };
00132
00134 class open_file : public pion::exception {
00135 virtual void update_what_msg() const {
00136 set_what_msg("unable to open file", boost::get_error_info<errinfo_file_name>(*this));
00137 }
00138 };
00139
00141 class open_plugin : public pion::exception {
00142 virtual void update_what_msg() const {
00143 set_what_msg("unable to open plugin", boost::get_error_info<errinfo_plugin_name>(*this));
00144 }
00145 };
00146
00148 class read_file : public pion::exception {
00149 virtual void update_what_msg() const {
00150 set_what_msg("unable to read file", boost::get_error_info<errinfo_file_name>(*this));
00151 }
00152 };
00153
00155 class file_not_found : public pion::exception {
00156 virtual void update_what_msg() const {
00157 set_what_msg("file not found", boost::get_error_info<errinfo_file_name>(*this));
00158 }
00159 };
00160
00162 class directory_not_found : public pion::exception {
00163 virtual void update_what_msg() const {
00164 set_what_msg("directory not found", boost::get_error_info<errinfo_dir_name>(*this));
00165 }
00166 };
00167
00169 class plugin_not_found : public pion::exception {
00170 virtual void update_what_msg() const {
00171 set_what_msg("plugin not found", boost::get_error_info<errinfo_plugin_name>(*this));
00172 }
00173 };
00174
00176 class duplicate_plugin : public pion::exception {
00177 virtual void update_what_msg() const {
00178 set_what_msg("duplicate plugin", boost::get_error_info<errinfo_plugin_name>(*this));
00179 }
00180 };
00181
00183 class plugin_missing_symbol : public pion::exception {
00184 virtual void update_what_msg() const {
00185 set_what_msg("missing plugin symbol", boost::get_error_info<errinfo_symbol_name>(*this));
00186 }
00187 };
00188
00190 class plugin_undefined : public pion::exception {
00191 virtual void update_what_msg() const {
00192 set_what_msg("plugin has undefined state");
00193 }
00194 };
00195
00197 class bad_password_hash : public pion::exception {
00198 virtual void update_what_msg() const {
00199 set_what_msg("bad password hash");
00200 }
00201 };
00202
00203 }
00204
00205 }
00206
00207 #endif