libwreport  3.6
error.h
Go to the documentation of this file.
1 #ifndef WREPORT_ERROR_H
2 #define WREPORT_ERROR_H
3 
4 #include <stdexcept>
5 #include <string>
6 
18 namespace wreport {
19 
21 enum ErrorCode {
24  // Item not found
25  WR_ERR_NOTFOUND = 1,
26  // Wrong variable type
27  WR_ERR_TYPE = 2,
28  // Cannot allocate memory
29  WR_ERR_ALLOC = 3,
30  // ODBC error
31  WR_ERR_ODBC = 4,
32  // Handle management error
33  WR_ERR_HANDLES = 5,
34  // Buffer is too short to fit data
35  WR_ERR_TOOLONG = 6,
36  // Error reported by the system
37  WR_ERR_SYSTEM = 7,
38  // Consistency check failed
39  WR_ERR_CONSISTENCY = 8,
40  // Parse error
41  WR_ERR_PARSE = 9,
42  // Write error
43  WR_ERR_WRITE = 10,
44  // Regular expression error
45  WR_ERR_REGEX = 11,
46  // Feature not implemented
47  WR_ERR_UNIMPLEMENTED = 12,
48  // Value outside acceptable domain
49  WR_ERR_DOMAIN = 13
50 };
51 
56 #define WREPORT_THROWF_ATTRS(a, b) __attribute__ ((noreturn, format(printf, a, b)))
57 
59 struct error : public std::exception
60 {
66  virtual ErrorCode code() const noexcept = 0;
67 
69  virtual const char* what() const noexcept = 0;
70 
72  static const char* strerror(ErrorCode code);
73 };
74 
76 struct error_alloc : public error
77 {
79  const char* msg;
80 
87  error_alloc(const char* msg) : msg(msg) {}
88  ~error_alloc() {}
89 
90  ErrorCode code() const noexcept override { return WR_ERR_ALLOC; }
91 
93  const char* what() const noexcept override { return msg; }
94 };
95 
96 namespace errors {
97 template<ErrorCode ERROR_CODE>
98 struct StringBase : public error
99 {
101  std::string msg;
102 
104  StringBase(const std::string& msg) noexcept : msg(msg) {}
105 
106  ErrorCode code() const noexcept override { return ERROR_CODE; }
107 
108  const char* what() const noexcept override { return msg.c_str(); }
109 
110 };
111 }
112 
114 struct error_notfound : public errors::StringBase<WR_ERR_NOTFOUND>
115 {
116  using StringBase::StringBase;
117 
119  static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
120 };
121 
126 struct error_type : public errors::StringBase<WR_ERR_TYPE>
127 {
128  using StringBase::StringBase;
129 
131  static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
132 };
133 
139 struct error_handles : public errors::StringBase<WR_ERR_HANDLES>
140 {
141  using StringBase::StringBase;
142 
144  static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
145 };
146 
148 struct error_toolong : public errors::StringBase<WR_ERR_TOOLONG>
149 {
150  using StringBase::StringBase;
151 
153  static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
154 };
155 
160 struct error_system : public errors::StringBase<WR_ERR_SYSTEM>
161 {
167  error_system(const std::string& msg);
168 
176  error_system(const std::string& msg, int errno_val);
177 
179  static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
180 };
181 
183 struct error_consistency : public errors::StringBase<WR_ERR_CONSISTENCY>
184 {
185  using StringBase::StringBase;
186 
188  static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
189 };
190 
192 struct error_parse : public errors::StringBase<WR_ERR_PARSE>
193 {
194  using StringBase::StringBase;
195 
204  error_parse(const char* file, int line, const std::string& msg);
205 
207  static void throwf(const char* file, int line, const char* fmt, ...) WREPORT_THROWF_ATTRS(3, 4);
208 };
209 
211 struct error_regexp : public errors::StringBase<WR_ERR_REGEX>
212 {
222  error_regexp(int code, void* re, const std::string& msg);
223 
225  static void throwf(int code, void* re, const char* fmt, ...) WREPORT_THROWF_ATTRS(3, 4);
226 };
227 
229 struct error_unimplemented : public errors::StringBase<WR_ERR_UNIMPLEMENTED>
230 {
231  using StringBase::StringBase;
232 
234  static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
235 };
236 
238 struct error_domain : public errors::StringBase<WR_ERR_DOMAIN>
239 {
240  using StringBase::StringBase;
241 
243  static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
244 };
245 
246 }
247 #endif
const char * what() const noexcept override
Error message.
Definition: error.h:108
Base class for DB-All.e exceptions.
Definition: error.h:59
virtual ErrorCode code() const noexcept=0
Exception-specific error code.
Reports that memory allocation has failed.
Definition: error.h:76
Report an error while handling regular expressions.
Definition: error.h:211
Report an error when parsing informations.
Definition: error.h:192
Report an error when a consistency check failed.
Definition: error.h:183
ErrorCode code() const noexcept override
Exception-specific error code.
Definition: error.h:106
Reports that a feature is still not implemented.
Definition: error.h:229
No error.
Definition: error.h:23
For functions working with handles, reports a problem with handling handles, such as impossibility to...
Definition: error.h:139
For functions handling data with multiple types, reports a mismatch between the type requested and th...
Definition: error.h:126
static const char * strerror(ErrorCode code)
String description for an error code.
Report that a parameter is outside the acceptable domain.
Definition: error.h:238
Reports that a search-like function could not find what was requested.
Definition: error.h:114
const char * msg
error message returned by what()
Definition: error.h:79
error_alloc(const char *msg)
Definition: error.h:87
Report an error with a buffer being to short for the data it needs to fit.
Definition: error.h:148
std::string msg
error message returned by what()
Definition: error.h:101
StringBase(const std::string &msg) noexcept
Definition: error.h:104
String functions.
Definition: benchmark.h:13
Definition: error.h:98
Report a system error message.
Definition: error.h:160
virtual const char * what() const noexcept=0
Error message.
#define WREPORT_THROWF_ATTRS(a, b)
Tell the compiler that a function always throws and expects printf-style arguments.
Definition: error.h:56
ErrorCode
C-style error codes used by exceptions.
Definition: error.h:21
const char * what() const noexcept override
Throw the exception, building the message printf-style.
Definition: error.h:93
ErrorCode code() const noexcept override
Exception-specific error code.
Definition: error.h:90