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    /// An effect a plan cannot read (`spec/mutate` §11): a malformed shape,
34    /// a field no target kind has, a value of the wrong type. The surface
35    /// reports it as `filter_invalid`.
36    InvalidEffect(String),
37    /// Anything else, with a message.
38    Other(String),
39}
40
41impl fmt::Display for Error {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        match self {
44            Error::Sqlite(e) => write!(f, "sqlite: {e}"),
45            Error::SchemaTooNew { found, supported } => write!(
46                f,
47                "database schema (v{found}) is newer than this build (v{supported}); upgrade omgbase"
48            ),
49            Error::InvalidTimestamp(ts) => write!(f, "invalid RFC 3339 UTC timestamp {ts:?}"),
50            Error::Json(e) => write!(f, "json: {e}"),
51            Error::Search(e) => write!(f, "search: {e}"),
52            Error::Mutation(e) => write!(f, "mutation: {e}"),
53            Error::MintExhausted { prefix, rejected } => write!(
54                f,
55                "mint({prefix}): {rejected} consecutive candidates were already in use; the minter cannot produce a fresh id"
56            ),
57            Error::InvalidEffect(msg) => write!(f, "invalid effect: {msg}"),
58            Error::Other(msg) => f.write_str(msg),
59        }
60    }
61}
62
63impl std::error::Error for Error {
64    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
65        match self {
66            Error::Sqlite(e) => Some(e),
67            Error::Json(e) => Some(e),
68            Error::Search(e) => Some(e),
69            Error::Mutation(e) => Some(e),
70            _ => None,
71        }
72    }
73}
74
75impl From<rusqlite::Error> for Error {
76    fn from(e: rusqlite::Error) -> Self {
77        Error::Sqlite(e)
78    }
79}
80
81impl From<serde_json::Error> for Error {
82    fn from(e: serde_json::Error) -> Self {
83        Error::Json(e)
84    }
85}
86
87impl From<omgbase_search::Error> for Error {
88    fn from(e: omgbase_search::Error) -> Self {
89        Error::Search(e)
90    }
91}
92
93impl From<omgbase_mutate::MutationError> for Error {
94    fn from(e: omgbase_mutate::MutationError) -> Self {
95        Error::Mutation(e)
96    }
97}
98
99impl Error {
100    /// The mutation error inside, if this is one.
101    #[must_use]
102    pub fn as_mutation(&self) -> Option<&omgbase_mutate::MutationError> {
103        match self {
104            Error::Mutation(e) => Some(e),
105            _ => None,
106        }
107    }
108}
109
110/// `Result` with this crate's [`Error`].
111pub type Result<T> = std::result::Result<T, Error>;