source: trunk/debathena/third/schroot/sbuild/sbuild-error.h @ 24167

Revision 24167, 8.1 KB checked in by broder, 15 years ago (diff)
Import schroot upstream into subversion.
Line 
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
30namespace sbuild
31{
32
33  /**
34   * Error exception base class.
35   */
36  class error_base : public std::runtime_error
37  {
38  protected:
39    /**
40     * The constructor.
41     *
42     * @param error the error message.
43     */
44    error_base(std::string const& error):
45      runtime_error(error),
46      reason()
47    {
48    }
49
50    /**
51     * The constructor.
52     *
53     * @param error the error message.
54     * @param reason further information about the error
55     */
56    error_base(std::string const& error,
57          std::string const& reason):
58      runtime_error(error),
59      reason(reason)
60    {
61    }
62
63  public:
64    /// The destructor.
65    virtual ~error_base () throw ()
66    {}
67
68    /**
69     * Get the reason for the error.
70     *
71     * @returns the reason.
72     */
73    virtual const char *
74    why () const throw ()
75    {
76      return this->reason.c_str();
77    }
78
79    /**
80     * Get the reason for the error.
81     *
82     * @returns the reason.
83     */
84    std::string const&
85    get_reason () const
86    {
87      return this->reason;
88    }
89
90    /**
91     * Set the reason for the error.
92     *
93     * @param reason further information about the error
94     */
95    void
96    set_reason (std::string const& reason)
97    {
98      this->reason = reason;
99    }
100
101  private:
102    /// The reason for the error.
103    std::string reason;
104  };
105
106  /**
107   * Error exception class.
108   */
109  template <typename T>
110  class error : public error_base
111  {
112  public:
113    /// The enum type providing the error codes for this type.
114    typedef T error_type;
115    /// Mapping between error code and error description.
116    typedef std::map<error_type,const char *> map_type;
117
118    /**
119     * The constructor.
120     *
121     * @param error the error message.
122     */
123    error(std::string const& error):
124      error_base(error)
125    {
126    }
127
128    /**
129     * The constructor.
130     *
131     * @param error the error message.
132     * @param reason further information about the error
133     */
134    error(std::string const& error,
135          std::string const& reason):
136      error_base(error, reason)
137    {
138    }
139
140    /// The destructor.
141    virtual ~error () throw ()
142    {}
143
144  private:
145    /// Mapping between error code and string.
146    static map_type error_strings;
147
148    /**
149     * Get a translated error string.
150     *
151     * @param error the error code.
152     * @returns a translated error string.
153     */
154    static const char *
155    get_error (error_type error);
156
157  protected:
158    /**
159     * Format an error message.
160     *
161     * @param context1 context of the error.
162     * @param context2 additional context of the error.
163     * @param context3 additional context of the error.
164     * @param error the error code.
165     * @param detail1 details of the error.
166     * @param detail2 additional details of the error.
167     * @param detail3 additional details of the error.
168     * @returns a translated error message.
169     *
170     * @todo Merge the logic shared between the two specialisations to
171     * prevent code duplication.
172     */
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
184    /**
185     * Format an error message.
186     *
187     * @param context1 context of the error.
188     * @param context2 additional context of the error.
189     * @param context3 additional context of the error.
190     * @param error the error code.
191     * @param detail1 details of the error.
192     * @param detail2 additional details of the error.
193     * @param detail3 additional details of the error.
194     * @returns a translated error message.
195     */
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
207    /**
208     * Format an reason string.
209     *
210     * @param context1 context of the error.
211     * @param context2 additional context of the error.
212     * @param context3 additional context of the error.
213     * @param error the error or error code.
214     * @param detail1 details of the error.
215     * @param detail2 additional details of the error.
216     * @param detail3 additional details of the error.
217     * @returns a translated error message.
218     */
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
230    /**
231     * Add detail to format string.
232     *
233     * @param fmt the format string.
234     * @param value the value to add.
235     */
236    template<typename A>
237    static void
238    add_detail(boost::format& fmt,
239               A const&       value);
240
241    /**
242     * Helper class to add detail to format string.
243     * Used for non-exception types.
244     */
245    template<typename A, bool b>
246    struct add_detail_helper
247    {
248      /**
249       * The constructor.
250       *
251       * @param fmt the format string to add to.
252       * @param value the value to add.
253       */
254      add_detail_helper(boost::format& fmt,
255                        A const&       value)
256      {
257        fmt % value;
258      }
259    };
260
261    /**
262     * Helper class to add detail to format string.
263     * Used for exception types.
264     */
265    template<typename A>
266    struct add_detail_helper<A, true>
267    {
268      /**
269       * The constructor.
270       *
271       * @param fmt the format string to add to.
272       * @param value the exception to add.
273       */
274      add_detail_helper(boost::format& fmt,
275                        A const&       value)
276      {
277        fmt % value.what();
278      }
279    };
280
281    /**
282     * Add reason to reason string.
283     *
284     * @param reason the reason string.
285     * @param value the value to add.
286     */
287    template<typename A>
288    static void
289    add_reason(std::string& reason,
290               A const&     value);
291
292    /**
293     * Helper class to add reason to reason string.
294     * Used for non-exception types.
295     */
296    template<typename A, bool b>
297    struct add_reason_helper
298    {
299      /**
300       * The constructor.
301       *
302       * @param reason the reason to add to.
303       * @param value the value to add.
304       */
305      add_reason_helper(std::string& reason,
306                        A const&     value)
307      {
308      }
309    };
310
311    /**
312     * Helper class to add reason to reason string.
313     * Used for exception types.
314     */
315    template<typename A>
316    struct add_reason_helper<A, true>
317    {
318      /**
319       * The constructor.
320       *
321       * @param reason the reason to add to.
322       * @param value the exception to add.
323       */
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 */
Note: See TracBrowser for help on using the repository browser.