libwreport 3.34
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
49 WR_ERR_DOMAIN = 13
50};
51
56#define WREPORT_THROWF_ATTRS(a, b) __attribute__ ((noreturn, format(printf, a, b)))
57
59class error : public std::exception
60{
61public:
67 virtual ErrorCode code() const noexcept = 0;
68
70 virtual const char* what() const noexcept = 0;
71
73 static const char* strerror(ErrorCode code);
74};
75
77class error_alloc : public error
78{
79public:
81 const char* msg;
82
89 error_alloc(const char* msg) : msg(msg) {}
90 ~error_alloc() {}
91
92 ErrorCode code() const noexcept override { return WR_ERR_ALLOC; }
93
95 const char* what() const noexcept override { return msg; }
96};
97
98namespace errors {
99template<ErrorCode ERROR_CODE>
100class 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};
114}
115
117class error_notfound : public errors::StringBase<WR_ERR_NOTFOUND>
118{
119public:
120 using StringBase::StringBase;
121
123 static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
124};
125
130class error_type : public errors::StringBase<WR_ERR_TYPE>
131{
132public:
133 using StringBase::StringBase;
134
136 static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
137};
138
144class error_handles : public errors::StringBase<WR_ERR_HANDLES>
145{
146public:
147 using StringBase::StringBase;
148
150 static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
151};
152
154class error_toolong : public errors::StringBase<WR_ERR_TOOLONG>
155{
156public:
157 using StringBase::StringBase;
158
160 static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
161};
162
167class error_system : public errors::StringBase<WR_ERR_SYSTEM>
168{
169public:
175 error_system(const std::string& msg);
176
184 error_system(const std::string& msg, int errno_val);
185
187 static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
188};
189
191class error_consistency : public errors::StringBase<WR_ERR_CONSISTENCY>
192{
193public:
194 using StringBase::StringBase;
195
197 static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
198};
199
201class error_parse : public errors::StringBase<WR_ERR_PARSE>
202{
203public:
204 using StringBase::StringBase;
205
214 error_parse(const char* file, int line, const std::string& msg);
215
217 static void throwf(const char* file, int line, const char* fmt, ...) WREPORT_THROWF_ATTRS(3, 4);
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, ...) WREPORT_THROWF_ATTRS(3, 4);
237};
238
240class error_unimplemented : public errors::StringBase<WR_ERR_UNIMPLEMENTED>
241{
242public:
243 using StringBase::StringBase;
244
246 static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
247};
248
250class error_domain : public errors::StringBase<WR_ERR_DOMAIN>
251{
252public:
253 using StringBase::StringBase;
254
256 static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
257};
258
259}
260#endif
Reports that memory allocation has failed.
Definition: error.h:78
const char * what() const noexcept override
Throw the exception, building the message printf-style.
Definition: error.h:95
ErrorCode code() const noexcept override
Exception-specific error code.
Definition: error.h:92
const char * msg
error message returned by what()
Definition: error.h:81
error_alloc(const char *msg)
Definition: error.h:89
Report an error when a consistency check failed.
Definition: error.h:192
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:251
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:145
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:118
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:202
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:168
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:155
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:131
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:241
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:60
virtual ErrorCode code() const noexcept=0
Exception-specific error code.
static const char * strerror(ErrorCode code)
String description for an error code.
virtual const char * what() const noexcept=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