boost_sqlite 1
A sqlite C++ library
Loading...
Searching...
No Matches
error.hpp
1//
2// Copyright (c) 2022 Klemens Morgenstern (klemens.morgenstern@gmx.net)
3//
4// Distributed under the Boost Software License, Version 1.0. (See accompanying
5// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6//
7
8#ifndef BOOST_SQLITE_ERROR_HPP
9#define BOOST_SQLITE_ERROR_HPP
10
11#include <boost/sqlite/detail/config.hpp>
12#include <boost/sqlite/cstring_ref.hpp>
13#include <boost/sqlite/memory.hpp>
14#include <boost/system/error_code.hpp>
15#include <boost/system/error_category.hpp>
16#include <boost/system/result.hpp>
17
18
19BOOST_SQLITE_BEGIN_NAMESPACE
20
21BOOST_SQLITE_DECL
22system::error_category & sqlite_category();
23
24
33{
35 error_info() = default;
36
38 error_info(core::string_view msg) noexcept : msg_(new (memory_tag{}) char[msg.size() + 1u])
39 {
40 std::memcpy(msg_.get(), msg.data(), msg.size() + 1);
41 }
42
44 void set_message(core::string_view msg)
45 {
46 reserve(msg.size() + 1u);
47 std::memcpy(msg_.get(), msg.data(), msg.size() + 1);
48 }
50 void reset(char * c = nullptr)
51 {
52 msg_.reset(c);
53 }
54
57 {
58 va_list args;
59 va_start(args, fmt);
60 msg_.reset(sqlite3_vmprintf(fmt.c_str(), args));
61 va_end(args);
62 return msg_.get();
63 }
64
67 {
68 if (capacity() == 0)
69 return "";
70 va_list args;
71 va_start(args, fmt);
72 sqlite3_vsnprintf(capacity(), msg_.get(), fmt.c_str(), args);
73 va_end(args);
74 return msg_.get();
75 }
77 void reserve(std::size_t sz)
78 {
79 if (msg_)
80 {
81 if (sqlite3_msize(msg_.get()) < sz)
82 msg_.reset(static_cast<char*>(sqlite3_realloc64(msg_.release(), sz)));
83 }
84 else
85 msg_.reset(static_cast<char*>(sqlite3_malloc64(sz)));
86 }
87
89 std::size_t capacity() const {return msg_ ? sqlite3_msize(msg_.get()) : 0u;}
90
92 cstring_ref message() const noexcept { return msg_ ? msg_.get() : ""; }
93
94 char * release()
95 {
96 return msg_.release();
97 }
99 void clear() noexcept { if (msg_) *msg_ = '\0'; }
100
101
102 operator bool() const {return msg_.operator bool();}
103 private:
104 unique_ptr<char> msg_;
105};
106
107
113struct error
114{
116 int code;
119
120 error(int code, error_info info) : code(code), info(std::move(info)) {}
121 explicit error(int code) : code(code) {}
122 error(int code, core::string_view info)
123 : code(code),
124 info(info) {}
125
126 error(system::error_code code, error_info info)
127 : code(code.category() == sqlite_category() ? code.value() : SQLITE_FAIL),
128 info(std::move(info)) {}
129
130 error(system::error_code code) : code(code.category() == sqlite_category() ? code.value() : SQLITE_FAIL)
131 {
132 if (code.category() == sqlite_category())
133 info = error_info{code.what()};
134 }
135 error() = default;
136 error(error && ) noexcept = default;
137};
138
139BOOST_NORETURN BOOST_SQLITE_DECL void throw_exception_from_error( error const & e, boost::source_location const & loc );
140
141template<typename T = void>
142using result = system::result<T, error>;
143
144template<typename T>
145struct is_result_type : std::false_type {};
146
147template<typename T>
148struct is_result_type<result<T>> : std::true_type {};
149
150
151BOOST_SQLITE_END_NAMESPACE
152
153
154#endif // BOOST_SQLITE_ERROR_HPP
Small wrapper for a null-terminated string that can be directly passed to C APIS.
Additional information about error conditions stored in an sqlite-allocate string.
Definition error.hpp:33
cstring_ref format(cstring_ref fmt,...)
use sqlite_mprintf to generate a message
Definition error.hpp:56
void clear() noexcept
Restores the object to its initial state.
Definition error.hpp:99
void reset(char *c=nullptr)
transfer ownership into the message
Definition error.hpp:50
cstring_ref message() const noexcept
Gets the error message.
Definition error.hpp:92
void reserve(std::size_t sz)
reserve data in the buffer i.e. allocate
Definition error.hpp:77
cstring_ref snformat(cstring_ref fmt,...)
use sqlite_snprintf to generate a message
Definition error.hpp:66
void set_message(core::string_view msg)
set the message by copy
Definition error.hpp:44
error_info()=default
Default constructor.
error_info(core::string_view msg) noexcept
Initialization constructor.
Definition error.hpp:38
std::size_t capacity() const
Get the allocated memory.
Definition error.hpp:89
An error containing both a code & optional message.
Definition error.hpp:114
error_info info
The additional information of the error.
Definition error.hpp:118
int code
The code of the error.
Definition error.hpp:116