pion  5.0.6
error.hpp
1 // ---------------------------------------------------------------------
2 // pion: a Boost C++ framework for building lightweight HTTP interfaces
3 // ---------------------------------------------------------------------
4 // Copyright (C) 2007-2014 Splunk Inc. (https://github.com/splunk/pion)
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // See http://www.boost.org/LICENSE_1_0.txt
8 //
9 
10 #ifndef __PION_ERROR_HEADER__
11 #define __PION_ERROR_HEADER__
12 
13 #include <string>
14 #include <sstream>
15 #include <exception>
16 #include <boost/version.hpp>
17 #include <boost/throw_exception.hpp>
18 #include <boost/exception/exception.hpp>
19 #include <boost/exception/info.hpp>
20 #include <boost/exception/error_info.hpp>
21 #include <boost/exception/get_error_info.hpp>
22 #if BOOST_VERSION >= 104700
23 #include <boost/units/io.hpp>
24 #endif
25 #include <pion/config.hpp>
26 
27 
28 namespace pion { // begin namespace pion
29 
30  //
31  // exception: simple exception class for pion errors that generates what()
32  // strings with more descriptive messages and optionally arguments as well
33  //
34  class exception
35  : public virtual std::exception, public virtual boost::exception
36  {
37  public:
38  exception() {}
39  exception(const std::string& msg) : m_what_msg(msg) {}
40  exception(const char * const msg) : m_what_msg(msg) {}
41  virtual ~exception() throw () {}
42  virtual const char* what() const throw() {
43  if (m_what_msg.empty()) update_what_msg();
44  return m_what_msg.c_str();
45  }
46  protected:
47  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 {
48  std::ostringstream tmp;
49 #if BOOST_VERSION >= 104700
50  tmp << ( msg ? msg : boost::units::detail::demangle(BOOST_EXCEPTION_DYNAMIC_TYPEID(*this).type_->name()) );
51 #else
52  tmp << ( msg ? msg : boost::units::detail::demangle(BOOST_EXCEPTION_DYNAMIC_TYPEID(*this).type_.name()) );
53 #endif
54  if (arg1 || arg2 || arg3) tmp << ':';
55  if (arg1) tmp << ' ' << *arg1;
56  if (arg2) tmp << ' ' << *arg2;
57  if (arg3) tmp << ' ' << *arg3;
58  m_what_msg = tmp.str();
59  }
60  virtual void update_what_msg() const { set_what_msg(); }
61  mutable std::string m_what_msg;
62  };
63 
64 
71  template <class T>
72  static inline std::string
73  diagnostic_information( T const & e )
74  {
75  boost::exception const * const be = dynamic_cast<const boost::exception*>(&e);
76  std::exception const * const se = dynamic_cast<const std::exception*>(&e);
77  std::ostringstream tmp;
78  if (se) {
79  tmp << se->what();
80  } else {
81 #if BOOST_VERSION >= 104700
82  tmp << boost::units::detail::demangle(BOOST_EXCEPTION_DYNAMIC_TYPEID(e).type_->name());
83 #else
84  tmp << boost::units::detail::demangle(BOOST_EXCEPTION_DYNAMIC_TYPEID(e).type_.name());
85 #endif
86  }
87  if (be) {
88  //char const * const * fn=boost::get_error_info<boost::throw_function>(*be);
89  //if (fn) tmp << " at " << *fn;
90  char const * const * f=boost::get_error_info<boost::throw_file>(*be);
91  if (f) {
92  tmp << " [" << *f;
93  if (int const * l=boost::get_error_info<boost::throw_line>(*be))
94  tmp << ':' << *l;
95  tmp << "]";
96  }
97  }
98  return tmp.str();
99  }
100 
101 
102  namespace error { // begin namespace error
103 
104  //
105  // pion error info types
106  //
107 
109  typedef boost::error_info<struct errinfo_arg_name_,std::string> errinfo_message;
110 
112  typedef boost::error_info<struct errinfo_arg_name_,std::string> errinfo_arg_name;
113 
115  typedef boost::error_info<struct errinfo_file_name_,std::string> errinfo_file_name;
116 
118  typedef boost::error_info<struct errinfo_dir_name_,std::string> errinfo_dir_name;
119 
121  typedef boost::error_info<struct errinfo_plugin_name_,std::string> errinfo_plugin_name;
122 
124  typedef boost::error_info<struct errinfo_dir_name_,std::string> errinfo_symbol_name;
125 
126 
127  //
128  // pion exception types
129  //
130 
132  class bad_arg : public pion::exception {
133  virtual void update_what_msg() const {
134  set_what_msg("bad argument", boost::get_error_info<errinfo_arg_name>(*this));
135  }
136  };
137 
139  class bad_config : public pion::exception {
140  virtual void update_what_msg() const {
141  set_what_msg("config parser error", boost::get_error_info<errinfo_file_name>(*this));
142  }
143  };
144 
146  class open_file : public pion::exception {
147  virtual void update_what_msg() const {
148  set_what_msg("unable to open file", boost::get_error_info<errinfo_file_name>(*this));
149  }
150  };
151 
153  class open_plugin : public pion::exception {
154  virtual void update_what_msg() const {
155  set_what_msg("unable to open plugin", boost::get_error_info<errinfo_plugin_name>(*this));
156  }
157  };
158 
160  class read_file : public pion::exception {
161  virtual void update_what_msg() const {
162  set_what_msg("unable to read file", boost::get_error_info<errinfo_file_name>(*this));
163  }
164  };
165 
168  virtual void update_what_msg() const {
169  set_what_msg("file not found", boost::get_error_info<errinfo_file_name>(*this));
170  }
171  };
172 
175  virtual void update_what_msg() const {
176  set_what_msg("directory not found", boost::get_error_info<errinfo_dir_name>(*this));
177  }
178  };
179 
182  virtual void update_what_msg() const {
183  set_what_msg("plugin not found", boost::get_error_info<errinfo_plugin_name>(*this));
184  }
185  };
186 
189  virtual void update_what_msg() const {
190  set_what_msg("duplicate plugin", boost::get_error_info<errinfo_plugin_name>(*this));
191  }
192  };
193 
196  virtual void update_what_msg() const {
197  set_what_msg("missing plugin symbol", boost::get_error_info<errinfo_symbol_name>(*this));
198  }
199  };
200 
203  virtual void update_what_msg() const {
204  set_what_msg("plugin has undefined state");
205  }
206  };
207 
210  virtual void update_what_msg() const {
211  set_what_msg("bad password hash");
212  }
213  };
214 
215  } // end namespace error
216 
217 } // end namespace pion
218 
219 #endif
exception thrown if we are unable to open a plugin
Definition: error.hpp:153
exception thrown if a plugin is missing a required symbol
Definition: error.hpp:195
exception thrown if a required directory is not found
Definition: error.hpp:174
exception thrown if we try to add or load a duplicate plugin
Definition: error.hpp:188
exception thrown if a plugin has an undefined state
Definition: error.hpp:202
exception thrown if a file is not found
Definition: error.hpp:167
exception thrown if we failed to open a file
Definition: error.hpp:146
exception thrown if a plugin cannot be found
Definition: error.hpp:181
exception thrown if a bad password hash is provided
Definition: error.hpp:209
exception thrown if we failed to read data from a file
Definition: error.hpp:160
exception thrown for an invalid configuration argument or option
Definition: error.hpp:132
exception thrown if there is an error parsing a configuration file
Definition: error.hpp:139