1use 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#[derive(Debug, thiserror::Error)]
13pub enum FileReadError {
14 #[error("an update is required to read this file")]
16 UpdateRequired(crate::RequiredUpdate),
17 #[error("no storage connected")]
19 NoStorage,
20 #[error("file {id}, uploaded by device {device:?}, is missing from storage")]
23 Missing {
24 id: String,
26 device: DeviceId,
28 },
29 #[error("file {file} is only on device {device:?}")]
36 DeviceOnly {
37 file: FileId,
39 device: DeviceId,
41 },
42 #[error("file {id} is missing at {}", path.display())]
44 UserFileMissing {
45 id: String,
47 path: PathBuf,
49 },
50 #[error("file {id} changed at {}", path.display())]
52 UserFileChanged {
53 id: String,
55 path: PathBuf,
57 },
58 #[error("file {id} failed its content check")]
60 Integrity {
61 id: String,
63 #[source]
66 cause: Option<std::sync::Arc<coven_crypto::CryptoError>>,
67 },
68 #[error("file {id} range {offset}..{end} exceeds {size}")]
70 RangeOutOfBounds {
71 id: String,
73 offset: u64,
75 end: u64,
77 size: u64,
79 },
80 #[error(transparent)]
82 Database(DbError),
83 #[error(transparent)]
85 Disk(FileError),
86 #[error(transparent)]
88 Lock(StoreLockError),
89 #[error(transparent)]
91 Storage(StorageError),
92 #[error(transparent)]
96 Sync(Box<crate::SyncError>),
97 #[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
147impl 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 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#[derive(Debug, thiserror::Error)]
203pub(crate) enum TransferError {
204 #[error("file transfer was refused: {0:?}")]
206 Refused(crate::Refusal),
207 #[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;