Skip to main content

async_sqlite/
error.rs

1/// Enum of all possible errors.
2#[derive(Debug)]
3#[non_exhaustive]
4pub enum Error {
5    /// Indicates that the connection to the sqlite database is closed.
6    Closed,
7    /// Indicates that the sqlite worker queue is full.
8    QueueFull,
9    /// Invalid builder configuration.
10    Config { message: &'static str },
11    /// Error updating PRAGMA.
12    PragmaUpdate {
13        name: &'static str,
14        exp: &'static str,
15        got: String,
16    },
17    /// Represents a [`rusqlite::Error`].
18    Rusqlite(rusqlite::Error),
19    /// A user-provided closure panicked while being executed on the worker thread.
20    Panic { message: String },
21}
22
23impl std::error::Error for Error {
24    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
25        match self {
26            Error::Rusqlite(err) => Some(err),
27            _ => None,
28        }
29    }
30}
31
32impl std::fmt::Display for Error {
33    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34        match self {
35            Error::Closed => write!(f, "connection to sqlite database closed"),
36            Error::QueueFull => write!(f, "sqlite worker queue is full"),
37            Error::Config { message } => write!(f, "invalid configuration: {message}"),
38            Error::PragmaUpdate { exp, got, name } => {
39                write!(f, "updating pragma {name}: expected '{exp}', got '{got}'")
40            }
41            Error::Rusqlite(err) => err.fmt(f),
42            Error::Panic { message } => write!(f, "user closure panicked: {message}"),
43        }
44    }
45}
46
47impl From<rusqlite::Error> for Error {
48    fn from(value: rusqlite::Error) -> Self {
49        Error::Rusqlite(value)
50    }
51}
52
53impl<T> From<crossbeam_channel::SendError<T>> for Error {
54    fn from(_value: crossbeam_channel::SendError<T>) -> Self {
55        Error::Closed
56    }
57}
58
59impl<T> From<crossbeam_channel::TrySendError<T>> for Error {
60    fn from(value: crossbeam_channel::TrySendError<T>) -> Self {
61        match value {
62            crossbeam_channel::TrySendError::Full(_) => Error::QueueFull,
63            crossbeam_channel::TrySendError::Disconnected(_) => Error::Closed,
64        }
65    }
66}
67
68impl From<crossbeam_channel::RecvError> for Error {
69    fn from(_value: crossbeam_channel::RecvError) -> Self {
70        Error::Closed
71    }
72}
73
74impl From<futures_channel::oneshot::Canceled> for Error {
75    fn from(_value: futures_channel::oneshot::Canceled) -> Self {
76        Error::Closed
77    }
78}