Skip to main content

coven/
bootstrap_error.rs

1//! Why opening a new installation failed, and how each cause it keeps
2//! becomes one of its variants (§13, E10, E15).
3
4use super::*;
5use coven_foundation::files::BootstrapDirectoryError;
6
7/// Opening a new installation failed. A join's unpublished work, and a
8/// recorded restore's, remain for an explicit retry unless the call was
9/// cancelled or the invite was settled; a restore before its record removes
10/// its own.
11#[derive(Debug, thiserror::Error)]
12pub enum BootstrapError {
13    /// Cancellation removes unpublished local data and leaves final custody alone.
14    #[error("opening cancelled")]
15    Cancelled,
16    /// Cancelling a join after its request may have been sent. The join stays
17    /// unfinished and resumable; it ends only by approval, decline or expiry
18    /// (§13.2).
19    #[error("the join request may have been sent; the join stays unfinished")]
20    JoinRequestSent,
21    /// No join is under way for this store on this device: none was started
22    /// here, or it has ended.
23    #[error("no join is under way for store {0}")]
24    NoUnfinishedJoin(StoreId),
25    /// No unfinished first installation of this store is on this device, or
26    /// a call holds it, so `discard_unfinished_installation` deleted nothing.
27    #[error("no unfinished installation of store {0} to discard")]
28    NoUnfinishedInstallation(StoreId),
29    /// Cancelling a restore once its device may be registered. The restore
30    /// stays unfinished and listed, and only finishes (§13.1).
31    #[error("the device may be registered; the restore stays unfinished")]
32    RestoreRegistered,
33    /// No restore is under way for this store on this device: none recorded
34    /// itself here, or it has ended.
35    #[error("no restore is under way for store {0}")]
36    NoUnfinishedRestore(StoreId),
37    /// No replacement of this device's installation of the store is under
38    /// way past its record: none recorded itself, or it has finished, or a
39    /// connected reset's kept record is gone.
40    #[error("no replacement is under way for store {0}")]
41    NoUnfinishedReplacement(StoreId),
42    /// A join was signed in to another account than the one its invite
43    /// shared the folder with. Nothing of that sign-in is kept and nothing is
44    /// accepted or sent: the join stays listed, to resume with a sign-in to
45    /// the invited account, or to cancel (§13.2).
46    #[error("the invite was shared with {invited}, but the sign-in is {signed_in}")]
47    WrongAccount {
48        /// The account the invite shared the folder with, as the inviting
49        /// device recorded it.
50        invited: String,
51        /// The account the sign-in is, by its primary address.
52        signed_in: String,
53    },
54    /// The code cannot be used for this operation.
55    #[error(transparent)]
56    Code(#[from] CodeError),
57    /// Preparing, publishing or removing the unpublished directory failed.
58    #[error(transparent)]
59    Directory(BootstrapDirectoryError),
60    /// Opening the database failed.
61    #[error(transparent)]
62    Store(CovenError),
63    /// The device is locked, and the protection class the app chose keeps
64    /// the installation's files closed until it unlocks (api.md E1). A join's
65    /// unfinished installation stays for the retry, and a recorded
66    /// restore's; a restore before its record removes its own. The app waits
67    /// for the unlock and calls again.
68    #[error("{}: the device is locked, and the store's files are protected until it unlocks", path.display())]
69    DeviceLocked {
70        /// The first protected file the lock refused.
71        path: std::path::PathBuf,
72    },
73    /// Provider admission, signed membership or snapshot loading failed.
74    #[error(transparent)]
75    Sync(SyncError),
76    /// Provider sign-in failed.
77    #[error(transparent)]
78    OAuth(coven_storage::providers::OAuthError),
79    /// Final custody could not be read or written.
80    #[error(transparent)]
81    SecureStorage(KeyError),
82    /// Cleanup failed too; retry can inspect both failures.
83    #[error("{operation}; cleanup failed: {cleanup}")]
84    Cleanup {
85        /// The initial failure.
86        operation: Box<BootstrapError>,
87        /// Its cleanup failure.
88        #[source]
89        cleanup: Box<BootstrapError>,
90    },
91    /// The loaded store is published; confirming durability or final cleanup
92    /// failed. Custody remains committed and the handle retains session-only keys.
93    #[error("store is published; final durability or cleanup failed: {source}")]
94    Published {
95        /// The open store, including its session-only custody.
96        handle: CovenHandle,
97        /// Failure after publication.
98        #[source]
99        source: Box<BootstrapError>,
100    },
101    /// The call's runtime is shutting down and dropped its keychain work
102    /// before it started, so nothing changed (E15).
103    #[error(transparent)]
104    Shutdown(#[from] crate::task::Shutdown),
105    /// The call's work panicked: a bug, which the caller reports (E15).
106    #[error(transparent)]
107    Bug(#[from] crate::task::Bug),
108}
109
110impl From<BootstrapDirectoryError> for BootstrapError {
111    fn from(error: BootstrapDirectoryError) -> Self {
112        if let BootstrapDirectoryError::Bug(bug) = error {
113            return Self::Bug(bug);
114        }
115        match error.device_locked() {
116            Some(path) => Self::DeviceLocked { path: path.to_owned() },
117            None => Self::Directory(error),
118        }
119    }
120}
121
122/// A code sync refuses is refused as the call's own code: a join's, as a
123/// restore's.
124impl From<SyncError> for BootstrapError {
125    fn from(error: SyncError) -> Self {
126        match error {
127            SyncError::Code(error) => Self::Code(error),
128            SyncError::Bug(bug) => Self::Bug(bug),
129            error => Self::Sync(error),
130        }
131    }
132}
133
134impl From<CovenError> for BootstrapError {
135    fn from(error: CovenError) -> Self {
136        match error {
137            CovenError::DeviceLocked { path } => Self::DeviceLocked { path },
138            CovenError::Bug(bug) => Self::Bug(bug),
139            error => Self::Store(error),
140        }
141    }
142}
143
144/// A database call's or opening's failure is the store's.
145impl From<DbError> for BootstrapError {
146    fn from(error: DbError) -> Self {
147        CovenError::from(error).into()
148    }
149}
150
151impl From<coven_database::OpenError> for BootstrapError {
152    fn from(error: coven_database::OpenError) -> Self {
153        CovenError::from(error).into()
154    }
155}
156
157impl From<coven_storage::providers::OAuthError> for BootstrapError {
158    fn from(error: coven_storage::providers::OAuthError) -> Self {
159        match error {
160            coven_storage::providers::OAuthError::Storage(StorageError::Bug(bug)) => Self::Bug(bug),
161            error => Self::OAuth(error),
162        }
163    }
164}
165
166impl From<StoreLockError> for BootstrapError {
167    fn from(error: StoreLockError) -> Self {
168        BootstrapDirectoryError::from(error).into()
169    }
170}
171
172impl From<coven_foundation::files::FileError> for BootstrapError {
173    fn from(error: coven_foundation::files::FileError) -> Self {
174        BootstrapDirectoryError::from(error).into()
175    }
176}
177
178impl From<KeyError> for BootstrapError {
179    fn from(error: KeyError) -> Self {
180        match error {
181            KeyError::Bug(bug) => Self::Bug(bug),
182            error => Self::SecureStorage(error),
183        }
184    }
185}
186
187impl crate::task::CarriesBug for BootstrapError {
188    fn bug(&self) -> Option<&crate::task::Bug> {
189        match self {
190            Self::Bug(bug) => Some(bug),
191            Self::Directory(error) => error.bug(),
192            Self::Store(error) => error.bug(),
193            Self::Sync(error) => error.bug(),
194            Self::OAuth(error) => error.bug(),
195            Self::SecureStorage(error) => error.bug(),
196            Self::Cleanup { operation, cleanup } => operation.bug().or_else(|| cleanup.bug()),
197            Self::Published { source, .. } => source.bug(),
198            _ => None,
199        }
200    }
201}
202
203impl From<crate::task::TaskFailed> for BootstrapError {
204    fn from(failed: crate::task::TaskFailed) -> Self {
205        failed.into_error()
206    }
207}