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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//! Types for working with errors produced by Musq.
use std::{io, num::TryFromIntError, result::Result as StdResult, sync::PoisonError};
use tokio::sync::TryLockError;
pub use crate::sqlite::error::{ExtendedErrCode, PrimaryErrCode};
use crate::{
SqliteDataType, sqlite,
sqlite::{Value, error::SqliteError},
};
/// A specialized `Result` type for Musq.
pub type Result<T> = StdResult<T, Error>;
/// Errors encountered while decoding values.
#[derive(thiserror::Error, Debug)]
pub enum DecodeError {
/// Incompatible source SQLite type.
#[error("incompatible source data type: {0}")]
IncompatibleDataType(SqliteDataType),
/// Conversion error from SQLite value to Rust type.
#[error("decoding conversion error: {0}")]
Conversion(String),
}
/// Errors encountered while encoding values.
#[derive(thiserror::Error, Debug)]
pub enum EncodeError {
/// Conversion error from Rust type to SQLite value.
#[error("encoding conversion error: {0}")]
Conversion(String),
}
impl From<TryFromIntError> for DecodeError {
fn from(err: TryFromIntError) -> Self {
Self::Conversion(err.to_string())
}
}
impl From<String> for DecodeError {
fn from(err: String) -> Self {
Self::Conversion(err)
}
}
impl From<String> for EncodeError {
fn from(err: String) -> Self {
Self::Conversion(err)
}
}
/// Represents all the ways a method can fail within Musq.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
/// Error returned from the database.
#[error(
"error returned from database (primary: {primary:?}, extended: {extended:?}): {message}"
)]
Sqlite {
/// Primary SQLite error code.
primary: PrimaryErrCode,
/// Extended SQLite error code.
extended: ExtendedErrCode,
/// SQLite-provided error message.
message: String,
},
/// Error communicating with the database backend.
#[error("error communicating with database: {0}")]
Io(#[from] io::Error),
/// Unexpected or invalid data encountered while communicating with the database.
///
/// This should indicate there is a programming error in Musq or there
/// is something corrupted with the connection to the database itself.
#[error("encountered unexpected or invalid data: {0}")]
Protocol(String),
/// No rows returned by a query that expected to return at least one row.
#[error("no rows returned by a query that expected to return at least one row")]
RowNotFound,
/// Type in query doesn't exist. Likely due to typo or missing user type.
#[error("type named {type_name} not found")]
TypeNotFound {
/// Name of the missing type.
type_name: String,
},
/// Column index was out of bounds.
#[error("column index out of bounds: the len is {len}, but the index is {index}")]
ColumnIndexOutOfBounds {
/// Out-of-range index.
index: usize,
/// Available column count.
len: usize,
},
/// No column found for the given name.
#[error("no column found for name: {0}")]
ColumnNotFound(String),
/// Encountered an unknown column type code.
#[error("unknown column type: {0}")]
UnknownColumnType(i32),
/// Error occurred while decoding a value from a specific column.
#[error(
"error occurred while decoding column {column_name} at index {index} (value: {value:?}): {source}"
)]
ColumnDecode {
/// Column index or label.
index: String,
/// Column name.
column_name: String,
/// Raw SQLite value.
value: Value,
#[source]
/// Underlying decode error.
source: DecodeError,
},
/// Error occurred while decoding a value.
#[error("error occurred while decoding: {0}")]
Decode(#[source] DecodeError),
/// Error occurred while encoding a value.
#[error("error occurred while encoding: {0}")]
Encode(#[source] EncodeError),
/// A [`Pool::acquire`] timed out due to connections not becoming available or
/// because another task encountered too many errors while trying to open a new connection.
///
/// [`Pool::acquire`]: crate::Pool::acquire
#[error("pool timed out while waiting for an open connection")]
PoolTimedOut,
/// [`Pool::close`] was called while we were waiting in [`Pool::acquire`].
///
/// [`Pool::acquire`]: crate::Pool::acquire
/// [`Pool::close`]: crate::Pool::close
#[error("attempted to acquire a connection on a closed pool")]
PoolClosed,
/// A background worker has crashed.
#[error("attempted to communicate with a crashed background worker")]
WorkerCrashed,
/// `sqlite3_unlock_notify` kept returning `SQLITE_LOCKED` even after
/// resetting the blocking statement.
#[error("unlock_notify failed after multiple attempts")]
UnlockNotify,
}
impl Error {
/// Convert this error into a SQLite error if it originated there.
pub fn into_sqlite_error(self) -> Option<sqlite::error::SqliteError> {
match self {
Self::Sqlite {
primary,
extended,
message,
} => Some(sqlite::error::SqliteError {
primary,
extended,
message,
}),
_ => None,
}
}
}
impl From<SqliteError> for Error {
fn from(error: SqliteError) -> Self {
Self::Sqlite {
primary: error.primary,
extended: error.extended,
message: error.message,
}
}
}
impl<T> From<PoisonError<T>> for Error {
fn from(_: PoisonError<T>) -> Self {
Self::WorkerCrashed
}
}
impl From<TryLockError> for Error {
fn from(_: TryLockError) -> Self {
Self::WorkerCrashed
}
}