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    /// A read of an archived revision whose segment is not recorded, or
38    /// recorded but not on disk (spec §6.2 "Through a segment", 15.0; the
39    /// surface's `revision_archived { rev, archive, path? }`).
40    RevisionArchived {
41        /// The revision asked for.
42        rev: String,
43        /// The segment its `archive` column names.
44        archive: String,
45        /// The recorded file, when the segment is recorded (then it is the
46        /// file that is not on disk).
47        path: Option<String>,
48    },
49    /// A file that is not an archive segment, or one of another repository
50    /// (spec §7.1 "Attach / detach").
51    NotASegment {
52        /// The file.
53        path: String,
54        /// `no such file`, `no manifest row`, `not an archive segment`, or
55        /// `a segment of repository <x>, not <y>`.
56        detail: String,
57    },
58    /// An unrecorded segment under the archives directory names both live
59    /// and non-live revisions (spec §7.1 "The crash rule"): it predates
60    /// another squash or belongs to another store.
61    StaleSegment {
62        /// The file.
63        path: String,
64        /// The offending revision.
65        rev: String,
66    },
67    /// A malformed retention policy (spec §7.1 "The policy").
68    RetentionPolicy(String),
69    /// Anything else, with a message.
70    Other(String),
71}
72
73impl fmt::Display for Error {
74    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75        match self {
76            Error::Sqlite(e) => write!(f, "sqlite: {e}"),
77            Error::SchemaTooNew { found, supported } => write!(
78                f,
79                "database schema (v{found}) is newer than this build (v{supported}); upgrade omgbase"
80            ),
81            Error::InvalidTimestamp(ts) => write!(f, "invalid RFC 3339 UTC timestamp {ts:?}"),
82            Error::Json(e) => write!(f, "json: {e}"),
83            Error::Search(e) => write!(f, "search: {e}"),
84            Error::Mutation(e) => write!(f, "mutation: {e}"),
85            Error::MintExhausted { prefix, rejected } => write!(
86                f,
87                "mint({prefix}): {rejected} consecutive candidates were already in use; the minter cannot produce a fresh id"
88            ),
89            Error::InvalidEffect(msg) => write!(f, "invalid effect: {msg}"),
90            // The reference's `RevisionArchived` messages, byte for byte
91            // (`spec/surface` §2: the CLI prints them).
92            Error::RevisionArchived {
93                rev,
94                archive,
95                path: None,
96            } => write!(
97                f,
98                "revision {} is archived in segment {archive}, which is not attached (omg history attach <file>)",
99                serde_json::Value::String(rev.clone())
100            ),
101            Error::RevisionArchived {
102                rev,
103                archive,
104                path: Some(path),
105            } => write!(
106                f,
107                "revision {} is archived in segment {archive} at {path}, which is not on disk",
108                serde_json::Value::String(rev.clone())
109            ),
110            Error::NotASegment { path, detail } => write!(f, "{path}: {detail}"),
111            Error::StaleSegment { path, rev } => write!(
112                f,
113                "{path}: an unrecorded segment naming revision {rev}, which is not a live revision of this store — move the file out of the way"
114            ),
115            Error::RetentionPolicy(msg) => f.write_str(msg),
116            Error::Other(msg) => f.write_str(msg),
117        }
118    }
119}
120
121impl std::error::Error for Error {
122    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
123        match self {
124            Error::Sqlite(e) => Some(e),
125            Error::Json(e) => Some(e),
126            Error::Search(e) => Some(e),
127            Error::Mutation(e) => Some(e),
128            _ => None,
129        }
130    }
131}
132
133impl From<rusqlite::Error> for Error {
134    fn from(e: rusqlite::Error) -> Self {
135        Error::Sqlite(e)
136    }
137}
138
139impl From<serde_json::Error> for Error {
140    fn from(e: serde_json::Error) -> Self {
141        Error::Json(e)
142    }
143}
144
145impl From<omgbase_search::Error> for Error {
146    fn from(e: omgbase_search::Error) -> Self {
147        Error::Search(e)
148    }
149}
150
151impl From<omgbase_mutate::MutationError> for Error {
152    fn from(e: omgbase_mutate::MutationError) -> Self {
153        Error::Mutation(e)
154    }
155}
156
157impl Error {
158    /// The mutation error inside, if this is one.
159    #[must_use]
160    pub fn as_mutation(&self) -> Option<&omgbase_mutate::MutationError> {
161        match self {
162            Error::Mutation(e) => Some(e),
163            _ => None,
164        }
165    }
166}
167
168/// `Result` with this crate's [`Error`].
169pub type Result<T> = std::result::Result<T, Error>;