libsl3 1.2.41002
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 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 textual representantion (the name) of an ErrCode
41 * \param ec wanted ErrCode name
42 * \return the ErrCode name
43 */
44 constexpr const char*
46 {
47 return ec == ErrCode::SQL3Error ? "SQLite3Error"
48 : ec == ErrCode::NoConnection ? "NoConnection"
49 : ec == ErrCode::OutOfRange ? "OutOfRange"
50 : ec == ErrCode::TypeMisMatch ? "TypeMisMatch"
51 : ec == ErrCode::NullValueAccess ? "NullValueAccess"
52 : ec == ErrCode::UNEXPECTED ? "UNEXPECTED"
53 : "NA";
54 }
55
56 /**
57 * \brief Exception base type
58 *
59 * A command base for all ErrType objects.
60 * All exceptions thrown by the library wil have this base type.
61 */
62 class LIBSL3_API Error : public std::runtime_error
63 {
64 protected:
65 public:
66 /// Inherit constructors from std::runtime_error
67 using std::runtime_error::runtime_error;
68
69 /**
70 * \brief Get ErrCode
71 * \return the Errcode of the excetion
72 */
73 virtual ErrCode getId () const = 0;
74 };
75
76 /**
77 * \brief overloaded stream operator
78 * \param os ostream
79 * \param e the Error
80 * \return the ostream
81 */
82 LIBSL3_API std::ostream& operator<< (std::ostream& os, const Error& e);
83
84 /**
85 * \brief Object class representing an ErrCode
86 *
87 * Allows typedef objects using an Errcode.
88 * Each sl3::ErrCode becomes an ErrType object.
89 */
90 template <ErrCode ec> class LIBSL3_API ErrType final : public Error
91 {
92 public:
93 ErrType ()
94 : Error ("")
95 {
96 }
97
98 using Error::Error;
99
100 ErrCode
101 getId () const override
102 {
103 return ec;
104 }
105 };
106
107 /// error that will be thrown if no open database was available but needed
109
110 /// thrown if an index op is out of range
112
113 /**
114 * \brief thrown in case of an incorrect type usage
115 */
117
118 /// thrown in case of accessing a Null value field/parameter
120
121 /// thrown if something unexpected happened, mostly used by test tools and in
122 /// debug mode
124
125 /**
126 * \brief packaging an error from sqlite3.
127 * This exception will be thrown if sqlite3 reports an error.
128 *
129 * Holds the sqlite3 error code and the error message if it is available.
130 * \see Database::getMostRecentErrMsg
131 * \see Database::getMostRecentErrCode
132 *
133 * \n Extended error codes can be obtained through Database class.
134 * \see Database::getMostRecentExtendedErrCode
135 * \see Database::enableExtendedResultCodes
136 *
137 */
138 template <> class LIBSL3_API ErrType<ErrCode::SQL3Error> final : public Error
139 {
140 public:
141 /**
142 * \brief c'tor
143 * \param sl3ec sqite error code
144 * \param sl3msg sqite error code
145 * \param msg additional message
146 */
147 ErrType (int sl3ec, const char* sl3msg, const std::string& msg)
148 : Error ("(" + std::to_string (sl3ec) + ":" + sl3msg + "):" + msg)
149 , _sqlite_ec (sl3ec)
150 , _sqlite_msg (sl3msg)
151 {
152 }
153
154 /**
155 * \brief c'tor
156 * \param sl3ec sqite error code
157 * \param sl3msg sqite error code
158 */
159 ErrType (int sl3ec, const char* sl3msg)
160 : ErrType (sl3ec, sl3msg, "")
161 {
162 }
163 ErrCode
164 getId () const override
165 {
166 return ErrCode::SQL3Error;
167 }
168
169 /**
170 * \brief Get the sqlite3 error code
171 * \return the sqlite3 error code
172 */
173 int
175 {
176 return _sqlite_ec;
177 }
178
179 /**
180 * \brief Get the sqlite3 error message.
181 *
182 * If the exception was created with a sqlite a error message
183 * it can be accessed here.
184 *
185 * \return the sqlite3 error message
186 */
187 const std::string&
189 {
190 return _sqlite_msg;
191 };
192
193 private:
194 int _sqlite_ec;
195 std::string _sqlite_msg;
196 };
197
198 /**
199 * \brief Alias for ErrType<ErrCode::SQL3Error>
200 *
201 * A short alias for an ErrType<ErrCode::SQL3Error>
202 */
204
205#define ASSERT_EXCEPT(exp, except) \
206 if (!(exp)) \
207 throw except (std::string (__FUNCTION__) + ": " + #exp)
208
209 /*
210 TODO check if this can be done, togehter with __FUNCTION__
211 into a nice code locastion for nicer messages
212 File.cpp::Function::39 The problem
213
214
215 using cstr = const char * const;
216
217 static constexpr cstr past_last_slash(cstr str, cstr last_slash)
218 {
219 return
220 *str == '\0' ? last_slash :
221 *str == '/' ? past_last_slash(str + 1, str + 1) :
222 past_last_slash(str + 1, last_slash);
223 }
224
225 static constexpr cstr past_last_slash(cstr str)
226 {
227 return past_last_slash(str, str);
228 }
229
230 #define __SHORT_FILE__ ({constexpr cstr sf__ {past_last_slash(__FILE__)};
231 sf__;})
232
233 */
234
235} // ns
236
237#endif /* ...ERROR_HPP_ */
packaging an error from sqlite3. This exception will be thrown if sqlite3 reports an error.
Definition: error.hpp:139
ErrType(int sl3ec, const char *sl3msg, const std::string &msg)
c'tor
Definition: error.hpp:147
int SQLiteErrorCode() const
Get the sqlite3 error code.
Definition: error.hpp:174
ErrCode getId() const override
Get ErrCode.
Definition: error.hpp:164
const std::string & SQLiteErrorMessage() const
Get the sqlite3 error message.
Definition: error.hpp:188
ErrType(int sl3ec, const char *sl3msg)
c'tor
Definition: error.hpp:159
Object class representing an ErrCode.
Definition: error.hpp:91
ErrCode getId() const override
Get ErrCode.
Definition: error.hpp:101
Exception base type.
Definition: error.hpp:63
virtual ErrCode getId() const =0
Get ErrCode.
Namespace of libSL3.
Definition: columns.hpp:18
ErrCode
Error codes.
Definition: error.hpp:30
@ SQL3Error
sqlite3 error happened
@ OutOfRange
index op out of range
@ NoConnection
no database
@ UNEXPECTED
for everything that happens unexpected
@ NullValueAccess
accessing a value that is Null
@ TypeMisMatch
type cast problem
LIBSL3_API std::ostream & operator<<(std::ostream &stm, const sl3::DbValue &v)
Stream op for a DbValue.
constexpr const char * ErrCodeName(ErrCode ec)
get textual representantion (the name) of an ErrCode
Definition: error.hpp:45
STL namespace.