libsl3 1.3.53004
A C++ interface for SQLite
Loading...
Searching...
No Matches
error.hpp
1/******************************************************************************
2 ------------- Copyright (c) 2009-2023 H a r a l d A c h i t z ---------------
3 ---------- < h a r a l d dot a c h i t z at g m a i l dot c o m > ------------
4 ---- This Source Code Form is subject to the terms of the Mozilla Public -----
5 ---- License, v. 2.0. If a copy of the MPL was not distributed with this -----
6 ---------- file, You can obtain one at http://mozilla.org/MPL/2.0/. ----------
7 ******************************************************************************/
8
9#ifndef SL3__ERROR_HPP_
10#define SL3__ERROR_HPP_
11
12#include <exception>
13#include <iosfwd>
14#include <stdexcept>
15#include <string>
16
17#include <sl3/config.hpp>
18
19namespace sl3
20{
21 /**
22 * \brief Error codes
23 *
24 * These enum values are used to create ErrType objects.
25 *
26 * Each ErrCode becomes an ErrType object.
27 *
28 */
29 enum class ErrCode
30 {
31 SQL3Error = 1, ///< sqlite3 error happened
32 NoConnection = 2, ///< no database
33 OutOfRange = 5, ///< index op out of range
34 TypeMisMatch = 6, ///< type cast problem
35 NullValueAccess = 7, ///< accessing a value that is Null
36 UNEXPECTED = 99 ///< for everything that happens unexpected
37 };
38
39 /**
40 * \brief Get the textual representation (the name) of an ErrCode
41 * \param ec
42 * wanted ErrCode name
43 * \return the ErrCode name
44 */
45 constexpr const char*
47 {
48 return ec == ErrCode::SQL3Error ? "SQLite3Error"
49 : ec == ErrCode::NoConnection ? "NoConnection"
50 : ec == ErrCode::OutOfRange ? "OutOfRange"
51 : ec == ErrCode::TypeMisMatch ? "TypeMisMatch"
52 : ec == ErrCode::NullValueAccess ? "NullValueAccess"
53 : ec == ErrCode::UNEXPECTED ? "UNEXPECTED"
54 : "NA";
55 }
56
57 /**
58 * \brief Exception base type
59 *
60 * A common base for all ErrType objects.
61 * All exceptions thrown by the
62 * library will have this base type.
63 */
64 class LIBSL3_API Error : public std::runtime_error
65 {
66 protected:
67 public:
68 /// Inherit constructors from std::runtime_error
69 using std::runtime_error::runtime_error;
70
71 /**
72 * \brief Get ErrCode
73 * \return the ErrCode of the exception
74 */
75 virtual ErrCode getId () const = 0;
76 };
77
78 /**
79 * \brief Overloaded stream operator
80 * \param os ostream
81 * \param e the Error
82 * \return the ostream
83 */
84 LIBSL3_API std::ostream& operator<< (std::ostream& os, const Error& e);
85
86 /**
87 * \brief Object class representing an ErrCode
88 *
89 * Allows typedefs using an ErrCode.
90 * Each sl3::ErrCode becomes
91 * an ErrType object.
92 */
93 template <ErrCode ec> class LIBSL3_API ErrType final : public Error
94 {
95 public:
96 ErrType ()
97 : Error ("")
98 {
99 }
100
101 using Error::Error;
102
103 ErrCode
104 getId () const override
105 {
106 return ec;
107 }
108 };
109
110 /// error that will be thrown if no open database was available but needed
112
113 /// thrown if an index op is out of range
115
116 /**
117 * \brief thrown in case of an incorrect type usage
118 */
120
121 /// thrown in case of accessing a Null value field/parameter
123
124 /// thrown if something unexpected happened, mostly used by test tools and in
125 /// debug mode
127
128#ifndef LIBSL3_DLL
129 extern template class ErrType<ErrCode::NoConnection>;
130 extern template class ErrType<ErrCode::OutOfRange>;
131 extern template class ErrType<ErrCode::TypeMisMatch>;
132 extern template class ErrType<ErrCode::NullValueAccess>;
133 extern template class ErrType<ErrCode::UNEXPECTED>;
134#endif
135
136 /**
137 * \brief Package an error from sqlite3.
138 * This exception will be thrown if sqlite3 reports an error.
139 *
140 * Holds the sqlite3 error code and the error message if it is available.
141 * \see Database::getMostRecentErrMsg
142 * \see Database::getMostRecentErrCode
143 *
144 * \n Extended error codes can be obtained through Database class.
145 * \see Database::getMostRecentExtendedErrCode
146 * \see Database::enableExtendedResultCodes
147 *
148 */
149 template <> class LIBSL3_API ErrType<ErrCode::SQL3Error> final : public Error
150 {
151 public:
152 /**
153 * \brief c'tor
154 * \param sl3ec sqlite error code
155 * \param sl3msg sqlite error
156 * message
157 * \param msg additional message
158 */
159 ErrType (int sl3ec, const char* sl3msg, const std::string& msg)
160 : Error ("(" + std::to_string (sl3ec) + ":" + sl3msg + "):" + msg)
161 , _sqlite_ec (sl3ec)
162 , _sqlite_msg (sl3msg)
163 {
164 }
165
166 /**
167 * \brief c'tor
168 * \param sl3ec sqlite error code
169 * \param sl3msg sqlite error
170 * message
171 */
172 ErrType (int sl3ec, const char* sl3msg)
173 : ErrType (sl3ec, sl3msg, "")
174 {
175 }
176 ErrCode
177 getId () const override
178 {
179 return ErrCode::SQL3Error;
180 }
181
182 /**
183 * \brief Get the sqlite3 error code
184 * \return the sqlite3 error code
185 */
186 int
188 {
189 return _sqlite_ec;
190 }
191
192 /**
193 * \brief Get the sqlite3 error message.
194 *
195 * If the exception was created with a sqlite error message,
196 * it can
197 * be accessed here.
198 *
199 * \return the sqlite3 error message
200 */
201 const std::string&
203 {
204 return _sqlite_msg;
205 };
206
207 private:
208 int _sqlite_ec;
209 std::string _sqlite_msg;
210 };
211
212 /**
213 * \brief Alias for ErrType<ErrCode::SQL3Error>
214 *
215 * A short alias for an ErrType<ErrCode::SQL3Error>
216 */
218
219#define ASSERT_EXCEPT(exp, except) \
220 if (!(exp)) \
221 throw except (std::string (__FUNCTION__) + ": " + #exp)
222
223 /*
224 TODO check if this can be done, together with __FUNCTION__,
225 into a nice
226 code location for clearer messages
227 File.cpp::Function::39 The problem
228
229
230 using cstr = const char * const;
231
232 static constexpr cstr past_last_slash(cstr str, cstr last_slash)
233 {
234 return
235 *str == '\0' ? last_slash :
236 *str == '/' ? past_last_slash(str + 1, str + 1) :
237 past_last_slash(str + 1, last_slash);
238 }
239
240 static constexpr cstr past_last_slash(cstr str)
241 {
242 return past_last_slash(str, str);
243 }
244
245 #define __SHORT_FILE__ ({constexpr cstr sf__ {past_last_slash(__FILE__)};
246 sf__;})
247
248 */
249
250} // ns
251
252#endif /* ...ERROR_HPP_ */
ErrType(int sl3ec, const char *sl3msg, const std::string &msg)
c'tor
Definition error.hpp:159
int SQLiteErrorCode() const
Get the sqlite3 error code.
Definition error.hpp:187
ErrCode getId() const override
Get ErrCode.
Definition error.hpp:177
const std::string & SQLiteErrorMessage() const
Get the sqlite3 error message.
Definition error.hpp:202
ErrType(int sl3ec, const char *sl3msg)
c'tor
Definition error.hpp:172
Object class representing an ErrCode.
Definition error.hpp:94
ErrCode getId() const override
Get ErrCode.
Definition error.hpp:104
Exception base type.
Definition error.hpp:65
virtual ErrCode getId() const =0
Get ErrCode.
Namespace of libSL3.
Definition columns.hpp:18
ErrType< ErrCode::SQL3Error > SQLite3Error
Alias for ErrType<ErrCode::SQL3Error>.
Definition error.hpp:217
ErrType< ErrCode::NoConnection > ErrNoConnection
error that will be thrown if no open database was available but needed
Definition error.hpp:111
ErrType< ErrCode::OutOfRange > ErrOutOfRange
thrown if an index op is out of range
Definition error.hpp:114
ErrCode
Error codes.
Definition error.hpp:30
@ SQL3Error
sqlite3 error happened
Definition error.hpp:31
@ OutOfRange
index op out of range
Definition error.hpp:33
@ NoConnection
no database
Definition error.hpp:32
@ UNEXPECTED
for everything that happens unexpected
Definition error.hpp:36
@ NullValueAccess
accessing a value that is Null
Definition error.hpp:35
@ TypeMisMatch
type cast problem
Definition error.hpp:34
ErrType< ErrCode::NullValueAccess > ErrNullValueAccess
thrown in case of accessing a Null value field/parameter
Definition error.hpp:122
LIBSL3_API std::ostream & operator<<(std::ostream &stm, const sl3::DbValue &v)
Stream op for a DbValue.
ErrType< ErrCode::UNEXPECTED > ErrUnexpected
Definition error.hpp:126
ErrType< ErrCode::TypeMisMatch > ErrTypeMisMatch
thrown in case of an incorrect type usage
Definition error.hpp:119
constexpr const char * ErrCodeName(ErrCode ec)
Get the textual representation (the name) of an ErrCode.
Definition error.hpp:46
STL namespace.