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

Revision 24167, 5.9 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_CUSTOM_ERROR_TCC
20#define SBUILD_CUSTOM_ERROR_TCC
21
22#include <sbuild/sbuild-i18n.h>
23#include <sbuild/sbuild-custom-error.h>
24#include <sbuild/sbuild-null.h>
25
26template <typename T>
27template <typename A, typename B, typename C,
28          typename D, typename E, typename F>
29inline std::string
30sbuild::error<T>::format_error (A const&   context1,
31                                B const&   context2,
32                                C const&   context3,
33                                error_type error,
34                                D const&   detail1,
35                                E const&   detail2,
36                                F const&   detail3)
37{
38  std::string format;
39  std::string msg(get_error(error));
40  unsigned int nargs(0);
41
42  if (msg.find("%1%") != std::string::npos)
43    {
44      nargs = 1;
45    }
46  else if(typeid(context1) != typeid(sbuild::null))
47    {
48      format += "%1%: ";
49      nargs = 1;
50    }
51
52  if (msg.find("%2%") != std::string::npos)
53    {
54      nargs = 2;
55    }
56  else if (typeid(context2) != typeid(sbuild::null))
57    {
58      format += "%2%: ";
59      nargs = 2;
60    }
61
62  if (msg.find("%3%") != std::string::npos)
63    {
64      nargs = 3;
65    }
66  else if (typeid(context3) != typeid(sbuild::null))
67    {
68      format += "%3%: ";
69      nargs = 3;
70    }
71
72  format += msg;
73
74  if (msg.find("%4%") != std::string::npos)
75    {
76      nargs = 4;
77    }
78  else if (typeid(detail1) != typeid(sbuild::null))
79    {
80      if (msg.empty())
81        format += "%4%";
82      else
83        format += ": %4%";
84      nargs = 4;
85    }
86
87  if (msg.find("%5%") != std::string::npos)
88    {
89      nargs = 5;
90    }
91  else if (typeid(detail2) != typeid(sbuild::null))
92    {
93      if (msg.empty() && nargs < 4)
94        format += "%5%";
95      else
96        format += ": %5%";
97      nargs = 5;
98    }
99
100  if (msg.find("%6%") != std::string::npos)
101    {
102      nargs = 6;
103    }
104  else if (typeid(detail3) != typeid(sbuild::null))
105    {
106      if (msg.empty() && nargs < 4)
107        format += "%6%";
108      else
109        format += ": %6%";
110      nargs = 6;
111    }
112
113  boost::format fmt(format);
114  if (nargs >= 1)
115    add_detail(fmt, context1);
116  if (nargs >= 2)
117    add_detail(fmt, context2);
118  if (nargs >= 3)
119    add_detail(fmt, context3);
120  if (nargs >= 4)
121    add_detail(fmt, detail1);
122  if (nargs >= 5)
123    add_detail(fmt, detail2);
124  if (nargs >= 6)
125    add_detail(fmt, detail3);
126
127  return fmt.str();
128}
129
130template <typename T>
131template <typename A, typename B, typename C,
132          typename D, typename E, typename F>
133inline std::string
134sbuild::error<T>::format_error (A const&   context1,
135                                B const&   context2,
136                                C const&   context3,
137                                std::runtime_error const& error,
138                                D const&   detail1,
139                                E const&   detail2,
140                                F const&   detail3)
141{
142  std::string format;
143  std::string msg(error.what());
144  unsigned int nargs(0);
145
146  if (typeid(context1) != typeid(sbuild::null))
147    {
148      format += "%1%: ";
149      nargs = 1;
150    }
151
152  if (typeid(context2) != typeid(sbuild::null))
153    {
154      format += "%2%: ";
155      nargs = 2;
156    }
157
158  if (typeid(context3) != typeid(sbuild::null))
159    {
160      format += "%3%: ";
161      nargs = 3;
162    }
163
164  format += msg;
165
166  if (typeid(detail1) != typeid(sbuild::null))
167    {
168      if (msg.empty())
169        format += "%4%";
170      else
171        format += ": %4%";
172      nargs = 4;
173
174    }
175
176  if (typeid(detail2) != typeid(sbuild::null))
177    {
178      if (msg.empty() && nargs < 4)
179        format += "%5%";
180      else
181        format += ": %5%";
182      nargs = 5;
183    }
184
185  if (typeid(detail3) != typeid(sbuild::null))
186    {
187      if (msg.empty() && nargs < 4)
188        format += "%6%";
189      else
190        format += ": %6%";
191      nargs = 6;
192    }
193
194  boost::format fmt(format);
195  if (nargs >= 1)
196    add_detail(fmt, context1);
197  if (nargs >= 2)
198    add_detail(fmt, context2);
199  if (nargs >= 3)
200    add_detail(fmt, context3);
201  if (nargs >= 4)
202    add_detail(fmt, detail1);
203  if (nargs >= 5)
204    add_detail(fmt, detail2);
205  if (nargs >= 6)
206    add_detail(fmt, detail3);
207
208  return fmt.str();
209}
210
211template<typename T>
212template<typename A>
213inline void
214sbuild::error<T>::add_detail(boost::format& fmt,
215                             A const&       value)
216{
217  add_detail_helper<A, boost::is_base_and_derived<std::exception, A>::value>
218    (fmt, value);
219}
220
221template <typename T>
222template <typename A, typename B, typename C, typename R,
223          typename D, typename E, typename F>
224inline std::string
225sbuild::error<T>::format_reason (A const&   context1,
226                                 B const&   context2,
227                                 C const&   context3,
228                                 R const&   error,
229                                 D const&   detail1,
230                                 E const&   detail2,
231                                 F const&   detail3)
232{
233  std::string reason;
234
235  add_reason(reason, context1);
236  add_reason(reason, context2);
237  add_reason(reason, context3);
238  add_reason(reason, error);
239  add_reason(reason, detail1);
240  add_reason(reason, detail2);
241  add_reason(reason, detail3);
242
243  return reason;
244}
245
246template<typename T>
247template<typename A>
248inline void
249sbuild::error<T>::add_reason(std::string& reason,
250                             A const&     value)
251{
252  add_reason_helper<A, boost::is_base_and_derived<std::exception, A>::value>
253    (reason, value);
254}
255
256template <typename T>
257inline const char *
258sbuild::error<T>::get_error (error_type error)
259{
260  typename map_type::const_iterator pos = error_strings.find(error);
261
262  if (pos != error_strings.end())
263    return gettext(pos->second);
264
265  // Untranslated: it's a programming error to get this message.
266  return "Unknown error";
267}
268
269#endif /* SBUILD_CUSTOM_ERROR_TCC */
270
271/*
272 * Local Variables:
273 * mode:C++
274 * End:
275 */
Note: See TracBrowser for help on using the repository browser.