1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//! Error type for the client module.
use sos_sdk::{
    commit::CommitHash,
    events::WriteEvent,
    vault::{secret::SecretId, Summary},
};
use std::path::PathBuf;
use thiserror::Error;
use uuid::Uuid;

/// Errors generated by the client module.
#[derive(Debug, Error)]
pub enum Error {
    /// Error generated if we could not determine a cache directory.
    #[error("could not determine cache directory")]
    NoCache,

    /// Error generated when an archive folder is not available.
    #[error("archive folder does not exist")]
    NoArchive,

    /// Error generated when an open folder is expected.
    #[error("no open folder")]
    NoOpenFolder,

    /// Error generated when secret data does not have an identifier
    /// but an existing secret is expected.
    #[error("secret does not have an identifier")]
    NoSecretId,

    /// Error generated when a file secret is expected.
    #[error("not a file secret")]
    NotFileContent,

    /// Error generated when attempting to archive a secret that
    /// is already archived.
    #[error("cannot move to archive, already archived")]
    AlreadyArchived,

    /// Error generated when attempting to unarchive a secret that
    /// is not archived.
    #[error("cannot unarchive, not archived")]
    NotArchived,

    /// Error generated when a path is not a directory.
    #[error("path {0} is not a directory")]
    NotDirectory(PathBuf),

    /// Error generated when a path is not a file.
    #[error("path {0} is not a file")]
    NotFile(PathBuf),

    /// Error generated when no default folder is available.
    #[error("no default folder")]
    NoDefaultFolder,

    /// Error generated when a secret is not a contact secret.
    #[cfg(feature = "contacts")]
    #[error("not a contact")]
    NotContact,

    /// Error generated when a contacts folder is not available.
    #[cfg(feature = "contacts")]
    #[error("no contacts folder")]
    NoContactsFolder,

    /// Error generated when a PEM-encoded certificate is invalid.
    #[error("invalid PEM encoding")]
    PemEncoding,

    /// Error generated when a provider is not valid.
    #[error("provider {0} is not valid")]
    InvalidProvider(String),

    /// Error generated when a file already exists.
    #[error("file {0} already exists")]
    FileExists(PathBuf),

    /// Error generated when unlocking a vault failed.
    #[error("failed to unlock vault")]
    VaultUnlockFail,

    /// Error generated when an unexpected response code is received.
    #[error("unexpected response status code {0}")]
    ResponseCode(u16),

    /// Error generated when root commit hashes do not match.
    #[error("local and remote root hashes do not match; local = {0}, remote = {1}; you may need to pull or push to sync changes")]
    RootHashMismatch(CommitHash, CommitHash),

    /// Error generated if a server failed to send the expected
    /// commit proof header.
    #[error("server failed to send the expected commit proof header")]
    ServerProof,

    /// Error generated attempting to access a vault that is not available.
    #[error("cache not available for {0}")]
    CacheNotAvailable(Uuid),

    /// Error generated when a conflict is detected where the local  
    /// is behind the remote.
    ///
    /// Pulling from remote and applying changes afterwards should resolve
    /// the conflict.
    #[error("conflict detected, pull required")]
    ConflictBehind {
        /// Summary of the vault that triggered the conflict.
        summary: Summary,
        /// Commit hash of the local event log.
        local: (CommitHash, usize),
        /// Commit hash of the remote event log.
        remote: (CommitHash, usize),
        /// Events that can be applied after a pull.
        events: Vec<WriteEvent<'static>>,
    },

    /// Error generated when a conflict is detected that may be
    /// resolved by the user.
    #[error("conflict detected that may be resolvable")]
    Conflict {
        /// Summary of the vault that triggered the conflict.
        summary: Summary,
        /// Commit hash of the local event log.
        local: (CommitHash, usize),
        /// Commit hash of the remote event log.
        remote: (CommitHash, usize),
    },

    /// Error generated when a commit tree is expected to have a root.
    #[error("commit tree does not have a root")]
    NoRootCommit,

    /// Error generated when a return value is expected from a RPC call
    /// but the response did not have a result.
    #[error("method did not return a value")]
    NoReturnValue,

    /// Error generated when a session has no been set.
    #[error("session not set, authentication is required")]
    NoSession,

    /// Error generated when a session has no been set.
    #[error("session is invalid, authentication is required")]
    InvalidSession,

    /// Error generated when a client receives an unauthorized response.
    #[error("not authorized, authentication is required")]
    NotAuthorized,

    /// Error generated attempting to make changes to the current
    /// vault but no vault is open.
    #[error("no vault is available, vault must be open")]
    NoOpenVault,

    /// Error generated when a secret could not be found.
    #[error(r#"secret "{0}" not found"#)]
    SecretNotFound(SecretId),

    /// Generic boxed error.
    #[error(transparent)]
    Boxed(#[from] Box<dyn std::error::Error + Send + Sync>),

    /// Error generated by the main node library.
    #[error(transparent)]
    Node(#[from] crate::Error),

    /// Error generated parsing to an integer.
    #[error(transparent)]
    ParseInt(#[from] std::num::ParseIntError),

    /// Error generated converting a header to a string.
    #[error(transparent)]
    ToStr(#[from] reqwest::header::ToStrError),

    /// Error generated by the io module.
    #[error(transparent)]
    Io(#[from] std::io::Error),

    /// Error generated by the JSON library.
    #[error(transparent)]
    Json(#[from] serde_json::Error),

    /// Error generated attempting to convert from a slice.
    #[error(transparent)]
    TryFromSlice(#[from] std::array::TryFromSliceError),

    /// Error generated by the core library.
    #[error(transparent)]
    Core(#[from] sos_sdk::Error),

    /// Error generated by the HTTP request library.
    #[error(transparent)]
    Http(#[from] reqwest::Error),

    /// Error generated attempting to parse a URL.
    #[error(transparent)]
    UrlParse(#[from] url::ParseError),

    /// Error generated attempting to convert to a UTF-8 string.
    #[error(transparent)]
    Utf8(#[from] std::str::Utf8Error),

    /// Error generated decoding a base58 string.
    #[error(transparent)]
    Base58Decode(#[from] bs58::decode::Error),

    /// Error generated converting an HTTP status code.
    #[error(transparent)]
    HttpStatus(#[from] http::status::InvalidStatusCode),

    /// Error generated by the websocket client.
    #[cfg(not(target_arch = "wasm32"))]
    #[error(transparent)]
    WebSocket(#[from] tokio_tungstenite::tungstenite::Error),

    /// Error generated by the address library.
    #[error(transparent)]
    Address(#[from] web3_address::Error),

    /// Error generated when converting to a UUID.
    #[error(transparent)]
    Uuid(#[from] uuid::Error),

    /// Error generated when parsing from hex.
    #[error(transparent)]
    Hex(#[from] hex::FromHexError),

    #[cfg(all(feature = "peer", not(target_arch = "wasm32")))]
    /// Error generated by the peer library.
    #[error(transparent)]
    Peer(#[from] crate::peer::Error),

    #[cfg(feature = "migrate")]
    /// Error generated by the migrate library.
    #[error(transparent)]
    Migrate(#[from] sos_migrate::Error),

    /// Error generated by the vcard library.
    #[cfg(feature = "contacts")]
    #[error(transparent)]
    Vcard(#[from] sos_sdk::vcard4::Error),

    /// Error generated by the MPC library.
    #[error(transparent)]
    Mpc(#[from] sos_sdk::mpc::Error),

    /// Error generated by the noise library.
    #[error(transparent)]
    Snow(#[from] sos_sdk::mpc::snow::Error),
}