1use crate::OperationRejection;
4use coven_crypto::{custody::KeyError, CryptoError, MaterialError};
5use coven_database::DbError;
6use coven_foundation::files::{FileError, ObservationError, StoreLockError};
7use coven_foundation::id_source::KeyId;
8use coven_merge::Audience;
9use coven_storage::{StorageError, StorageFailure};
10use std::sync::Arc;
11
12#[derive(Clone, Copy, Debug, PartialEq, Eq)]
15pub enum CutOffRefusal {
16 DeviceNotGone,
20 NotStoreOwner,
24 NoAccountShared,
27}
28
29#[derive(Debug, thiserror::Error)]
31pub enum SyncError {
32 #[error("pending: {0:?}")]
34 Pending(Box<crate::PendingRecord>),
35 #[error("{failure:?}: {source}")]
38 Failed {
39 failure: crate::LocalFailure,
41 #[source]
43 source: Arc<dyn std::error::Error + Send + Sync>,
44 },
45 #[error("{operation}; then: {cleanup}")]
48 Cleanup {
49 operation: Box<SyncError>,
51 #[source]
53 cleanup: Box<SyncError>,
54 },
55 #[error("storage is not connected")]
57 NoStorage,
58 #[error("the store owner cannot be removed or leave the account that holds the store")]
63 StoreOwner,
64 #[error("invitation or join request is no longer current")]
66 InvitationChanged,
67 #[error("operation {0:?} is not pending")]
69 NotPending(crate::OperationId),
70 #[error("snapshot reload {0:?} must finish before publishing entries or snapshots")]
72 ReloadPending(crate::OperationId),
73 #[error("snapshot {0:?} must finish before stored files are deleted")]
76 SnapshotPending(crate::OperationId),
77 #[error("invalid operation data: {0}")]
79 OperationData(#[from] serde_json::Error),
80 #[error("the store sequence is exhausted")]
83 SequenceExhausted,
84 #[error(transparent)]
86 Storage(StorageError),
87 #[error(transparent)]
89 Database(DbError),
90 #[error(transparent)]
92 SecureStorage(KeyError),
93 #[error(transparent)]
95 Crypto(CryptoError),
96 #[error(transparent)]
98 Format(coven_format::Error),
99 #[error(transparent)]
101 Key(#[from] MaterialError),
102 #[error("key {key} is not available yet")]
104 KeyUnavailable {
105 audience: Audience,
107 key: KeyId,
109 },
110 #[error("administrative change rejected: {0:?}")]
112 Rejected(OperationRejection),
113 #[error("the device cut-off was refused: {0:?}")]
115 CutOffRefused(CutOffRefusal),
116 #[error(transparent)]
118 Damaged(#[from] crate::DamagedObject),
119 #[error("this device is not in the store: {0:?}")]
124 Removed(coven_database::Removal),
125 #[error("another copy uses this device's id: {0:?}")]
129 DeviceIdInUse(crate::DeviceIdEvidence),
130 #[error("member {member:?} has newer keys; held credentials cannot send")]
134 MemberKeysReplaced {
135 member: coven_foundation::id_source::MemberId,
137 },
138 #[error("an update is required: {0:?}")]
140 UpdateRequired(crate::RequiredUpdate),
141 #[error(transparent)]
143 Code(#[from] crate::CodeError),
144 #[error(transparent)]
149 Shutdown(#[from] coven_foundation::task::Shutdown),
150 #[error(transparent)]
153 Bug(#[from] coven_foundation::task::Bug),
154 #[error("{0}; it has stopped")]
159 WorkerFailed(coven_foundation::task::Bug),
160}
161
162impl From<CutOffRefusal> for SyncError {
163 fn from(refusal: CutOffRefusal) -> Self {
164 Self::CutOffRefused(refusal)
165 }
166}
167
168impl coven_foundation::task::CarriesBug for SyncError {
169 fn bug(&self) -> Option<&coven_foundation::task::Bug> {
170 match self {
171 Self::Bug(bug) => Some(bug),
172 Self::WorkerFailed(_) => None,
174 Self::Database(error) => error.bug(),
175 Self::SecureStorage(error) => error.bug(),
176 Self::Storage(error) => error.bug(),
177 Self::Cleanup { operation, cleanup } => operation.bug().or_else(|| cleanup.bug()),
178 Self::Failed { source, .. } => {
180 source.downcast_ref::<Self>().and_then(coven_foundation::task::CarriesBug::bug)
181 }
182 _ => None,
183 }
184 }
185}
186
187impl From<coven_foundation::task::TaskFailed> for SyncError {
188 fn from(failed: coven_foundation::task::TaskFailed) -> Self {
189 failed.into_error()
190 }
191}
192
193impl From<coven_database::SqliteError> for SyncError {
194 fn from(error: coven_database::SqliteError) -> Self {
195 DbError::from(error).into()
196 }
197}
198
199impl coven_database::WriteFailure for SyncError {
203 fn with_rollback(self, rollback: coven_database::SqliteError) -> Self {
204 match self {
205 Self::Database(error) => Self::Database(error.with_rollback(rollback)),
206 error => Self::cleanup(error, rollback.into()),
207 }
208 }
209
210 fn with_cleanup(write: Self, failures: Vec<DbError>) -> Self {
211 match write {
212 Self::Database(error) => Self::Database(DbError::with_cleanup(error, failures)),
213 error => failures
214 .into_iter()
215 .fold(error, |error, failure| Self::cleanup(error, failure.into())),
216 }
217 }
218}
219
220impl From<DbError> for SyncError {
221 fn from(error: DbError) -> Self {
222 match error {
223 error @ DbError::OperationChanged(_) => {
226 Self::Failed { failure: crate::LocalFailure::Database, source: Arc::new(error) }
227 }
228 error @ DbError::Invariant { .. } => {
230 Self::Failed { failure: crate::LocalFailure::Bug, source: Arc::new(error) }
231 }
232 DbError::Shutdown(shutdown) => Self::Shutdown(shutdown),
234 DbError::Bug(bug) => Self::Bug(bug),
235 DbError::StagingCleanup { operation, cleanup } => {
236 Self::cleanup((*operation).into(), (*cleanup).into())
237 }
238 error => Self::Database(error),
239 }
240 }
241}
242
243impl SyncError {
244 pub fn cleanup(operation: SyncError, cleanup: SyncError) -> Self {
247 Self::Cleanup { operation: Box::new(operation), cleanup: Box::new(cleanup) }
248 }
249
250 #[deny(clippy::wildcard_enum_match_arm)]
253 pub fn pending_reason(&self, provider: coven_storage::CloudProvider) -> crate::PendingReason {
254 use crate::PendingReason;
255 let error = self;
256 match error {
257 SyncError::Pending(record) => record.reason.clone(),
258 SyncError::Storage(error) => PendingReason::Storage {
259 provider,
260 failure: error.failure(),
261 answered: error.answered(),
262 },
263 SyncError::Damaged(object) => PendingReason::Refused(object.failure.clone()),
264 SyncError::UpdateRequired(update) => PendingReason::UpdateRequired(*update),
265 SyncError::Removed(removal) => PendingReason::Removed(*removal),
266 SyncError::MemberKeysReplaced { member } => {
267 PendingReason::MemberKeysReplaced { member: *member }
268 }
269 SyncError::KeyUnavailable { audience, key } => PendingReason::KeyUnavailable {
270 audience: audience.clone(),
271 key: *key,
272 holders: None,
273 },
274 SyncError::Rejected(reason) => PendingReason::Rejected(reason.clone()),
275 SyncError::Cleanup { operation, cleanup } => PendingReason::Cleanup {
276 operation: Box::new(operation.pending_reason(provider)),
277 cleanup: Box::new(cleanup.pending_reason(provider)),
278 },
279 SyncError::Failed { .. }
280 | SyncError::NoStorage
281 | SyncError::StoreOwner
282 | SyncError::InvitationChanged
283 | SyncError::CutOffRefused(_)
284 | SyncError::NotPending(_)
285 | SyncError::ReloadPending(_)
286 | SyncError::SnapshotPending(_)
287 | SyncError::OperationData(_)
288 | SyncError::SequenceExhausted
289 | SyncError::Database(_)
290 | SyncError::SecureStorage(_)
291 | SyncError::Crypto(_)
292 | SyncError::Format(_)
293 | SyncError::Key(_)
294 | SyncError::DeviceIdInUse(_)
295 | SyncError::Code(_)
296 | SyncError::Shutdown(_)
297 | SyncError::Bug(_)
298 | SyncError::WorkerFailed(_) => PendingReason::Failed(error.local_failure()),
299 }
300 }
301
302 pub(crate) fn disk(error: impl std::error::Error + Send + Sync + 'static) -> Self {
304 Self::Failed { failure: crate::LocalFailure::of_disk(&error), source: Arc::new(error) }
305 }
306
307 #[deny(clippy::wildcard_enum_match_arm)]
309 pub(crate) fn local_failure(&self) -> crate::LocalFailure {
310 match self {
311 Self::Failed { failure, .. } => *failure,
312 Self::Database(_) => crate::LocalFailure::Database,
313 Self::SecureStorage(_) => crate::LocalFailure::KeyCustody,
314 Self::Crypto(_) | Self::Key(_) => crate::LocalFailure::Crypto,
315 Self::Cleanup { operation, .. } => operation.local_failure(),
316 Self::Pending(_)
317 | Self::NoStorage
318 | Self::StoreOwner
319 | Self::InvitationChanged
320 | Self::NotPending(_)
321 | Self::ReloadPending(_)
322 | Self::SnapshotPending(_)
323 | Self::OperationData(_)
324 | Self::SequenceExhausted
325 | Self::Storage(_)
326 | Self::Format(_)
327 | Self::KeyUnavailable { .. }
328 | Self::Rejected(_)
329 | Self::CutOffRefused(_)
330 | Self::Damaged(_)
331 | Self::Removed(_)
332 | Self::DeviceIdInUse(_)
333 | Self::MemberKeysReplaced { .. }
334 | Self::UpdateRequired(_)
335 | Self::Code(_) => crate::LocalFailure::Operation,
336 Self::Shutdown(_) => crate::LocalFailure::Shutdown,
337 Self::Bug(_) | Self::WorkerFailed(_) => crate::LocalFailure::Bug,
338 }
339 }
340
341 pub(crate) fn local_failure_clears_by_itself(&self) -> bool {
346 matches!(
347 self,
348 Self::Database(_)
349 | Self::SecureStorage(_)
350 | Self::Failed { .. }
351 | Self::Shutdown(_)
352 | Self::Bug(_)
353 | Self::WorkerFailed(_)
354 ) && crate::PendingReason::Failed(self.local_failure()).clears()
355 == Some(crate::pending::Clears::ByItself)
356 }
357
358 pub(crate) fn cause(&self) -> &(dyn std::error::Error + 'static) {
361 match self {
362 Self::Failed { source, .. } => source.as_ref(),
363 error => error,
364 }
365 }
366
367 pub fn into_shared(self) -> (crate::LocalFailure, Arc<dyn std::error::Error + Send + Sync>) {
370 match self {
371 Self::Failed { failure, source } => (failure, source),
372 error => (error.local_failure(), Arc::new(error)),
373 }
374 }
375}
376
377impl From<StorageError> for SyncError {
378 fn from(error: StorageError) -> Self {
379 match error {
380 StorageError::Bug(bug) => Self::Bug(bug),
381 error => Self::Storage(error),
382 }
383 }
384}
385
386impl From<KeyError> for SyncError {
387 fn from(error: KeyError) -> Self {
388 match error {
389 KeyError::Bug(bug) => Self::Bug(bug),
390 error => Self::SecureStorage(error),
391 }
392 }
393}
394
395impl From<FileError> for SyncError {
396 fn from(error: FileError) -> Self {
397 match error {
398 FileError::Bug(bug) => Self::Bug(bug),
399 error => Self::disk(error),
400 }
401 }
402}
403
404impl From<ObservationError> for SyncError {
405 fn from(error: ObservationError) -> Self {
406 Self::disk(error)
407 }
408}
409
410impl From<coven_foundation::files::WriterError> for SyncError {
411 fn from(error: coven_foundation::files::WriterError) -> Self {
412 match error {
413 coven_foundation::files::WriterError::Lock(error) => error.into(),
414 coven_foundation::files::WriterError::File(error) => error.into(),
415 }
416 }
417}
418
419impl From<StoreLockError> for SyncError {
420 fn from(error: StoreLockError) -> Self {
421 match error {
422 StoreLockError::Bug(bug) => Self::Bug(bug),
423 error => Self::disk(error),
424 }
425 }
426}
427
428#[derive(Clone, Debug, thiserror::Error)]
430pub enum SyncFailure {
431 #[error("an update is required")]
433 UpdateRequired(crate::RequiredUpdate),
434 #[error("this device is not in the store: {0:?}")]
436 Removed(coven_database::Removal),
437 #[error("another copy uses this device's id: {0:?}")]
439 DeviceIdInUse(crate::DeviceIdEvidence),
440 #[error("storage failed: {0}")]
442 Storage(#[source] Arc<StorageError>),
443 #[error("coven's own work failed its last attempt: {0}")]
447 Operation(Box<crate::PendingRecord>),
448 #[error("sync failed: {0}")]
450 Other(#[source] Arc<dyn std::error::Error + Send + Sync>),
451}
452
453impl From<OperationRejection> for SyncError {
454 fn from(reason: OperationRejection) -> Self {
455 match reason {
456 OperationRejection::StoreOwner => Self::StoreOwner,
457 reason => Self::Rejected(reason),
458 }
459 }
460}
461
462impl From<StorageFailure> for SyncError {
463 fn from(failure: StorageFailure) -> Self {
464 Self::Storage(failure.into())
465 }
466}
467
468impl From<SyncError> for SyncFailure {
469 fn from(error: SyncError) -> Self {
470 match error {
471 SyncError::Removed(removal) => Self::Removed(removal),
472 SyncError::DeviceIdInUse(evidence) => Self::DeviceIdInUse(evidence),
473 SyncError::UpdateRequired(update) => Self::UpdateRequired(update),
474 SyncError::Storage(error) => Self::Storage(Arc::new(error)),
475 error => Self::Other(Arc::new(error)),
476 }
477 }
478}
479
480impl From<coven_format::Error> for SyncError {
481 fn from(error: coven_format::Error) -> Self {
482 match required_format(&error) {
483 Some(update) => Self::UpdateRequired(update),
484 None => Self::Format(error),
485 }
486 }
487}
488
489impl From<CryptoError> for SyncError {
490 fn from(error: CryptoError) -> Self {
491 Self::Crypto(error)
492 }
493}
494
495#[derive(Clone, Debug, thiserror::Error)]
497#[error("damaged object at {path}: {failure}")]
498pub struct DamagedObject {
499 pub path: String,
501 #[source]
503 pub failure: crate::Refusal,
504}
505
506pub(crate) fn required_format(
507 error: &(dyn std::error::Error + 'static),
508) -> Option<crate::RequiredUpdate> {
509 let version = match error.downcast_ref::<coven_format::Error>() {
510 Some(coven_format::Error::UnsupportedVersion(version)) => Some(*version),
511 _ => None,
512 }?;
513 (version > coven_format::FORMAT_VERSION)
514 .then_some(crate::RequiredUpdate::CovenFormat { version })
515}
516
517pub(crate) fn newer_format(error: &(dyn std::error::Error + 'static)) -> bool {
519 required_format(error).is_some()
520}
521
522#[cfg(test)]
523#[path = "error_tests.rs"]
524mod tests;