summaryrefslogtreecommitdiff
path: root/subprojects/boost-sqlite/doc/reference/error.adoc
blob: 424c7dd12814f905c7172e9d2eb07c98244c00c0 (plain)
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
== `sqlite/error.hpp`

=== `sqlite_category`

The sqlite_category is a `boost::system::error_category` to be used with sqlite errors.

=== `error_info`

The `error_info` class hold additional information about error conditions stored in an sqlite-allocate string.

Contains an error message describing what happened. Not all error conditions are able to generate this extended information - those that
can't will have an empty error message.

The `error_info` allocates memory from the sqlite pool and holds it.

[source,cpp]
----
struct error_info
{
    // Default constructor.
    error_info() = default;

    // Initialization constructor. Copies the message into a newly create buffer.
    error_info(core::string_view msg) noexcept;
    // set the message by copy
    void set_message(core::string_view msg);

    // Reset the buffer. If `c` is not null, its ownership is transferred into the error_info object.
    void reset(char * c = nullptr);

    // Format a message into a newly allocated buffer.
    cstring_ref format(cstring_ref fmt, ...);
    // Format a message into the existing buffer.
    cstring_ref snformat(cstring_ref fmt, ...);
    /// reserve data in the buffer i.e. allocate
    void reserve(std::size_t sz);

    // Get the allocated memory
    std::size_t capacity() const;

    // Gets the error message.
    cstring_ref message() const noexcept;

    // Release the underlying memory. It must be freed using `sqlite_free` later.
    char * release();
    // Restores the message to its initial state. Does not release memory.
    void clear() noexcept;

};
----

=== `error`

The `error` class holds `error_info` and a `code` and can be used with https://www.boost.org/doc/libs/master/libs/system/doc/html/system.html#ref_boostsystemresult_hpp[`boost::system::result`].

[source,cpp]
----
/**
 * \brief An error containing both a code & optional message.
 * \ingroup reference
 * \details Contains an error .
 */
struct error
{
  // The code of the error.
  int code;
  // The additional information of the error
  error_info info;

  // Create an error with code & message
  error(int code, error_info info) ;
  error(int code, core::string_view info);
  error(system::error_code code, error_info info)  // <1>
  // Create an error with only a code.
  explicit error(int code);

  error(system::error_code code);
  // Create an empty error;
  error() = default;
  error(error && ) noexcept = default;
};

// For compatability with system::result;
void throw_exception_from_error( error const & e, boost::source_location const & loc );

template<typename T = void>
using result = system::result<T, error>;
----
<1> If code.category() is not `sqlite_category`, the code will be set to `SQLITE_FAIL`.