snapper_box/error.rs
1//! Error types for the crate
2//!
3//! This module provides both [`BackendError`] for interacting with the low level APIs, and
4//! [`CryptoBoxError`] for high level errors.
5
6use snafu::Snafu;
7
8/// Error type describing faults that can happen during encryption or decryption.
9///
10/// This is a fairly low level type for interacting with the api directly, you probably want
11/// [`CryptoBoxError`]
12#[derive(Debug, Snafu)]
13#[non_exhaustive]
14#[snafu(visibility(pub(crate)))]
15pub enum BackendError {
16 /// HMAC tag verification failed
17 BadHMAC,
18 /// An error occurred while deriving a key from a password
19 Argon2Failure {
20 /// The underlying error
21 source: argon2::Error,
22 },
23 /// Failed to deserialize a key
24 KeyDeserialization,
25 /// Failed to serialize an item
26 ItemSerialization,
27 /// Failed to deserialize an item
28 ItemDeserialization,
29 /// A error occurred while attempting to compress a blob
30 Compression {
31 /// Underlying zstd error
32 source: std::io::Error,
33 },
34 /// A error occurred while attempting to decompress a blob
35 Decompression {
36 /// Underlying zstd error
37 source: std::io::Error,
38 },
39 /// An underlying IO error occurred while attempting to read a segment
40 SegmentIO {
41 /// Underlying error
42 source: std::io::Error,
43 },
44 /// A length mismatch occurred while parsing a segment
45 SegmentLength,
46 /// No data was provided for a segment, not even a length
47 NoData,
48 /// Suspected no data provided, please see IO error for details
49 NoDataIO {
50 /// Underlying error
51 source: std::io::Error,
52 },
53 /// Invalid compression flag in serialized data
54 InvalidCompression,
55 /// Failed reading/writing an entry
56 EntryIO {
57 /// Underlying error
58 source: std::io::Error,
59 },
60 /// Indicates an invalid state where an [`LsmFile`](crate::file::LsmFile)'s internal state points to a
61 /// value that doesn't exist
62 InvalidLsmState,
63}
64
65/// Errors that can be returned when interacting with a [`CryptoBox`](crate::CryptoBox)
66#[derive(Debug, Snafu)]
67#[non_exhaustive]
68#[snafu(visibility(pub(crate)))]
69pub enum CryptoBoxError {
70 /// Directory already exists
71 #[snafu(display("Directory already exists: {}", directory))]
72 DirectoryAlreadyExists {
73 /// The directory that already exists
74 directory: String,
75 },
76 /// Failed creating the `CryptoBox` directory
77 FailedCreatingDirectory {
78 /// The directory we couldn't create
79 directory: String,
80 /// The underlying IO error
81 source: std::io::Error,
82 },
83 /// Failed to encrypt root key
84 RootKeyEncryption {
85 /// The underlying error
86 source: BackendError,
87 },
88 /// Failed to decrypt root key. Most likely this is an incorrect password.
89 RootKeyDecryption {
90 /// The underlying error
91 source: BackendError,
92 },
93 /// Failed to write root key file
94 #[snafu(display("Failed to write to path: {}", path))]
95 RootKeyIO {
96 /// The path we failed to write to
97 path: String,
98 /// The underlying error
99 source: std::io::Error,
100 },
101 /// [`RootKey`](crate::crypto::RootKey) serialization error
102 RootKeySerial {
103 /// The underlying error
104 source: serde_cbor::Error,
105 },
106 /// Failed to initialize the root namespace
107 #[snafu(display("Failed to initialize root namespace: {}", path))]
108 RootNamespaceInit {
109 /// The path of the root namespace
110 path: String,
111 /// The underlying error
112 source: BackendError,
113 },
114 /// Failed to open root namespace
115 #[snafu(display("Failed to open root namespace: {}", path))]
116 RootNamespaceOpen {
117 /// The path of the root namespace
118 path: String,
119 /// The underlying error
120 source: BackendError,
121 },
122 /// Path does not exist or is not a directory
123 #[snafu(display("Path does not exist or is not a directory: {}", path))]
124 DirectoryDoesNotExist {
125 /// The path that doesn't exist
126 path: String,
127 },
128 /// Missing namespace directory
129 MissingNamespaceDirectory,
130 /// `CryptoBox` is missing the configuration entry
131 MissingConfiguration,
132 /// Failed to open a namespace
133 #[snafu(display("Failed to open a namespace {}", name))]
134 NamespaceOpen {
135 /// The name of the namespace that failed to open
136 name: String,
137 /// The underlying error
138 source: BackendError,
139 },
140 /// No such namespace
141 #[snafu(display("No such namespace: {}", name))]
142 NoSuchNamespace {
143 /// Namespace that doesn't exist
144 name: String,
145 },
146 /// Error retrieving item
147 Fetch {
148 /// Underlying failure
149 source: BackendError,
150 },
151 /// Error storing item
152 Store {
153 /// Underlying failure
154 source: BackendError,
155 },
156 /// An error occurred while flushing the `CryptoBox`
157 #[snafu(display("One or more errors occurred flushing: {:?}", sources))]
158 Flush {
159 /// Underlying failures
160 sources: Vec<(Option<String>, BackendError)>,
161 },
162 /// An error occurred in the async wrapper
163 #[cfg(feature = "experimental-async")]
164 AsyncError,
165}