error.h
Go to the documentation of this file.
#ifndef WREPORT_ERROR_H
#define WREPORT_ERROR_H
#include <stdexcept>
#include <string>
namespace wreport {
enum ErrorCode {
WR_ERR_NONE = 0,
WR_ERR_NOTFOUND = 1,
WR_ERR_TYPE = 2,
WR_ERR_ALLOC = 3,
WR_ERR_ODBC = 4,
WR_ERR_HANDLES = 5,
WR_ERR_TOOLONG = 6,
WR_ERR_SYSTEM = 7,
WR_ERR_CONSISTENCY = 8,
WR_ERR_PARSE = 9,
WR_ERR_WRITE = 10,
WR_ERR_REGEX = 11,
WR_ERR_UNIMPLEMENTED = 12,
WR_ERR_DOMAIN = 13
};
#define WREPORT_THROWF_ATTRS(a, b) __attribute__ ((noreturn, format(printf, a, b)))
class error : public std::exception
{
public:
virtual ErrorCode code() const noexcept = 0;
virtual const char* what() const noexcept = 0;
static const char* strerror(ErrorCode code);
};
class error_alloc : public error
{
public:
const char* msg;
error_alloc(const char* msg) : msg(msg) {}
~error_alloc() {}
ErrorCode code() const noexcept override { return WR_ERR_ALLOC; }
const char* what() const noexcept override { return msg; }
};
namespace errors {
template<ErrorCode ERROR_CODE>
class StringBase : public error
{
public:
std::string msg;
StringBase(const std::string& msg) noexcept : msg(msg) {}
ErrorCode code() const noexcept override { return ERROR_CODE; }
const char* what() const noexcept override { return msg.c_str(); }
};
}
class error_notfound : public errors::StringBase<WR_ERR_NOTFOUND>
{
public:
using StringBase::StringBase;
static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
};
class error_type : public errors::StringBase<WR_ERR_TYPE>
{
public:
using StringBase::StringBase;
static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
};
class error_handles : public errors::StringBase<WR_ERR_HANDLES>
{
public:
using StringBase::StringBase;
static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
};
class error_toolong : public errors::StringBase<WR_ERR_TOOLONG>
{
public:
using StringBase::StringBase;
static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
};
class error_system : public errors::StringBase<WR_ERR_SYSTEM>
{
public:
error_system(const std::string& msg);
error_system(const std::string& msg, int errno_val);
static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
};
class error_consistency : public errors::StringBase<WR_ERR_CONSISTENCY>
{
public:
using StringBase::StringBase;
static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
};
class error_parse : public errors::StringBase<WR_ERR_PARSE>
{
public:
using StringBase::StringBase;
error_parse(const char* file, int line, const std::string& msg);
static void throwf(const char* file, int line, const char* fmt, ...) WREPORT_THROWF_ATTRS(3, 4);
};
class error_regexp : public errors::StringBase<WR_ERR_REGEX>
{
public:
error_regexp(int code, void* re, const std::string& msg);
static void throwf(int code, void* re, const char* fmt, ...) WREPORT_THROWF_ATTRS(3, 4);
};
class error_unimplemented : public errors::StringBase<WR_ERR_UNIMPLEMENTED>
{
public:
using StringBase::StringBase;
static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
};
class error_domain : public errors::StringBase<WR_ERR_DOMAIN>
{
public:
using StringBase::StringBase;
static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
};
}
#endif
wreport::error_alloc
Reports that memory allocation has failed.
Definition error.h:78
wreport::error_alloc::what
const char * what() const noexcept override
Throw the exception, building the message printf-style.
Definition error.h:95
wreport::error_alloc::code
ErrorCode code() const noexcept override
Exception-specific error code.
Definition error.h:92
wreport::error_alloc::msg
const char * msg
error message returned by what()
Definition error.h:81
wreport::error_alloc::error_alloc
error_alloc(const char *msg)
Definition error.h:89
wreport::error_consistency
Report an error when a consistency check failed.
Definition error.h:192
wreport::error_consistency::throwf
static void throwf(const char *fmt,...) WREPORT_THROWF_ATTRS(1
Throw the exception, building the message printf-style.
wreport::error_domain
Report that a parameter is outside the acceptable domain.
Definition error.h:251
wreport::error_domain::throwf
static void throwf(const char *fmt,...) WREPORT_THROWF_ATTRS(1
Throw the exception, building the message printf-style.
wreport::error_handles
For functions working with handles, reports a problem with handling handles, such as impossibility to...
Definition error.h:145
wreport::error_handles::throwf
static void throwf(const char *fmt,...) WREPORT_THROWF_ATTRS(1
Throw the exception, building the message printf-style.
wreport::error_notfound
Reports that a search-like function could not find what was requested.
Definition error.h:118
wreport::error_notfound::throwf
static void throwf(const char *fmt,...) WREPORT_THROWF_ATTRS(1
Throw the exception, building the message printf-style.
wreport::error_parse::throwf
static void throwf(const char *file, int line, const char *fmt,...) WREPORT_THROWF_ATTRS(3
Throw the exception, building the message printf-style.
wreport::error_parse::error_parse
error_parse(const char *file, int line, const std::string &msg)
wreport::error_regexp::error_regexp
error_regexp(int code, void *re, const std::string &msg)
wreport::error_regexp::throwf
static void throwf(int code, void *re, const char *fmt,...) WREPORT_THROWF_ATTRS(3
Throw the exception, building the message printf-style.
wreport::error_system::error_system
error_system(const std::string &msg, int errno_val)
Create an exception taking further information from an explicit errno value.
wreport::error_system::error_system
error_system(const std::string &msg)
Create an exception taking further information from errno.
wreport::error_system::throwf
static void throwf(const char *fmt,...) WREPORT_THROWF_ATTRS(1
Throw the exception, building the message printf-style.
wreport::error_toolong
Report an error with a buffer being to short for the data it needs to fit.
Definition error.h:155
wreport::error_toolong::throwf
static void throwf(const char *fmt,...) WREPORT_THROWF_ATTRS(1
Throw the exception, building the message printf-style.
wreport::error_type
For functions handling data with multiple types, reports a mismatch between the type requested and th...
Definition error.h:131
wreport::error_type::throwf
static void throwf(const char *fmt,...) WREPORT_THROWF_ATTRS(1
Throw the exception, building the message printf-style.
wreport::error_unimplemented
Reports that a feature is still not implemented.
Definition error.h:241
wreport::error_unimplemented::throwf
static void throwf(const char *fmt,...) WREPORT_THROWF_ATTRS(1
Throw the exception, building the message printf-style.
wreport::error
Base class for DB-All.e exceptions.
Definition error.h:60
wreport::error::code
virtual ErrorCode code() const noexcept=0
Exception-specific error code.
wreport::error::strerror
static const char * strerror(ErrorCode code)
String description for an error code.
wreport::error::what
virtual const char * what() const noexcept=0
Error message.
wreport::errors::StringBase
Definition error.h:101
wreport::errors::StringBase::code
ErrorCode code() const noexcept override
Exception-specific error code.
Definition error.h:109
wreport::errors::StringBase::StringBase
StringBase(const std::string &msg) noexcept
Definition error.h:107
wreport::errors::StringBase::msg
std::string msg
error message returned by what()
Definition error.h:104
wreport::errors::StringBase::what
const char * what() const noexcept override
Error message.
Definition error.h:111
WREPORT_THROWF_ATTRS
#define WREPORT_THROWF_ATTRS(a, b)
Tell the compiler that a function always throws and expects printf-style arguments.
Definition error.h:56
wreport
String functions.
Definition benchmark.h:13
wreport::ErrorCode
ErrorCode
C-style error codes used by exceptions.
Definition error.h:21
wreport::WR_ERR_NONE
@ WR_ERR_NONE
No error.
Definition error.h:23
wreport::WR_ERR_ODBC
@ WR_ERR_ODBC
ODBC error.
Definition error.h:31
wreport::WR_ERR_TOOLONG
@ WR_ERR_TOOLONG
Buffer is too short to fit data.
Definition error.h:35
wreport::WR_ERR_HANDLES
@ WR_ERR_HANDLES
Handle management error.
Definition error.h:33
wreport::WR_ERR_UNIMPLEMENTED
@ WR_ERR_UNIMPLEMENTED
Feature not implemented.
Definition error.h:47
wreport::WR_ERR_SYSTEM
@ WR_ERR_SYSTEM
Error reported by the system.
Definition error.h:37
wreport::WR_ERR_REGEX
@ WR_ERR_REGEX
Regular expression error.
Definition error.h:45
wreport::WR_ERR_DOMAIN
@ WR_ERR_DOMAIN
Value outside acceptable domain.
Definition error.h:49
wreport::WR_ERR_TYPE
@ WR_ERR_TYPE
Wrong variable type.
Definition error.h:27
wreport::WR_ERR_CONSISTENCY
@ WR_ERR_CONSISTENCY
Consistency check failed.
Definition error.h:39
wreport::WR_ERR_WRITE
@ WR_ERR_WRITE
Write error.
Definition error.h:43
wreport::WR_ERR_NOTFOUND
@ WR_ERR_NOTFOUND
Item not found.
Definition error.h:25
wreport::WR_ERR_PARSE
@ WR_ERR_PARSE
Parse error.
Definition error.h:41
wreport::WR_ERR_ALLOC
@ WR_ERR_ALLOC
Cannot allocate memory.
Definition error.h:29