| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (c) 2026 UtopiaIA |
| 3 | |
| 4 | //! Why an engine operation failed. |
| 5 | |
| 6 | use braid_format::{ChangesetError, Kind, ObjectId, PathError, TreeError}; |
| 7 | |
| 8 | use crate::path::show; |
| 9 | |
| 10 | /// An error reported by an object store, boxed so that [`Error`] does not |
| 11 | /// depend on the store type. |
| 12 | pub type StoreError = Box<dyn std::error::Error + Send + Sync + 'static>; |
| 13 | |
| 14 | /// Why an engine operation failed. |
| 15 | /// |
| 16 | /// [`Error::Store`] is the only variant that says nothing about the data: the |
| 17 | /// store could not be read or written, and trying again may work. Every other |
| 18 | /// variant means that an object or an argument is not valid, and is how |
| 19 | /// [`admit`](crate::admit) explains a rejection. |
| 20 | #[derive(Debug, thiserror::Error)] |
| 21 | #[non_exhaustive] |
| 22 | pub enum Error { |
| 23 | /// The object store failed. |
| 24 | #[error("object store error: {0}")] |
| 25 | Store(#[source] StoreError), |
| 26 | |
| 27 | /// An object that must be present is not. |
| 28 | #[error("{kind} {id} is missing")] |
| 29 | Missing { |
| 30 | /// The missing object. |
| 31 | id: ObjectId, |
| 32 | /// What it was expected to be. |
| 33 | kind: Kind, |
| 34 | }, |
| 35 | |
| 36 | /// An object is stored as another kind than the one it is used as. |
| 37 | #[error("object {id} is a {found}, not a {expected}")] |
| 38 | WrongKind { |
| 39 | /// The object. |
| 40 | id: ObjectId, |
| 41 | /// The kind it is used as. |
| 42 | expected: Kind, |
| 43 | /// The kind it is stored as. |
| 44 | found: Kind, |
| 45 | }, |
| 46 | |
| 47 | /// The bytes stored under an id do not hash to that id. |
| 48 | #[error("object {id} does not match its id")] |
| 49 | Corrupt { |
| 50 | /// The object. |
| 51 | id: ObjectId, |
| 52 | }, |
| 53 | |
| 54 | /// A stored tree is not valid, or not the canonical split of its entries. |
| 55 | #[error("tree {id} is not valid: {error}")] |
| 56 | Tree { |
| 57 | /// The root of the directory. |
| 58 | id: ObjectId, |
| 59 | /// What is wrong with it. |
| 60 | #[source] |
| 61 | error: TreeError, |
| 62 | }, |
| 63 | |
| 64 | /// Entries do not form a valid directory. |
| 65 | #[error("cannot build a directory: {0}")] |
| 66 | Entries(#[source] TreeError), |
| 67 | |
| 68 | /// A stored changeset, or its change list, is not valid. |
| 69 | #[error("changeset {id} is not valid: {error}")] |
| 70 | Changeset { |
| 71 | /// The changeset. |
| 72 | id: ObjectId, |
| 73 | /// What is wrong with it. |
| 74 | #[source] |
| 75 | error: ChangesetError, |
| 76 | }, |
| 77 | |
| 78 | /// The parts of a new changeset do not form a valid one. |
| 79 | #[error("cannot assemble the changeset: {0}")] |
| 80 | Assemble(#[source] ChangesetError), |
| 81 | |
| 82 | /// An external change list does not hold as many changes as its changeset |
| 83 | /// declares. |
| 84 | #[error("changeset {changeset} declares {declared} changes, its list holds {found}")] |
| 85 | ChangeCount { |
| 86 | /// The changeset. |
| 87 | changeset: ObjectId, |
| 88 | /// The count in the changeset. |
| 89 | declared: u64, |
| 90 | /// The number of changes in the list. |
| 91 | found: usize, |
| 92 | }, |
| 93 | |
| 94 | /// A change list is stored in its own blob although its encoding is short |
| 95 | /// enough to be stored inline, which is not canonical. |
| 96 | #[error( |
| 97 | "changeset {changeset} keeps a change list of {len} bytes in a blob, short enough to be inline" |
| 98 | )] |
| 99 | ExternalListFitsInline { |
| 100 | /// The changeset. |
| 101 | changeset: ObjectId, |
| 102 | /// Length of the encoded list. |
| 103 | len: usize, |
| 104 | }, |
| 105 | |
| 106 | /// A change list does not apply to its base tree. |
| 107 | #[error("cannot apply the change to {:?}: {problem}", show(.path))] |
| 108 | Apply { |
| 109 | /// Where the problem is: the path of a change, or the prefix of one |
| 110 | /// that is in the way. |
| 111 | path: Vec<u8>, |
| 112 | /// What the problem is. |
| 113 | problem: ApplyProblem, |
| 114 | }, |
| 115 | |
| 116 | /// A parent of a changeset has not been admitted. |
| 117 | #[error("parent {parent} of changeset {changeset} has not been admitted")] |
| 118 | ParentNotAdmitted { |
| 119 | /// The changeset. |
| 120 | changeset: ObjectId, |
| 121 | /// The parent that is not admitted. |
| 122 | parent: ObjectId, |
| 123 | }, |
| 124 | |
| 125 | /// A changeset's change list, applied to its first parent, does not give |
| 126 | /// the root tree the changeset declares. |
| 127 | #[error("changeset {changeset} declares root {declared}, its changes give {computed}")] |
| 128 | RootMismatch { |
| 129 | /// The changeset. |
| 130 | changeset: ObjectId, |
| 131 | /// The root the changeset names. |
| 132 | declared: ObjectId, |
| 133 | /// The root its change list produces. |
| 134 | computed: ObjectId, |
| 135 | }, |
| 136 | |
| 137 | /// A blob does not have the size a change gives it. |
| 138 | #[error("blob {blob} holds {actual} bytes, not {declared}")] |
| 139 | BlobSize { |
| 140 | /// The blob. |
| 141 | blob: ObjectId, |
| 142 | /// The size in the change. |
| 143 | declared: u64, |
| 144 | /// The size of the stored blob. |
| 145 | actual: u64, |
| 146 | }, |
| 147 | |
| 148 | /// The base of a change is not on the first-parent chain of the branch |
| 149 | /// head it is landing on. |
| 150 | #[error("the base of the change ({}) is not on the branch (head {})", show_id(.base), show_id(.head))] |
| 151 | BaseNotOnBranch { |
| 152 | /// The change's first parent, if it has one. |
| 153 | base: Option<ObjectId>, |
| 154 | /// The branch head, if the branch has one. |
| 155 | head: Option<ObjectId>, |
| 156 | }, |
| 157 | |
| 158 | /// A path has more names than [`MAX_PATH_DEPTH`](crate::MAX_PATH_DEPTH), |
| 159 | /// the limit a store sets on how deep a tree may be. |
| 160 | #[error("{:?} has more than {} names", show(.path), crate::MAX_PATH_DEPTH)] |
| 161 | TooDeep { |
| 162 | /// The path of a change, or the first path deeper than the limit |
| 163 | /// that a walk over a tree met. |
| 164 | path: Vec<u8>, |
| 165 | }, |
| 166 | |
| 167 | /// Two changesets to merge have no common ancestor. |
| 168 | #[error("changesets {} and {} have no common ancestor", .ours.short(), .theirs.short())] |
| 169 | UnrelatedHistories { |
| 170 | /// The changeset merged into. |
| 171 | ours: ObjectId, |
| 172 | /// The changeset merged. |
| 173 | theirs: ObjectId, |
| 174 | }, |
| 175 | |
| 176 | /// Derived data about a changeset contradicts the history it was derived |
| 177 | /// from: a record made by a faulty version, or tampered with. |
| 178 | #[error("the derived data of {id} contradicts its history")] |
| 179 | DerivedData { |
| 180 | /// The changeset. |
| 181 | id: ObjectId, |
| 182 | }, |
| 183 | |
| 184 | /// A walk would list more objects than its caller allows (see |
| 185 | /// [`Reach::max_objects`](crate::Reach::max_objects)). |
| 186 | #[error("the walk reaches more than {limit} objects")] |
| 187 | TooManyObjects { |
| 188 | /// The most objects the walk could list. |
| 189 | limit: usize, |
| 190 | }, |
| 191 | |
| 192 | /// Two changesets to merge have several best common ancestors, as after |
| 193 | /// criss-cross merges, with different trees: the merge would depend on |
| 194 | /// which one it started from. |
| 195 | #[error( |
| 196 | "changesets {} and {} have {} best common ancestors with different trees", |
| 197 | .ours.short(), |
| 198 | .theirs.short(), |
| 199 | .bases.len() |
| 200 | )] |
| 201 | SeveralMergeBases { |
| 202 | /// The changeset merged into. |
| 203 | ours: ObjectId, |
| 204 | /// The changeset merged. |
| 205 | theirs: ObjectId, |
| 206 | /// The best common ancestors, sorted by id. |
| 207 | bases: Vec<ObjectId>, |
| 208 | }, |
| 209 | } |
| 210 | |
| 211 | impl Error { |
| 212 | /// Whether the store failed, as opposed to the data being invalid. |
| 213 | pub fn is_store_failure(&self) -> bool { |
| 214 | matches!(self, Error::Store(_)) |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /// Wraps a store error. |
| 219 | pub(crate) fn store_error<E: std::error::Error + Send + Sync + 'static>(error: E) -> Error { |
| 220 | Error::Store(Box::new(error)) |
| 221 | } |
| 222 | |
| 223 | fn show_id(id: &Option<ObjectId>) -> String { |
| 224 | id.map_or_else(|| "none".to_owned(), |id| id.short()) |
| 225 | } |
| 226 | |
| 227 | /// Why a change list does not apply to a tree. |
| 228 | #[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)] |
| 229 | #[non_exhaustive] |
| 230 | pub enum ApplyProblem { |
| 231 | /// The path is not valid. |
| 232 | #[error("the path is not valid: {0}")] |
| 233 | InvalidPath(PathError), |
| 234 | /// A symlink takes a name that git or braid reads. |
| 235 | #[error("this name must not be a symlink")] |
| 236 | SymlinkName, |
| 237 | /// More than one change names the path. |
| 238 | #[error("more than one change names this path")] |
| 239 | Duplicate, |
| 240 | /// A change below this path needs it to be a directory, and it is a file, |
| 241 | /// a symlink or a gitlink that the change list does not delete. |
| 242 | #[error("a change below this path needs a directory, and it is not one")] |
| 243 | NotADirectory, |
| 244 | /// A change sets this path, and a directory with entries remains there. |
| 245 | #[error("a file is set where a directory with entries remains")] |
| 246 | DirectoryNotEmpty, |
| 247 | /// A change deletes this path, which is a directory: changes are per |
| 248 | /// file, so a directory goes away when its files do. |
| 249 | #[error("the path is a directory; delete its files instead")] |
| 250 | IsADirectory, |
| 251 | /// A change deletes this path, which does not exist. |
| 252 | #[error("the path to delete does not exist")] |
| 253 | DeleteMissing, |
| 254 | /// A change sets this path to the entry it already has. |
| 255 | #[error("the path already has this entry")] |
| 256 | NoOp, |
| 257 | } |