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
use bonsaidb_core::arc_bytes::serde::Bytes;
use bonsaidb_core::networking;
use bonsaidb_core::schema::Name;

/// Errors related to working with the BonsaiDb client.
#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[cfg(feature = "websockets")]
    /// An error occurred from the WebSocket transport layer.
    #[error("a transport error occurred: '{0}'")]
    WebSocket(crate::client::WebSocketError),

    /// An error occurred from networking.
    #[error("a networking error occurred: '{0}'")]
    Network(#[from] bonsaidb_core::networking::Error),

    /// An invalid Url was provided.
    #[error("invalid url: '{0}'")]
    InvalidUrl(String),

    /// The connection was interrupted.
    #[error("unexpected disconnection")]
    Core(#[from] bonsaidb_core::Error),

    /// An error from a `Api`. The actual error is still serialized, as it
    /// could be any type.
    #[error("api {name} error")]
    Api {
        /// The unique name of the api that responded with an error
        name: Name,
        /// The serialized bytes of the error type.
        error: Bytes,
    },

    /// The server is incompatible with this version of the client.
    #[error("server incompatible with client protocol version")]
    ProtocolVersionMismatch,
}

impl Error {
    pub(crate) fn disconnected() -> Self {
        Self::Core(bonsaidb_core::Error::Networking(
            networking::Error::Disconnected,
        ))
    }

    pub(crate) fn request_timeout() -> Self {
        Self::Core(bonsaidb_core::Error::Networking(
            networking::Error::RequestTimeout,
        ))
    }

    pub(crate) fn connect_timeout() -> Self {
        Self::Core(bonsaidb_core::Error::Networking(
            networking::Error::ConnectTimeout,
        ))
    }
}

impl<T> From<flume::SendError<T>> for Error {
    fn from(_: flume::SendError<T>) -> Self {
        Self::disconnected()
    }
}

impl From<flume::RecvTimeoutError> for Error {
    fn from(err: flume::RecvTimeoutError) -> Self {
        match err {
            flume::RecvTimeoutError::Timeout => Self::request_timeout(),
            flume::RecvTimeoutError::Disconnected => Self::disconnected(),
        }
    }
}

impl From<flume::RecvError> for Error {
    fn from(_: flume::RecvError) -> Self {
        Self::disconnected()
    }
}

impl From<Error> for bonsaidb_core::Error {
    fn from(other: Error) -> Self {
        match other {
            Error::Core(err) => err,
            other => Self::other("bonsaidb-client", other),
        }
    }
}

#[cfg(feature = "websockets")]
impl From<bincode::Error> for Error {
    fn from(other: bincode::Error) -> Self {
        Self::Core(bonsaidb_core::Error::other("bincode", other))
    }
}

#[cfg(not(target_arch = "wasm32"))]
mod fabruic_impls {
    macro_rules! impl_from_fabruic {
        ($error:ty) => {
            impl From<$error> for $crate::Error {
                fn from(other: $error) -> Self {
                    Self::Core(bonsaidb_core::Error::other("quic", other))
                }
            }
        };
    }

    impl_from_fabruic!(fabruic::error::Sender);
    impl_from_fabruic!(fabruic::error::Receiver);
    impl_from_fabruic!(fabruic::error::Stream);
    impl_from_fabruic!(fabruic::error::Connecting);
    impl_from_fabruic!(fabruic::error::Connect);
}

#[cfg(feature = "websockets")]
impl From<crate::client::WebSocketError> for Error {
    #[cfg(not(target_arch = "wasm32"))]
    fn from(err: crate::client::WebSocketError) -> Self {
        if let crate::client::WebSocketError::Http(response) = &err {
            if response.status() == 406 {
                return Self::ProtocolVersionMismatch;
            }
        }

        Self::WebSocket(err)
    }

    #[cfg(target_arch = "wasm32")]
    fn from(err: crate::client::WebSocketError) -> Self {
        Self::WebSocket(err)
    }
}

impl From<pot::Error> for Error {
    fn from(err: pot::Error) -> Self {
        Self::from(bonsaidb_core::Error::from(err))
    }
}

/// An error returned from an api request.
#[derive(thiserror::Error, Debug)]
pub enum ApiError<T> {
    /// The API returned its own error type.
    #[error("api error: {0}")]
    Api(T),
    /// An error from BonsaiDb occurred.
    #[error("client error: {0}")]
    Client(#[from] Error),
}

impl From<ApiError<Self>> for bonsaidb_core::Error {
    fn from(error: ApiError<Self>) -> Self {
        match error {
            ApiError::Api(err) => err,
            ApiError::Client(err) => Self::from(err),
        }
    }
}