This repository has been archived by the owner on Jan 3, 2024. It is now read-only.
forked from fnc12/sqlite_orm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_code.h
169 lines (145 loc) · 6.04 KB
/
error_code.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#pragma once
#include <sqlite3.h>
#include <system_error> // std::error_code, std::system_error
#include <string> // std::string
#include <stdexcept>
#include <sstream> // std::ostringstream
#include <type_traits>
namespace sqlite_orm {
/** @short Enables classifying sqlite error codes.
@note We don't bother listing all possible values;
this also allows for compatibility with
'Construction rules for enum class values (P0138R2)'
*/
enum class sqlite_errc {};
enum class orm_error_code {
not_found = 1,
type_is_not_mapped_to_storage,
trying_to_dereference_null_iterator,
too_many_tables_specified,
incorrect_set_fields_specified,
column_not_found,
table_has_no_primary_key_column,
cannot_start_a_transaction_within_a_transaction,
no_active_transaction,
incorrect_journal_mode_string,
invalid_collate_argument_enum,
failed_to_init_a_backup,
unknown_member_value,
incorrect_order,
cannot_use_default_value,
arguments_count_does_not_match,
function_not_found,
index_is_out_of_bounds,
value_is_null,
no_tables_specified,
};
}
namespace std {
template<>
struct is_error_code_enum<::sqlite_orm::sqlite_errc> : true_type {};
template<>
struct is_error_code_enum<::sqlite_orm::orm_error_code> : true_type {};
}
namespace sqlite_orm {
class orm_error_category : public std::error_category {
public:
const char* name() const noexcept override final {
return "ORM error";
}
std::string message(int c) const override final {
switch(static_cast<orm_error_code>(c)) {
case orm_error_code::not_found:
return "Not found";
case orm_error_code::type_is_not_mapped_to_storage:
return "Type is not mapped to storage";
case orm_error_code::trying_to_dereference_null_iterator:
return "Trying to dereference null iterator";
case orm_error_code::too_many_tables_specified:
return "Too many tables specified";
case orm_error_code::incorrect_set_fields_specified:
return "Incorrect set fields specified";
case orm_error_code::column_not_found:
return "Column not found";
case orm_error_code::table_has_no_primary_key_column:
return "Table has no primary key column";
case orm_error_code::cannot_start_a_transaction_within_a_transaction:
return "Cannot start a transaction within a transaction";
case orm_error_code::no_active_transaction:
return "No active transaction";
case orm_error_code::invalid_collate_argument_enum:
return "Invalid collate_argument enum";
case orm_error_code::failed_to_init_a_backup:
return "Failed to init a backup";
case orm_error_code::unknown_member_value:
return "Unknown member value";
case orm_error_code::incorrect_order:
return "Incorrect order";
case orm_error_code::cannot_use_default_value:
return "The statement 'INSERT INTO * DEFAULT VALUES' can be used with only one row";
case orm_error_code::arguments_count_does_not_match:
return "Arguments count does not match";
case orm_error_code::function_not_found:
return "Function not found";
case orm_error_code::index_is_out_of_bounds:
return "Index is out of bounds";
case orm_error_code::value_is_null:
return "Value is null";
case orm_error_code::no_tables_specified:
return "No tables specified";
default:
return "unknown error";
}
}
};
class sqlite_error_category : public std::error_category {
public:
const char* name() const noexcept override final {
return "SQLite error";
}
std::string message(int c) const override final {
return sqlite3_errstr(c);
}
};
inline const orm_error_category& get_orm_error_category() {
static orm_error_category res;
return res;
}
inline const sqlite_error_category& get_sqlite_error_category() {
static sqlite_error_category res;
return res;
}
inline std::error_code make_error_code(sqlite_errc ev) noexcept {
return {static_cast<int>(ev), get_sqlite_error_category()};
}
inline std::error_code make_error_code(orm_error_code ev) noexcept {
return {static_cast<int>(ev), get_orm_error_category()};
}
template<typename... T>
std::string get_error_message(sqlite3* db, T&&... args) {
std::ostringstream stream;
using unpack = int[];
static_cast<void>(unpack{0, (static_cast<void>(static_cast<void>(stream << args)), 0)...});
stream << sqlite3_errmsg(db);
return stream.str();
}
template<typename... T>
[[noreturn]] void throw_error(sqlite3* db, T&&... args) {
throw std::system_error{sqlite_errc(sqlite3_errcode(db)), get_error_message(db, std::forward<T>(args)...)};
}
inline std::system_error sqlite_to_system_error(int ev) {
return {sqlite_errc(ev)};
}
inline std::system_error sqlite_to_system_error(sqlite3* db) {
return {sqlite_errc(sqlite3_errcode(db)), sqlite3_errmsg(db)};
}
[[noreturn]] inline void throw_translated_sqlite_error(int ev) {
throw sqlite_to_system_error(ev);
}
[[noreturn]] inline void throw_translated_sqlite_error(sqlite3* db) {
throw sqlite_to_system_error(db);
}
[[noreturn]] inline void throw_translated_sqlite_error(sqlite3_stmt* stmt) {
throw sqlite_to_system_error(sqlite3_db_handle(stmt));
}
}