libwreport 3.40
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
18namespace wreport {
19
51
56#define WREPORT_THROWF_ATTRS(a, b) \
57 __attribute__((noreturn, format(printf, a, b)))
58
60class error : public std::exception
61{
62public:
68 virtual ErrorCode code() const noexcept = 0;
69
71 const char* what() const noexcept override = 0;
72
74 static const char* strerror(ErrorCode code);
75};
76
78class error_alloc : public error
79{
80public:
82 const char* msg;
83
90 error_alloc(const char* msg) : msg(msg) {}
91 ~error_alloc() {}
92
93 ErrorCode code() const noexcept override { return WR_ERR_ALLOC; }
94
96 const char* what() const noexcept override { return msg; }
97};
98
99namespace errors {
100template <ErrorCode ERROR_CODE> class StringBase : public error
101{
102public:
104 std::string msg;
105
107 StringBase(const std::string& msg) noexcept : msg(msg) {}
108
109 ErrorCode code() const noexcept override { return ERROR_CODE; }
110
111 const char* what() const noexcept override { return msg.c_str(); }
112};
113} // namespace errors
114
116class error_notfound : public errors::StringBase<WR_ERR_NOTFOUND>
117{
118public:
119 using StringBase::StringBase;
120
122 static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
123};
124
129class error_type : public errors::StringBase<WR_ERR_TYPE>
130{
131public:
132 using StringBase::StringBase;
133
135 static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
136};
137
143class error_handles : public errors::StringBase<WR_ERR_HANDLES>
144{
145public:
146 using StringBase::StringBase;
147
149 static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
150};
151
153class error_toolong : public errors::StringBase<WR_ERR_TOOLONG>
154{
155public:
156 using StringBase::StringBase;
157
159 static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
160};
161
166class error_system : public errors::StringBase<WR_ERR_SYSTEM>
167{
168public:
174 error_system(const std::string& msg);
175
183 error_system(const std::string& msg, int errno_val);
184
186 static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
187};
188
190class error_consistency : public errors::StringBase<WR_ERR_CONSISTENCY>
191{
192public:
193 using StringBase::StringBase;
194
196 static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
197};
198
200class error_parse : public errors::StringBase<WR_ERR_PARSE>
201{
202public:
203 using StringBase::StringBase;
204
213 error_parse(const char* file, int line, const std::string& msg);
214
216 static void throwf(const char* file, int line, const char* fmt, ...)
218};
219
221class error_regexp : public errors::StringBase<WR_ERR_REGEX>
222{
223public:
233 error_regexp(int code, void* re, const std::string& msg);
234
236 static void throwf(int code, void* re, const char* fmt, ...)
238};
239
241class error_unimplemented : public errors::StringBase<WR_ERR_UNIMPLEMENTED>
242{
243public:
244 using StringBase::StringBase;
245
247 static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
248};
249
251class error_domain : public errors::StringBase<WR_ERR_DOMAIN>
252{
253public:
254 using StringBase::StringBase;
255
257 static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
258};
259
260} // namespace wreport
261#endif
Reports that memory allocation has failed.
Definition error.h:79
const char * what() const noexcept override
Throw the exception, building the message printf-style.
Definition error.h:96
ErrorCode code() const noexcept override
Exception-specific error code.
Definition error.h:93
const char * msg
error message returned by what()
Definition error.h:82
error_alloc(const char *msg)
Definition error.h:90
Report an error when a consistency check failed.
Definition error.h:191
static void throwf(const char *fmt,...) WREPORT_THROWF_ATTRS(1
Throw the exception, building the message printf-style.
Report that a parameter is outside the acceptable domain.
Definition error.h:252
static void throwf(const char *fmt,...) WREPORT_THROWF_ATTRS(1
Throw the exception, building the message printf-style.
For functions working with handles, reports a problem with handling handles, such as impossibility to...
Definition error.h:144
static void throwf(const char *fmt,...) WREPORT_THROWF_ATTRS(1
Throw the exception, building the message printf-style.
Reports that a search-like function could not find what was requested.
Definition error.h:117
static void throwf(const char *fmt,...) WREPORT_THROWF_ATTRS(1
Throw the exception, building the message printf-style.
Report an error when parsing informations.
Definition error.h:201
static void throwf(const char *file, int line, const char *fmt,...) WREPORT_THROWF_ATTRS(3
Throw the exception, building the message printf-style.
error_parse(const char *file, int line, const std::string &msg)
Report an error while handling regular expressions.
Definition error.h:222
error_regexp(int code, void *re, const std::string &msg)
static void throwf(int code, void *re, const char *fmt,...) WREPORT_THROWF_ATTRS(3
Throw the exception, building the message printf-style.
Report a system error message.
Definition error.h:167
error_system(const std::string &msg, int errno_val)
Create an exception taking further information from an explicit errno value.
error_system(const std::string &msg)
Create an exception taking further information from errno.
static void throwf(const char *fmt,...) WREPORT_THROWF_ATTRS(1
Throw the exception, building the message printf-style.
Report an error with a buffer being to short for the data it needs to fit.
Definition error.h:154
static void throwf(const char *fmt,...) WREPORT_THROWF_ATTRS(1
Throw the exception, building the message printf-style.
For functions handling data with multiple types, reports a mismatch between the type requested and th...
Definition error.h:130
static void throwf(const char *fmt,...) WREPORT_THROWF_ATTRS(1
Throw the exception, building the message printf-style.
Reports that a feature is still not implemented.
Definition error.h:242
static void throwf(const char *fmt,...) WREPORT_THROWF_ATTRS(1
Throw the exception, building the message printf-style.
Base class for DB-All.e exceptions.
Definition error.h:61
virtual ErrorCode code() const noexcept=0
Exception-specific error code.
static const char * strerror(ErrorCode code)
String description for an error code.
const char * what() const noexcept override=0
Error message.
Definition error.h:101
ErrorCode code() const noexcept override
Exception-specific error code.
Definition error.h:109
StringBase(const std::string &msg) noexcept
Definition error.h:107
std::string msg
error message returned by what()
Definition error.h:104
const char * what() const noexcept override
Error message.
Definition error.h:111
#define WREPORT_THROWF_ATTRS(a, b)
Tell the compiler that a function always throws and expects printf-style arguments.
Definition error.h:56
String functions.
Definition benchmark.h:13
ErrorCode
C-style error codes used by exceptions.
Definition error.h:21
@ WR_ERR_NONE
No error.
Definition error.h:23
@ WR_ERR_ODBC
ODBC error.
Definition error.h:31
@ WR_ERR_TOOLONG
Buffer is too short to fit data.
Definition error.h:35
@ WR_ERR_HANDLES
Handle management error.
Definition error.h:33
@ WR_ERR_UNIMPLEMENTED
Feature not implemented.
Definition error.h:47
@ WR_ERR_SYSTEM
Error reported by the system.
Definition error.h:37
@ WR_ERR_REGEX
Regular expression error.
Definition error.h:45
@ WR_ERR_DOMAIN
Value outside acceptable domain.
Definition error.h:49
@ WR_ERR_TYPE
Wrong variable type.
Definition error.h:27
@ WR_ERR_CONSISTENCY
Consistency check failed.
Definition error.h:39
@ WR_ERR_WRITE
Write error.
Definition error.h:43
@ WR_ERR_NOTFOUND
Item not found.
Definition error.h:25
@ WR_ERR_PARSE
Parse error.
Definition error.h:41
@ WR_ERR_ALLOC
Cannot allocate memory.
Definition error.h:29