1use std::sync::Arc;
8
9use fedimint_api_client::api::{ClientConfigDownloadError, FederationError};
10pub use fedimint_client_module::error::*;
11use fedimint_core::core::{ModuleInstanceId, ModuleKind};
12use fedimint_core::db::{DatabaseError, DbMigrationError};
13use fedimint_core::encoding::DecodeError;
14pub use fedimint_eventlog::EventHandlerError;
15use thiserror::Error;
16
17#[derive(Debug, Error)]
19#[non_exhaustive]
20pub enum ClientSecretError {
21 #[error("An encoded client secret already exists and cannot be overwritten")]
23 AlreadyExists,
24
25 #[error("No encoded client secret is present in the database")]
27 NotPresent,
28
29 #[error("The stored client secret could not be decoded")]
31 Decode(#[from] DecodeError),
32}
33
34#[derive(Debug, Error)]
36#[non_exhaustive]
37pub enum ClientBuildError {
38 #[error("The client database is not initialized")]
40 DatabaseNotInitialized,
41
42 #[error("The client database is already initialized")]
45 DatabaseAlreadyInitialized,
46
47 #[error("The secret does not match the one this database was created with")]
49 SecretMismatch,
50
51 #[error("Failed to download the client config")]
53 ConfigDownload(#[source] Box<ClientConfigDownloadError>),
54
55 #[error("Failed to decode the client config")]
58 ConfigDecode(#[from] DecodeError),
59
60 #[error("Failed to migrate the client database")]
62 Migration(#[from] DbMigrationError),
63
64 #[error("Module {instance_id} ({kind}) failed to prepare its recovery")]
66 ModuleRecoveryPrepare {
67 kind: ModuleKind,
69 instance_id: ModuleInstanceId,
71 #[source]
73 source: ClientModuleError,
74 },
75
76 #[error("Module {instance_id} ({kind}) failed to initialize")]
78 ModuleInit {
79 kind: ModuleKind,
81 instance_id: ModuleInstanceId,
83 #[source]
85 source: ClientModuleError,
86 },
87
88 #[error("Database error")]
90 Database(#[from] DatabaseError),
91
92 #[error("The client is already stopped")]
94 AlreadyStopped,
95}
96
97impl From<ClientConfigDownloadError> for ClientBuildError {
98 fn from(source: ClientConfigDownloadError) -> Self {
99 Self::ConfigDownload(Box::new(source))
100 }
101}
102
103#[derive(Debug, Error)]
109#[non_exhaustive]
110pub enum GlobalRpcError {
111 #[error(transparent)]
114 Json(#[from] serde_json::Error),
115
116 #[error(transparent)]
118 Federation(#[from] FederationError),
119
120 #[error(transparent)]
123 Recovery(#[from] RecoveryError),
124
125 #[error(transparent)]
127 Backup(#[from] BackupError),
128
129 #[error("Unknown method: {method}")]
131 UnknownMethod {
132 method: String,
134 },
135}
136
137#[derive(Debug, Error)]
139#[non_exhaustive]
140pub enum BackupError {
141 #[error("Cannot back up while a module recovery is still running")]
143 PendingRecoveries,
144
145 #[error("The federation could not be reached")]
147 Federation(#[source] Box<FederationError>),
148
149 #[error("Module {instance_id} failed to produce its backup")]
151 Module {
152 instance_id: ModuleInstanceId,
154 #[source]
156 source: ClientModuleError,
157 },
158
159 #[error("The backup payload is {size} bytes, over the limit of {max}")]
161 TooLarge {
162 size: usize,
164 max: usize,
166 },
167
168 #[error("The backup could not be encrypted or decrypted")]
170 Encryption(#[source] Box<dyn std::error::Error + Send + Sync>),
171
172 #[error("The backup could not be decoded")]
174 Decode(#[from] DecodeError),
175}
176
177impl From<FederationError> for BackupError {
178 fn from(source: FederationError) -> Self {
179 Self::Federation(Box::new(source))
180 }
181}
182
183#[derive(Debug, Error)]
185#[non_exhaustive]
186pub enum RecoveryError {
187 #[error("Recovery of module {module_instance_id} failed")]
192 Failed {
193 module_instance_id: ModuleInstanceId,
195 #[source]
198 source: Arc<ClientModuleError>,
199 },
200
201 #[error("The client shut down before the recovery finished")]
203 ClientStopped,
204}