sbuild  1.6.10
sbuild-error.h
1 /* Copyright © 2005-2007 Roger Leigh <rleigh@debian.org>
2  *
3  * schroot is free software: you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * schroot is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  *********************************************************************/
18 
19 #ifndef SBUILD_ERROR_H
20 #define SBUILD_ERROR_H
21 
22 #include <map>
23 #include <stdexcept>
24 #include <string>
25 #include <typeinfo>
26 
27 #include <boost/format.hpp>
28 #include <boost/type_traits.hpp>
29 
30 namespace sbuild
31 {
32 
36  class error_base : public std::runtime_error
37  {
38  protected:
44  error_base(std::string const& error):
45  runtime_error(error),
46  reason()
47  {
48  }
49 
56  error_base(std::string const& error,
57  std::string const& reason):
58  runtime_error(error),
59  reason(reason)
60  {
61  }
62 
63  public:
65  virtual ~error_base () throw ()
66  {}
67 
73  virtual const char *
74  why () const throw ()
75  {
76  return this->reason.c_str();
77  }
78 
84  std::string const&
85  get_reason () const
86  {
87  return this->reason;
88  }
89 
95  void
96  set_reason (std::string const& reason)
97  {
98  this->reason = reason;
99  }
100 
101  private:
103  std::string reason;
104  };
105 
109  template <typename T>
110  class error : public error_base
111  {
112  public:
114  typedef T error_type;
116  typedef std::map<error_type,const char *> map_type;
117 
123  error(std::string const& error):
124  error_base(error)
125  {
126  }
127 
134  error(std::string const& error,
135  std::string const& reason):
136  error_base(error, reason)
137  {
138  }
139 
141  virtual ~error () throw ()
142  {}
143 
144  private:
146  static map_type error_strings;
147 
154  static const char *
155  get_error (error_type error);
156 
157  protected:
173  template <typename A, typename B, typename C,
174  typename D, typename E, typename F>
175  static std::string
176  format_error (A const& context1,
177  B const& context2,
178  C const& context3,
179  error_type error,
180  D const& detail1,
181  E const& detail2,
182  F const& detail3);
183 
196  template <typename A, typename B, typename C,
197  typename D, typename E, typename F>
198  static std::string
199  format_error (A const& context1,
200  B const& context2,
201  C const& context3,
202  std::runtime_error const& error,
203  D const& detail1,
204  E const& detail2,
205  F const& detail3);
206 
219  template <typename A, typename B, typename C,
220  typename R, typename D, typename E, typename F>
221  static std::string
222  format_reason (A const& context1,
223  B const& context2,
224  C const& context3,
225  R const& error,
226  D const& detail1,
227  E const& detail2,
228  F const& detail3);
229 
236  template<typename A>
237  static void
238  add_detail(boost::format& fmt,
239  A const& value);
240 
245  template<typename A, bool b>
247  {
254  add_detail_helper(boost::format& fmt,
255  A const& value)
256  {
257  fmt % value;
258  }
259  };
260 
265  template<typename A>
266  struct add_detail_helper<A, true>
267  {
274  add_detail_helper(boost::format& fmt,
275  A const& value)
276  {
277  fmt % value.what();
278  }
279  };
280 
287  template<typename A>
288  static void
289  add_reason(std::string& reason,
290  A const& value);
291 
296  template<typename A, bool b>
298  {
305  add_reason_helper(std::string& reason,
306  A const& value)
307  {
308  }
309  };
310 
315  template<typename A>
316  struct add_reason_helper<A, true>
317  {
324  add_reason_helper(std::string& reason,
325  A const& value)
326  {
327  try
328  {
329  sbuild::error_base const& eb(dynamic_cast<sbuild::error_base const&>(value));
330  if (!reason.empty())
331  reason += '\n';
332  reason += eb.why();
333  }
334  catch (std::bad_cast const& discard)
335  {
336  }
337  }
338  };
339 
340  };
341 
342 }
343 
344 #include "sbuild-error.tcc"
345 
346 #endif /* SBUILD_ERROR_H */
347 
348 /*
349  * Local Variables:
350  * mode:C++
351  * End:
352  */
Helper class to add detail to format string.
Definition: sbuild-error.h:246
Helper class to add reason to reason string.
Definition: sbuild-error.h:297
static map_type error_strings
Mapping between error code and string.
Definition: sbuild-error.h:146
add_reason_helper(std::string &reason, A const &value)
The constructor.
Definition: sbuild-error.h:305
Debian source builder components.
Definition: sbuild-auth-null.h:24
void set_reason(std::string const &reason)
Set the reason for the error.
Definition: sbuild-error.h:96
T error_type
The enum type providing the error codes for this type.
Definition: sbuild-error.h:114
virtual const char * why() const
Get the reason for the error.
Definition: sbuild-error.h:74
virtual ~error_base()
The destructor.
Definition: sbuild-error.h:65
std::string const & get_reason() const
Get the reason for the error.
Definition: sbuild-error.h:85
error_base(std::string const &error)
The constructor.
Definition: sbuild-error.h:44
add_reason_helper(std::string &reason, A const &value)
The constructor.
Definition: sbuild-error.h:324
Error exception base class.
Definition: sbuild-error.h:36
std::string reason
The reason for the error.
Definition: sbuild-error.h:103
Error exception class.
Definition: sbuild-error.h:110
error(std::string const &error, std::string const &reason)
The constructor.
Definition: sbuild-error.h:134
virtual ~error()
The destructor.
Definition: sbuild-error.h:141
std::map< error_type, const char * > map_type
Mapping between error code and error description.
Definition: sbuild-error.h:116
add_detail_helper(boost::format &fmt, A const &value)
The constructor.
Definition: sbuild-error.h:254
error_base(std::string const &error, std::string const &reason)
The constructor.
Definition: sbuild-error.h:56
add_detail_helper(boost::format &fmt, A const &value)
The constructor.
Definition: sbuild-error.h:274
error(std::string const &error)
The constructor.
Definition: sbuild-error.h:123