| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (c) 2026 UtopiaIA |
| 3 | |
| 4 | //! Errors returned by the store. |
| 5 | |
| 6 | use std::io; |
| 7 | use std::path::{Path, PathBuf}; |
| 8 | |
| 9 | use braid_format::ObjectId; |
| 10 | |
| 11 | /// Why a store operation failed. |
| 12 | #[derive(Debug, thiserror::Error)] |
| 13 | #[non_exhaustive] |
| 14 | pub enum Error { |
| 15 | /// A file system call failed. |
| 16 | #[error("cannot {action} {}: {source}", .path.display())] |
| 17 | Io { |
| 18 | /// What was being done, as a verb: "open", "read", "sync". |
| 19 | action: &'static str, |
| 20 | /// The file or directory involved. |
| 21 | path: PathBuf, |
| 22 | /// The error from the operating system. |
| 23 | #[source] |
| 24 | source: io::Error, |
| 25 | }, |
| 26 | |
| 27 | /// The directory has no `format` file, so it is not a store. |
| 28 | #[error("{} is not a braid store", .path.display())] |
| 29 | NotAStore { |
| 30 | /// The directory that was opened. |
| 31 | path: PathBuf, |
| 32 | }, |
| 33 | |
| 34 | /// The `format` file names a format this version does not understand. |
| 35 | #[error("unsupported store format {found:?}")] |
| 36 | UnsupportedFormat { |
| 37 | /// The contents of the `format` file. |
| 38 | found: String, |
| 39 | }, |
| 40 | |
| 41 | /// The store was written by a newer version of braid: its `format` file |
| 42 | /// names a storage version this one does not know, or a journal or an |
| 43 | /// index holds an entry it cannot read. Nothing is read from such a |
| 44 | /// store, and nothing is written over what it holds. |
| 45 | #[error("{} needs a newer version of braid: {detail}", .path.display())] |
| 46 | NewerStore { |
| 47 | /// The file that says so. |
| 48 | path: PathBuf, |
| 49 | /// What this version does not know. |
| 50 | detail: String, |
| 51 | }, |
| 52 | |
| 53 | /// Stored data failed a check: a checksum, a length, a sequence, an id. |
| 54 | /// The store never returns data that failed a check. |
| 55 | #[error("corrupt data in {} at byte {offset}: {detail}", .path.display())] |
| 56 | Corrupt { |
| 57 | /// The damaged file. |
| 58 | path: PathBuf, |
| 59 | /// Where in the file the damage was found. |
| 60 | offset: u64, |
| 61 | /// What failed. |
| 62 | detail: String, |
| 63 | }, |
| 64 | |
| 65 | /// An object whose stored form does not fit in one record. |
| 66 | #[error("an object of {size} bytes does not fit in one record")] |
| 67 | ObjectTooLarge { |
| 68 | /// The size of the object's payload. |
| 69 | size: u64, |
| 70 | }, |
| 71 | |
| 72 | /// Reading the content to store, or writing an object out, failed. |
| 73 | #[error("cannot {action} the stream: {source}")] |
| 74 | Stream { |
| 75 | /// What was being done: "read" or "write". |
| 76 | action: &'static str, |
| 77 | /// The error from the reader or the writer. |
| 78 | #[source] |
| 79 | source: io::Error, |
| 80 | }, |
| 81 | |
| 82 | /// A ref name the op log does not accept. |
| 83 | #[error("invalid ref name {name:?}: {reason}")] |
| 84 | InvalidRefName { |
| 85 | /// The rejected name. |
| 86 | name: String, |
| 87 | /// Why it was rejected. |
| 88 | reason: &'static str, |
| 89 | }, |
| 90 | |
| 91 | /// An agent label, a description or a whole operation over its size limit. |
| 92 | #[error("{what} is {len} bytes long, the limit is {max}")] |
| 93 | TooLong { |
| 94 | /// What was too long. |
| 95 | what: &'static str, |
| 96 | /// Its length in bytes. |
| 97 | len: usize, |
| 98 | /// The limit in bytes. |
| 99 | max: usize, |
| 100 | }, |
| 101 | |
| 102 | /// The same ref appears twice in one transaction. |
| 103 | #[error("ref {name:?} is updated twice in one transaction")] |
| 104 | DuplicateRef { |
| 105 | /// The repeated name. |
| 106 | name: String, |
| 107 | }, |
| 108 | |
| 109 | /// A ref did not hold the value an update expected, so the transaction |
| 110 | /// was not recorded. Read the refs again and retry. |
| 111 | #[error("ref {name:?} is {}, expected {}", show(.found), show(.expected))] |
| 112 | Conflict { |
| 113 | /// The ref that moved. |
| 114 | name: String, |
| 115 | /// The value the update expected. |
| 116 | expected: Option<ObjectId>, |
| 117 | /// The value the ref actually had. |
| 118 | found: Option<ObjectId>, |
| 119 | }, |
| 120 | |
| 121 | /// An update would point a ref at an object the store does not have. |
| 122 | #[error("ref {name:?} cannot point at {id}: the object is not in the store")] |
| 123 | MissingObject { |
| 124 | /// The ref being updated. |
| 125 | name: String, |
| 126 | /// The object it would point at. |
| 127 | id: ObjectId, |
| 128 | }, |
| 129 | |
| 130 | /// `undo_last` found no operation left to undo. |
| 131 | #[error("no operation left to undo")] |
| 132 | NothingToUndo, |
| 133 | |
| 134 | /// The op log was used from inside a transaction's closure, which would |
| 135 | /// wait for the lock the transaction itself holds. |
| 136 | #[error("the op log cannot be used from inside a transaction")] |
| 137 | Reentrant, |
| 138 | } |
| 139 | |
| 140 | impl Error { |
| 141 | /// Whether this is a [`Error::Conflict`]: the refs moved, and a retry with |
| 142 | /// fresh values may succeed. |
| 143 | pub fn is_conflict(&self) -> bool { |
| 144 | matches!(self, Error::Conflict { .. }) |
| 145 | } |
| 146 | |
| 147 | pub(crate) fn corrupt(path: &Path, offset: u64, detail: impl Into<String>) -> Self { |
| 148 | Error::Corrupt { |
| 149 | path: path.to_path_buf(), |
| 150 | offset, |
| 151 | detail: detail.into(), |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /// Builds the error for a failed file system call on `path`. |
| 157 | pub(crate) fn io_error<'a>( |
| 158 | action: &'static str, |
| 159 | path: &'a Path, |
| 160 | ) -> impl FnOnce(io::Error) -> Error + 'a { |
| 161 | move |source| Error::Io { |
| 162 | action, |
| 163 | path: path.to_path_buf(), |
| 164 | source, |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | fn show(id: &Option<ObjectId>) -> String { |
| 169 | match id { |
| 170 | Some(id) => id.to_hex(), |
| 171 | None => "absent".to_owned(), |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /// Why a transaction did not record an operation. |
| 176 | #[derive(Debug, thiserror::Error)] |
| 177 | pub enum TransactError<E> { |
| 178 | /// The closure returned this error. |
| 179 | #[error("transaction aborted: {0}")] |
| 180 | Aborted(E), |
| 181 | /// The store refused the updates or failed: a conflict, a missing object, |
| 182 | /// an I/O error. |
| 183 | #[error(transparent)] |
| 184 | Store(#[from] Error), |
| 185 | } |
| 186 | |
| 187 | impl<E> TransactError<E> { |
| 188 | /// Whether the transaction failed because a ref moved. Retrying with fresh |
| 189 | /// values may succeed. |
| 190 | pub fn is_conflict(&self) -> bool { |
| 191 | matches!(self, TransactError::Store(err) if err.is_conflict()) |
| 192 | } |
| 193 | } |