libwreport  2.4
error.h
Go to the documentation of this file.
1 /*
2  * wreport/error - wreport exceptions
3  *
4  * Copyright (C) 2005--2010 ARPA-SIM <urpsim@smr.arpa.emr.it>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * Author: Enrico Zini <enrico@enricozini.com>
20  */
21 
22 #ifndef WREPORT_ERROR_H
23 #define WREPORT_ERROR_H
24 
25 #include <stdexcept>
26 #include <string>
27 
40 namespace wreport {
41 
43 enum ErrorCode {
72 };
73 
78 #define WREPORT_THROWF_ATTRS(a, b) __attribute__ ((noreturn, format(printf, a, b)))
79 
81 struct error : public std::exception
82 {
88  virtual ErrorCode code() const throw () = 0;
89 
91  virtual const char* what() const throw () = 0;
92 
94  static const char* strerror(ErrorCode code);
95 };
96 
98 struct error_notfound : public error
99 {
100  std::string msg;
101 
103  error_notfound(const std::string& msg) : msg(msg) {}
104  ~error_notfound() throw () {}
105 
106  ErrorCode code() const throw () { return WR_ERR_NOTFOUND; }
107 
108  virtual const char* what() const throw () { return msg.c_str(); }
109 
111  static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
112 };
113 
118 struct error_type : public error
119 {
120  std::string msg;
121 
123  error_type(const std::string& msg) : msg(msg) {}
124  ~error_type() throw () {}
125 
126  ErrorCode code() const throw () { return WR_ERR_TYPE; }
127 
128  virtual const char* what() const throw () { return msg.c_str(); }
129 
131  static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
132 };
133 
135 struct error_alloc : public error
136 {
137  const char* msg;
138 
145  error_alloc(const char* msg) : msg(msg) {}
146  ~error_alloc() throw () {}
147 
148  ErrorCode code() const throw () { return WR_ERR_ALLOC; }
149 
151  virtual const char* what() const throw () { return msg; }
152 };
153 
159 struct error_handles : public error
160 {
161  std::string msg;
162 
164  error_handles(const std::string& msg) : msg(msg) {}
165  ~error_handles() throw () {}
166 
167  ErrorCode code() const throw () { return WR_ERR_HANDLES; }
168 
169  virtual const char* what() const throw () { return msg.c_str(); }
170 
172  static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
173 };
174 
176 struct error_toolong : public error
177 {
178  std::string msg;
179 
181  error_toolong(const std::string& msg) : msg(msg) {}
182  ~error_toolong() throw () {}
183 
184  ErrorCode code() const throw () { return WR_ERR_TOOLONG; }
185 
186  virtual const char* what() const throw () { return msg.c_str(); }
187 
189  static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
190 };
191 
196 struct error_system : public error
197 {
198  std::string msg;
199 
205  error_system(const std::string& msg);
213  error_system(const std::string& msg, int errno_val);
214  ~error_system() throw () {}
215 
216  ErrorCode code() const throw () { return WR_ERR_SYSTEM; }
217 
218  virtual const char* what() const throw () { return msg.c_str(); }
219 
221  static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
222 };
223 
225 struct error_consistency : public error
226 {
227  std::string msg;
228 
230  error_consistency(const std::string& msg) : msg(msg) {};
231  ~error_consistency() throw () {}
232 
233  ErrorCode code() const throw () { return WR_ERR_CONSISTENCY; }
234 
235  virtual const char* what() const throw () { return msg.c_str(); }
236 
238  static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
239 };
240 
242 struct error_parse : public error
243 {
244  std::string msg;
245 
247  error_parse(const std::string& msg) : msg(msg) {}
256  error_parse(const char* file, int line, const std::string& msg);
257  ~error_parse() throw () {}
258 
259  ErrorCode code() const throw () { return WR_ERR_PARSE; }
260 
261  virtual const char* what() const throw () { return msg.c_str(); }
262 
264  static void throwf(const char* file, int line, const char* fmt, ...) WREPORT_THROWF_ATTRS(3, 4);
265 };
266 
268 struct error_regexp : public error
269 {
270  std::string msg;
271 
281  error_regexp(int code, void* re, const std::string& msg);
282  ~error_regexp() throw () {}
283 
284  ErrorCode code() const throw () { return WR_ERR_REGEX; }
285 
286  virtual const char* what() const throw () { return msg.c_str(); }
287 
289  static void throwf(int code, void* re, const char* fmt, ...) WREPORT_THROWF_ATTRS(3, 4);
290 };
291 
293 struct error_unimplemented : public error
294 {
295  std::string msg;
296 
298  error_unimplemented(const std::string& msg) : msg(msg) {};
299  ~error_unimplemented() throw () {}
300 
301  ErrorCode code() const throw () { return WR_ERR_UNIMPLEMENTED; }
302 
303  virtual const char* what() const throw () { return msg.c_str(); }
304 
306  static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
307 };
308 
310 struct error_domain : public error
311 {
312  std::string msg;
313 
315  error_domain(const std::string& msg) : msg(msg) {}
316  ~error_domain() throw () {}
317 
318  ErrorCode code() const throw () { return WR_ERR_DOMAIN; }
319 
320  virtual const char* what() const throw () { return msg.c_str(); }
321 
323  static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
324 };
325 
326 }
327 
328 /* vim:set ts=4 sw=4: */
329 #endif