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 /// A delta node or frontmatter delta that cannot be resolved (spec §4.1
70 /// "Delta nodes", §6, 16.0): a `{`-form with no base revision, ops that
71 /// do not consume the base, a delta inside an archive segment. A corrupt
72 /// store — the read fails rather than guessing.
73 Delta(String),
74 /// Anything else, with a message.
75 Other(String),
76}
77
78impl fmt::Display for Error {
79 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80 match self {
81 Error::Sqlite(e) => write!(f, "sqlite: {e}"),
82 Error::SchemaTooNew { found, supported } => write!(
83 f,
84 "database schema (v{found}) is newer than this build (v{supported}); upgrade omgbase"
85 ),
86 Error::InvalidTimestamp(ts) => write!(f, "invalid RFC 3339 UTC timestamp {ts:?}"),
87 Error::Json(e) => write!(f, "json: {e}"),
88 Error::Search(e) => write!(f, "search: {e}"),
89 Error::Mutation(e) => write!(f, "mutation: {e}"),
90 Error::MintExhausted { prefix, rejected } => write!(
91 f,
92 "mint({prefix}): {rejected} consecutive candidates were already in use; the minter cannot produce a fresh id"
93 ),
94 Error::InvalidEffect(msg) => write!(f, "invalid effect: {msg}"),
95 // The reference's `RevisionArchived` messages, byte for byte
96 // (`spec/surface` §2: the CLI prints them).
97 Error::RevisionArchived {
98 rev,
99 archive,
100 path: None,
101 } => write!(
102 f,
103 "revision {} is archived in segment {archive}, which is not attached (omg history attach <file>)",
104 serde_json::Value::String(rev.clone())
105 ),
106 Error::RevisionArchived {
107 rev,
108 archive,
109 path: Some(path),
110 } => write!(
111 f,
112 "revision {} is archived in segment {archive} at {path}, which is not on disk",
113 serde_json::Value::String(rev.clone())
114 ),
115 Error::NotASegment { path, detail } => write!(f, "{path}: {detail}"),
116 Error::StaleSegment { path, rev } => write!(
117 f,
118 "{path}: an unrecorded segment naming revision {rev}, which is not a live revision of this store — move the file out of the way"
119 ),
120 Error::RetentionPolicy(msg) => f.write_str(msg),
121 Error::Delta(msg) => write!(f, "delta: {msg}"),
122 Error::Other(msg) => f.write_str(msg),
123 }
124 }
125}
126
127impl std::error::Error for Error {
128 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
129 match self {
130 Error::Sqlite(e) => Some(e),
131 Error::Json(e) => Some(e),
132 Error::Search(e) => Some(e),
133 Error::Mutation(e) => Some(e),
134 _ => None,
135 }
136 }
137}
138
139impl From<rusqlite::Error> for Error {
140 fn from(e: rusqlite::Error) -> Self {
141 Error::Sqlite(e)
142 }
143}
144
145impl From<serde_json::Error> for Error {
146 fn from(e: serde_json::Error) -> Self {
147 Error::Json(e)
148 }
149}
150
151impl From<omgbase_search::Error> for Error {
152 fn from(e: omgbase_search::Error) -> Self {
153 Error::Search(e)
154 }
155}
156
157impl From<omgbase_mutate::MutationError> for Error {
158 fn from(e: omgbase_mutate::MutationError) -> Self {
159 Error::Mutation(e)
160 }
161}
162
163impl Error {
164 /// The mutation error inside, if this is one.
165 #[must_use]
166 pub fn as_mutation(&self) -> Option<&omgbase_mutate::MutationError> {
167 match self {
168 Error::Mutation(e) => Some(e),
169 _ => None,
170 }
171 }
172}
173
174/// `Result` with this crate's [`Error`].
175pub type Result<T> = std::result::Result<T, Error>;