1
use std::sync::Arc;
2

            
3
use bonsaidb_core::{permissions::PermissionDenied, schema, schema::InsertError, AnyError};
4
use schema::InvalidNameError;
5

            
6
/// An error occurred while interacting with a [`Server`](crate::Server).
7
18
#[derive(Debug, thiserror::Error)]
8
pub enum Error {
9
    /// An error occurred from the QUIC transport layer.
10
    #[error("a networking error occurred: '{0}'")]
11
    Transport(String),
12

            
13
    #[cfg(feature = "websockets")]
14
    /// An error occurred from the Websocket transport layer.
15
    #[error("a websocket error occurred: '{0}'")]
16
    WebSocket(#[from] tokio_tungstenite::tungstenite::Error),
17

            
18
    /// An error occurred from IO
19
    #[error("a networking error occurred: '{0}'")]
20
    Io(#[from] tokio::io::Error),
21

            
22
    /// An error occurred while processing a request
23
    #[error("an error occurred processing a request: '{0}'")]
24
    Request(Arc<dyn AnyError>),
25

            
26
    /// An error occurred from within the schema.
27
    #[error("error from core {0}")]
28
    Core(#[from] bonsaidb_core::Error),
29

            
30
    /// An internal error occurred while waiting for a message.
31
    #[error("error while waiting for a message")]
32
    InternalCommunication,
33

            
34
    /// An error occurred while interacting with a local database.
35
    #[error("an error occurred interacting with a database: {0}")]
36
    Database(#[from] bonsaidb_local::Error),
37

            
38
    /// An error occurred with a certificate.
39
    #[error("a certificate error: {0}")]
40
    Certificate(#[from] fabruic::error::Certificate),
41

            
42
    /// An error occurred parsing a PEM file.
43
    #[error("an invalid PEM file: {0}")]
44
    #[cfg(feature = "pem")]
45
    Pem(#[from] pem::PemError),
46

            
47
    /// An error occurred requesting an ACME certificate.
48
    #[error("an error requesting an ACME certificate: {0}")]
49
    #[cfg(feature = "acme")]
50
    Acme(#[from] async_acme::acme::AcmeError),
51

            
52
    /// An error occurred while processing an ACME order.
53
    #[error("an error occurred while processing an ACME order: {0}")]
54
    #[cfg(feature = "acme")]
55
    AcmeOrder(#[from] async_acme::rustls_helper::OrderError),
56

            
57
    /// An error occurred during tls signing.
58
    #[error("an error occurred during tls signing")]
59
    TlsSigningError,
60
}
61

            
62
impl From<Error> for bonsaidb_core::Error {
63
    fn from(other: Error) -> Self {
64
        // without it, there's no way to get this to_string() easily.
65
        match other {
66
18774
            Error::Core(core) | Error::Database(bonsaidb_local::Error::Core(core)) => core,
67
            Error::Database(storage) => Self::Database(storage.to_string()),
68
            Error::Io(io) => Self::Io(io.to_string()),
69
            Error::Transport(networking) => Self::Transport(networking),
70
            #[cfg(feature = "websockets")]
71
            Error::WebSocket(err) => Self::Websocket(err.to_string()),
72
            err => Self::Server(err.to_string()),
73
        }
74
18774
    }
75
}
76

            
77
impl From<flume::RecvError> for Error {
78
    fn from(_: flume::RecvError) -> Self {
79
        Self::InternalCommunication
80
    }
81
}
82

            
83
impl From<tokio::sync::oneshot::error::RecvError> for Error {
84
    fn from(_: tokio::sync::oneshot::error::RecvError) -> Self {
85
        Self::InternalCommunication
86
    }
87
}
88

            
89
impl From<tokio::sync::oneshot::error::TryRecvError> for Error {
90
    fn from(_: tokio::sync::oneshot::error::TryRecvError) -> Self {
91
        Self::InternalCommunication
92
    }
93
}
94

            
95
impl From<PermissionDenied> for Error {
96
126
    fn from(err: PermissionDenied) -> Self {
97
126
        Self::Core(bonsaidb_core::Error::PermissionDenied(err))
98
126
    }
99
}
100

            
101
impl From<InvalidNameError> for Error {
102
    fn from(err: InvalidNameError) -> Self {
103
        Self::Core(bonsaidb_core::Error::InvalidName(err))
104
    }
105
}
106

            
107
impl From<rustls::sign::SignError> for Error {
108
    fn from(_: rustls::sign::SignError) -> Self {
109
        Self::TlsSigningError
110
    }
111
}
112

            
113
impl<T> From<InsertError<T>> for Error {
114
    fn from(error: InsertError<T>) -> Self {
115
        Self::from(error.error)
116
    }
117
}
118

            
119
pub trait ResultExt<R> {
120
    fn map_err_to_core(self) -> Result<R, bonsaidb_core::Error>
121
    where
122
        Self: Sized;
123
}
124

            
125
impl<R> ResultExt<R> for Result<R, Error> {
126
    fn map_err_to_core(self) -> Result<R, bonsaidb_core::Error>
127
    where
128
        Self: Sized,
129
    {
130
        self.map_err(bonsaidb_core::Error::from)
131
    }
132
}
133

            
134
#[cfg(feature = "websockets")]
135
impl From<bincode::Error> for Error {
136
    fn from(other: bincode::Error) -> Self {
137
        Self::Core(bonsaidb_core::Error::Websocket(format!(
138
            "error deserializing message: {:?}",
139
            other
140
        )))
141
    }
142
}
143

            
144
macro_rules! impl_from_fabruic {
145
    ($error:ty) => {
146
        impl From<$error> for Error {
147
            fn from(other: $error) -> Self {
148
                Self::Core(bonsaidb_core::Error::Transport(other.to_string()))
149
            }
150
        }
151
    };
152
}
153

            
154
impl_from_fabruic!(fabruic::error::CertificateChain);
155
impl_from_fabruic!(fabruic::error::Receiver);
156
impl_from_fabruic!(fabruic::error::Connecting);
157
impl_from_fabruic!(fabruic::error::PrivateKey);
158
impl_from_fabruic!(fabruic::error::KeyPair);
159
impl_from_fabruic!(fabruic::error::Connection);
160
impl_from_fabruic!(fabruic::error::Incoming);
161
impl_from_fabruic!(fabruic::error::AlreadyClosed);
162
impl_from_fabruic!(fabruic::error::Config);