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
use std::{convert::Infallible, str::Utf8Error, string::FromUtf8Error, sync::Arc};
use bonsaidb_core::{
permissions::PermissionDenied,
pubsub::{Disconnected, TryReceiveError},
schema::{view, InsertError, InvalidNameError},
AnyError,
};
use nebari::AbortError;
use crate::database::compat::UnknownVersion;
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("error from storage: {0}")]
Nebari(#[from] nebari::Error),
#[error("error while serializing internal structures: {0}")]
InternalSerialization(String),
#[error("error while serializing: {0}")]
Serialization(#[from] pot::Error),
#[error("error while communicating internally")]
InternalCommunication,
#[error("transaction is too large")]
TransactionTooLarge,
#[error("error from view: {0}")]
View(#[from] view::Error),
#[error("a vault error occurred: {0}")]
#[cfg(feature = "encryption")]
Vault(#[from] crate::vault::Error),
#[error("a vault error occurred: {0}")]
#[cfg(feature = "compression")]
Compression(#[from] lz4_flex::block::DecompressError),
#[error("encryption is disabled, but a collection is requesting encryption")]
#[cfg(not(feature = "encryption"))]
EncryptionDisabled,
#[error("a core error occurred: {0}")]
Core(#[from] bonsaidb_core::Error),
#[cfg(feature = "async")]
#[error("a concurrency error ocurred: {0}")]
TaskJoin(#[from] tokio::task::JoinError),
#[error("an IO error occurred: {0}")]
Io(#[from] std::io::Error),
#[error("an error from a job occurred: {0}")]
Job(Arc<Error>),
#[error("a backup error: {0}")]
Backup(Box<dyn AnyError>),
#[cfg(feature = "password-hashing")]
#[error("password hash error: {0}")]
PasswordHash(String),
}
impl<T> From<InsertError<T>> for Error {
fn from(err: InsertError<T>) -> Self {
Self::Core(err.error)
}
}
impl From<flume::RecvError> for Error {
fn from(_: flume::RecvError) -> Self {
Self::InternalCommunication
}
}
impl From<TryReceiveError> for Error {
fn from(_: TryReceiveError) -> Self {
Self::InternalCommunication
}
}
impl From<Disconnected> for Error {
fn from(_: Disconnected) -> Self {
Self::InternalCommunication
}
}
impl From<bincode::Error> for Error {
fn from(err: bincode::Error) -> Self {
Self::InternalSerialization(err.to_string())
}
}
impl<T> From<UnknownVersion<T>> for Error {
fn from(err: UnknownVersion<T>) -> Self {
Self::InternalSerialization(err.to_string())
}
}
#[cfg(feature = "password-hashing")]
impl From<argon2::Error> for Error {
fn from(err: argon2::Error) -> Self {
Self::PasswordHash(err.to_string())
}
}
#[cfg(feature = "password-hashing")]
impl From<argon2::password_hash::Error> for Error {
fn from(err: argon2::password_hash::Error) -> Self {
Self::PasswordHash(err.to_string())
}
}
#[cfg(feature = "async")]
impl From<tokio::sync::oneshot::error::RecvError> for Error {
fn from(_: tokio::sync::oneshot::error::RecvError) -> Self {
Self::InternalCommunication
}
}
#[cfg(feature = "async")]
impl From<tokio::sync::oneshot::error::TryRecvError> for Error {
fn from(_: tokio::sync::oneshot::error::TryRecvError) -> Self {
Self::InternalCommunication
}
}
impl From<Error> for bonsaidb_core::Error {
fn from(err: Error) -> Self {
match err {
Error::View(view::Error::Core(core)) | Error::Core(core) => core,
other => Self::Database(other.to_string()),
}
}
}
impl From<Arc<Error>> for Error {
fn from(err: Arc<Error>) -> Self {
match Arc::try_unwrap(err) {
Ok(err) => err,
Err(still_wrapped) => Error::Job(still_wrapped),
}
}
}
impl From<FromUtf8Error> for Error {
fn from(err: FromUtf8Error) -> Self {
Self::Core(bonsaidb_core::Error::InvalidUnicode(err.to_string()))
}
}
impl From<Utf8Error> for Error {
fn from(err: Utf8Error) -> Self {
Self::Core(bonsaidb_core::Error::InvalidUnicode(err.to_string()))
}
}
impl From<InvalidNameError> for Error {
fn from(err: InvalidNameError) -> Self {
Self::Core(bonsaidb_core::Error::from(err))
}
}
impl From<AbortError<Infallible>> for Error {
fn from(err: AbortError<Infallible>) -> Self {
match err {
AbortError::Nebari(error) => Self::Nebari(error),
AbortError::Other(_) => unreachable!(),
}
}
}
impl From<AbortError<Error>> for Error {
fn from(err: AbortError<Error>) -> Self {
match err {
AbortError::Nebari(error) => Self::Nebari(error),
AbortError::Other(error) => error,
}
}
}
impl From<PermissionDenied> for Error {
fn from(err: PermissionDenied) -> Self {
Self::Core(bonsaidb_core::Error::from(err))
}
}
#[test]
fn test_converting_error() {
use serde::ser::Error as _;
let err: bonsaidb_core::Error = Error::Serialization(pot::Error::custom("mymessage")).into();
match err {
bonsaidb_core::Error::Database(storage_error) => {
assert!(storage_error.contains("mymessage"));
}
_ => unreachable!(),
}
}