Skip to main content

omgbase_store/
error.rs

1//! The crate's error type.
2
3use std::fmt;
4
5/// Everything that can go wrong opening or writing a store.
6#[derive(Debug)]
7pub enum Error {
8    /// SQLite reported an error.
9    Sqlite(rusqlite::Error),
10    /// The database's `user_version` is newer than this build (spec §1 step 3).
11    SchemaTooNew {
12        /// The database's `user_version`.
13        found: i64,
14        /// [`crate::SCHEMA_VERSION`].
15        supported: i64,
16    },
17    /// A timestamp is not RFC 3339 UTC as spec §2.4 requires.
18    InvalidTimestamp(String),
19    /// A JSON column did not parse (tree entries, attrs, detail).
20    Json(serde_json::Error),
21    /// The embedding provider failed (`spec/search` §5).
22    Search(omgbase_search::Error),
23    /// A mutation was refused (`spec/mutate` §8): the code and its data.
24    Mutation(omgbase_mutate::MutationError),
25    /// A mint was rejected [`crate::MINT_GIVE_UP_AFTER`] times in a row
26    /// (spec §2.1): the minter cannot produce an id that is not in use.
27    MintExhausted {
28        /// The prefix being minted.
29        prefix: String,
30        /// How many consecutive candidates were in use.
31        rejected: usize,
32    },
33    /// Anything else, with a message.
34    Other(String),
35}
36
37impl fmt::Display for Error {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        match self {
40            Error::Sqlite(e) => write!(f, "sqlite: {e}"),
41            Error::SchemaTooNew { found, supported } => write!(
42                f,
43                "database schema (v{found}) is newer than this build (v{supported}); upgrade omgbase"
44            ),
45            Error::InvalidTimestamp(ts) => write!(f, "invalid RFC 3339 UTC timestamp {ts:?}"),
46            Error::Json(e) => write!(f, "json: {e}"),
47            Error::Search(e) => write!(f, "search: {e}"),
48            Error::Mutation(e) => write!(f, "mutation: {e}"),
49            Error::MintExhausted { prefix, rejected } => write!(
50                f,
51                "mint({prefix}): {rejected} consecutive candidates were already in use; the minter cannot produce a fresh id"
52            ),
53            Error::Other(msg) => f.write_str(msg),
54        }
55    }
56}
57
58impl std::error::Error for Error {
59    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
60        match self {
61            Error::Sqlite(e) => Some(e),
62            Error::Json(e) => Some(e),
63            Error::Search(e) => Some(e),
64            Error::Mutation(e) => Some(e),
65            _ => None,
66        }
67    }
68}
69
70impl From<rusqlite::Error> for Error {
71    fn from(e: rusqlite::Error) -> Self {
72        Error::Sqlite(e)
73    }
74}
75
76impl From<serde_json::Error> for Error {
77    fn from(e: serde_json::Error) -> Self {
78        Error::Json(e)
79    }
80}
81
82impl From<omgbase_search::Error> for Error {
83    fn from(e: omgbase_search::Error) -> Self {
84        Error::Search(e)
85    }
86}
87
88impl From<omgbase_mutate::MutationError> for Error {
89    fn from(e: omgbase_mutate::MutationError) -> Self {
90        Error::Mutation(e)
91    }
92}
93
94impl Error {
95    /// The mutation error inside, if this is one.
96    #[must_use]
97    pub fn as_mutation(&self) -> Option<&omgbase_mutate::MutationError> {
98        match self {
99            Error::Mutation(e) => Some(e),
100            _ => None,
101        }
102    }
103}
104
105/// `Result` with this crate's [`Error`].
106pub type Result<T> = std::result::Result<T, Error>;