Skip to main content

coven_sync/
file_error.rs

1//! Typed file read and upload failures shared with the application facade.
2
3use coven_database::{DbError, LocalFileError};
4use coven_foundation::{
5    files::{FileError, StoreLockError},
6    id_source::{DeviceId, FileId},
7};
8use coven_storage::StorageError;
9use std::path::PathBuf;
10
11/// Reading a file failed with a cause the app can distinguish (E8).
12#[derive(Debug, thiserror::Error)]
13pub enum FileReadError {
14    /// This file uses a newer coven format (Appendix C.2).
15    #[error("an update is required to read this file")]
16    UpdateRequired(crate::RequiredUpdate),
17    /// No storage capability is connected.
18    #[error("no storage connected")]
19    NoStorage,
20    /// Storage confirms nothing at the path a stored file's record names:
21    /// the provider lost it (§17.6). Nothing brings it back on its own.
22    #[error("file {id}, uploaded by device {device:?}, is missing from storage")]
23    Missing {
24        /// The file's id.
25        id: String,
26        /// The device that uploaded it.
27        device: DeviceId,
28    },
29    /// The file is only on one device, and nothing waits for it (§17.2). A
30    /// read, a device-only call's download among them, returns it only for
31    /// a file on another device: this device keeps the bytes of every file
32    /// whose record keeps it here (§17.7, §17.11). A pin returns it for a
33    /// file on any one device, this one included while it uploads, since
34    /// only stored bytes are kept (§17.8).
35    #[error("file {file} is only on device {device:?}")]
36    DeviceOnly {
37        /// The file.
38        file: FileId,
39        /// The device that has it.
40        device: DeviceId,
41    },
42    /// A recorded original is absent.
43    #[error("file {id} is missing at {}", path.display())]
44    UserFileMissing {
45        /// The file's id.
46        id: String,
47        /// Recorded original path.
48        path: PathBuf,
49    },
50    /// An original changed after it was staged.
51    #[error("file {id} changed at {}", path.display())]
52    UserFileChanged {
53        /// The file's id.
54        id: String,
55        /// Recorded original path.
56        path: PathBuf,
57    },
58    /// Authentication, a chunk hash or the whole-file hash failed.
59    #[error("file {id} failed its content check")]
60    Integrity {
61        /// The file's id.
62        id: String,
63        /// The authentication failure, when a chunk failed to open. A size,
64        /// layout or hash disagreement has no cause beyond the comparison.
65        #[source]
66        cause: Option<std::sync::Arc<coven_crypto::CryptoError>>,
67    },
68    /// The requested range is outside the file.
69    #[error("file {id} range {offset}..{end} exceeds {size}")]
70    RangeOutOfBounds {
71        /// The file's id.
72        id: String,
73        /// Requested start.
74        offset: u64,
75        /// Requested end, saturated on overflow.
76        end: u64,
77        /// Whole plaintext size.
78        size: u64,
79    },
80    /// A database failure, retaining its cause.
81    #[error(transparent)]
82    Database(DbError),
83    /// A disk failure, retaining its cause.
84    #[error(transparent)]
85    Disk(FileError),
86    /// The store cannot be retained while opening its file.
87    #[error(transparent)]
88    Lock(StoreLockError),
89    /// A provider failure, retaining its classification and native cause.
90    #[error(transparent)]
91    Storage(StorageError),
92    /// Recording what a read or an upload settled failed in sync's own
93    /// work, as a retry wait the clock can't hold, keeping its cause; a
94    /// failed read whose failure couldn't be recorded keeps both (§17.9).
95    #[error(transparent)]
96    Sync(Box<crate::SyncError>),
97    /// The read's work panicked: a bug, which the caller reports (E15).
98    #[error(transparent)]
99    Bug(#[from] coven_foundation::task::Bug),
100}
101impl From<FileError> for FileReadError {
102    fn from(error: FileError) -> Self {
103        match error {
104            FileError::Bug(bug) => Self::Bug(bug),
105            error => Self::Disk(error),
106        }
107    }
108}
109impl From<StoreLockError> for FileReadError {
110    fn from(error: StoreLockError) -> Self {
111        match error {
112            StoreLockError::Bug(bug) => Self::Bug(bug),
113            error => Self::Lock(error),
114        }
115    }
116}
117impl From<StorageError> for FileReadError {
118    fn from(error: StorageError) -> Self {
119        match error {
120            StorageError::Bug(bug) => Self::Bug(bug),
121            error => Self::Storage(error),
122        }
123    }
124}
125impl coven_foundation::task::CarriesBug for FileReadError {
126    fn bug(&self) -> Option<&coven_foundation::task::Bug> {
127        match self {
128            Self::Bug(bug) => Some(bug),
129            Self::Database(error) => error.bug(),
130            Self::Disk(error) => error.bug(),
131            Self::Lock(error) => error.bug(),
132            Self::Storage(error) => error.bug(),
133            Self::Sync(error) => error.bug(),
134            _ => None,
135        }
136    }
137}
138impl From<coven_foundation::files::WriterError> for FileReadError {
139    fn from(error: coven_foundation::files::WriterError) -> Self {
140        match error {
141            coven_foundation::files::WriterError::Lock(error) => error.into(),
142            coven_foundation::files::WriterError::File(error) => error.into(),
143        }
144    }
145}
146
147/// A database or provider failure as itself; any other of sync's own
148/// failures kept whole.
149impl From<crate::SyncError> for FileReadError {
150    fn from(error: crate::SyncError) -> Self {
151        match error {
152            crate::SyncError::Database(error) => error.into(),
153            crate::SyncError::Storage(error) => error.into(),
154            crate::SyncError::Bug(bug) => Self::Bug(bug),
155            error => Self::Sync(Box::new(error)),
156        }
157    }
158}
159impl From<DbError> for FileReadError {
160    fn from(error: DbError) -> Self {
161        match error {
162            DbError::Disk(error) => Self::Disk(error),
163            DbError::Lock(error) => Self::Lock(error),
164            DbError::Bug(bug) => Self::Bug(bug),
165            error => Self::Database(error),
166        }
167    }
168}
169impl FileReadError {
170    /// A chunk of file `id` that did not seal or open. An authentication
171    /// failure keeps its crypto error. A chunk length outside the header's
172    /// layout is the layout's disagreement, which the comparison states.
173    pub(crate) fn chunk(id: String, error: coven_format::file::FileError) -> Self {
174        Self::Integrity {
175            id,
176            cause: match error {
177                coven_format::file::FileError::Crypto(error) => Some(std::sync::Arc::new(error)),
178                coven_format::file::FileError::Format(_) => None,
179            },
180        }
181    }
182}
183impl From<LocalFileError> for FileReadError {
184    fn from(error: LocalFileError) -> Self {
185        match error {
186            LocalFileError::UserFileMissing { id, path } => Self::UserFileMissing { id, path },
187            LocalFileError::UserFileChanged { id, path } => Self::UserFileChanged { id, path },
188            LocalFileError::Integrity { id } => Self::Integrity { id, cause: None },
189            LocalFileError::RangeOutOfBounds { id, offset, end, size } => {
190                Self::RangeOutOfBounds { id, offset, end, size }
191            }
192            LocalFileError::Database(error) => error.into(),
193            LocalFileError::Disk(error) => Self::Disk(error),
194            LocalFileError::Lock(error) => Self::Lock(error),
195            LocalFileError::Bug(bug) => Self::Bug(bug),
196        }
197    }
198}
199
200/// A file transfer retains its typed cause and exact refusal. The pending
201/// record keeps the category before the app-facing error merges content checks.
202#[derive(Debug, thiserror::Error)]
203pub(crate) enum TransferError {
204    /// Content or immutable identity verification failed.
205    #[error("file transfer was refused: {0:?}")]
206    Refused(crate::Refusal),
207    /// Reading, storing or checking bytes failed.
208    #[error(transparent)]
209    File(#[from] FileReadError),
210}
211
212impl coven_foundation::task::CarriesBug for TransferError {
213    fn bug(&self) -> Option<&coven_foundation::task::Bug> {
214        match self {
215            Self::Refused(_) => None,
216            Self::File(error) => error.bug(),
217        }
218    }
219}
220
221impl From<StorageError> for TransferError {
222    fn from(error: StorageError) -> Self {
223        Self::File(error.into())
224    }
225}
226impl From<DbError> for TransferError {
227    fn from(error: DbError) -> Self {
228        Self::File(error.into())
229    }
230}
231impl From<FileError> for TransferError {
232    fn from(error: FileError) -> Self {
233        Self::File(error.into())
234    }
235}
236impl From<LocalFileError> for TransferError {
237    fn from(error: LocalFileError) -> Self {
238        Self::File(error.into())
239    }
240}
241
242#[cfg(test)]
243#[path = "file_error_tests.rs"]
244mod tests;