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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
use std::{borrow::Cow, fmt, sync::Arc};

use rmp::{
    decode::{MarkerReadError, NumValueReadError, ValueReadError},
    encode::{RmpWriteErr, ValueWriteError},
};

#[derive(Clone, Debug, thiserror::Error)]
#[error("{description} (code {code})")]
pub struct ErrorResponse {
    pub code: u32,
    pub description: String,
    pub extra: Option<rmpv::Value>,
}

impl ErrorResponse {
    pub fn new(code: u32, description: String, extra: Option<rmpv::Value>) -> Self {
        Self {
            code,
            description,
            extra,
        }
    }
}

#[non_exhaustive]
#[derive(Debug, thiserror::Error)]
pub enum Error {
    /// Error, returned in response from Tarantool instance.
    #[error("Error response: {0}")]
    Response(#[from] ErrorResponse),

    /// Timeout.
    #[error("Timeout")]
    Timeout,
    /// Timeout while establishing connection.
    #[error("Connect timeout")]
    ConnectTimeout,

    /// Authorization error.
    #[error("Authorization error: {} (code {})" ,.0.description, .0.code)]
    Auth(#[source] ErrorResponse),

    /// Errors, related to encoding requests.
    #[error(transparent)]
    Encode(#[from] EncodingError),
    /// Errors, related to decoding responses.
    #[error(transparent)]
    Decode(#[from] DecodingError),

    /// Duplicated sync detected.
    #[error("Duplicated sync '{0}'")]
    DuplicatedSync(u32),

    #[error("Failed to load metadata")]
    MetadataLoad(#[source] anyhow::Error),

    /// Underlying TCP connection closed.
    #[error("TCP connection error")]
    ConnectionError(#[from] Arc<tokio::io::Error>),
    /// Underlying TCP connection was closed.
    #[error("TCP connection closed")]
    ConnectionClosed,
}

impl From<tokio::io::Error> for Error {
    fn from(v: tokio::io::Error) -> Self {
        Self::ConnectionError(Arc::new(v))
    }
}

impl From<CodecDecodeError> for Error {
    fn from(value: CodecDecodeError) -> Self {
        match value {
            CodecDecodeError::Io(x) => x.into(),
            CodecDecodeError::Closed => Self::ConnectionClosed,
            CodecDecodeError::Decode(x) => x.into(),
        }
    }
}

impl From<CodecEncodeError> for Error {
    fn from(value: CodecEncodeError) -> Self {
        match value {
            CodecEncodeError::Io(x) => x.into(),
            CodecEncodeError::Encode(x) => x.into(),
        }
    }
}

/// Errors, related to encoding requests.
#[non_exhaustive]
#[derive(Debug, thiserror::Error)]
pub enum EncodingError {
    /// Error while encoding data into MessagePack format.
    #[error("Failed to encode data into MessagePack")]
    MessagePack(#[source] anyhow::Error),
}

impl<E> From<ValueWriteError<E>> for EncodingError
where
    E: RmpWriteErr + Send + Sync,
{
    fn from(v: ValueWriteError<E>) -> Self {
        Self::MessagePack(v.into())
    }
}

impl From<std::io::Error> for EncodingError {
    fn from(value: std::io::Error) -> Self {
        Self::MessagePack(value.into())
    }
}

/// Errors, related to decoding responses.
#[derive(Clone, Debug, thiserror::Error)]
#[error("{kind}{}", DecodingErrorLocation::display_in_error(.location))]
pub struct DecodingError {
    kind: Arc<DecodingErrorDetails>,
    location: Option<DecodingErrorLocation>,
}

impl DecodingError {
    pub(crate) fn new(kind: DecodingErrorDetails) -> Self {
        Self {
            kind: Arc::new(kind),
            location: None,
        }
    }

    pub(crate) fn missing_key(key: &'static str) -> Self {
        DecodingErrorDetails::MissingKey(key).into()
    }

    pub(crate) fn type_mismatch(
        expected: &'static str,
        actual: impl Into<Cow<'static, str>>,
    ) -> Self {
        DecodingErrorDetails::TypeMismatch {
            expected,
            actual: actual.into(),
        }
        .into()
    }

    pub(crate) fn message_pack(err: impl Into<anyhow::Error>) -> Self {
        DecodingErrorDetails::MessagePack(err.into()).into()
    }

    pub(crate) fn unknown_response_code(code: u32) -> Self {
        DecodingErrorDetails::UnknownResponseCode(code).into()
    }

    pub(crate) fn with_location(mut self, location: DecodingErrorLocation) -> Self {
        self.location = Some(location);
        self
    }

    pub(crate) fn in_key(self, key: &'static str) -> Self {
        self.with_location(DecodingErrorLocation::Key(key))
    }

    pub(crate) fn in_other(self, other: &'static str) -> Self {
        self.with_location(DecodingErrorLocation::Other(other))
    }

    pub fn kind(&self) -> &DecodingErrorDetails {
        &self.kind
    }

    pub fn location(&self) -> Option<&DecodingErrorLocation> {
        self.location.as_ref()
    }
}

impl From<DecodingErrorDetails> for DecodingError {
    fn from(value: DecodingErrorDetails) -> Self {
        Self::new(value)
    }
}

/// Details of [`DecodingError`].
#[non_exhaustive]
#[derive(Debug, thiserror::Error)]
pub enum DecodingErrorDetails {
    /// Unknown response code.
    #[error("unknown response code: {0}")]
    UnknownResponseCode(u32),
    /// Certain key missing in response.
    #[error("Missing key in response: {0}")]
    MissingKey(&'static str),
    /// Value have different type than expected for that key or field.
    #[error("Type mismatch, expected '{expected}', actual '{actual}'")]
    TypeMismatch {
        expected: &'static str,
        actual: Cow<'static, str>,
    },

    /// Error while deserializing [`rmpv::Value`] into concrete type.
    #[error("Failed to deserialize rmpv::Value")]
    Serde(#[source] rmpv::ext::Error),
    /// Error while decoding data from MessagePack format.
    #[error("Failed to decode data from MessagePack")]
    MessagePack(#[source] anyhow::Error),
}

impl From<ValueReadError> for DecodingError {
    fn from(v: ValueReadError) -> Self {
        DecodingErrorDetails::MessagePack(v.into()).into()
    }
}

impl From<rmpv::decode::Error> for DecodingError {
    fn from(v: rmpv::decode::Error) -> Self {
        DecodingErrorDetails::MessagePack(v.into()).into()
    }
}

impl From<rmpv::ext::Error> for DecodingError {
    fn from(v: rmpv::ext::Error) -> Self {
        DecodingErrorDetails::Serde(v).into()
    }
}

impl From<NumValueReadError> for DecodingError {
    fn from(v: NumValueReadError) -> Self {
        DecodingErrorDetails::MessagePack(v.into()).into()
    }
}

impl From<MarkerReadError> for DecodingError {
    fn from(v: MarkerReadError) -> Self {
        DecodingErrorDetails::MessagePack(v.0.into()).into()
    }
}

#[derive(Clone, Debug)]
pub enum DecodingErrorLocation {
    Key(&'static str),
    FrameLengthField,
    Other(&'static str),
}

impl fmt::Display for DecodingErrorLocation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            DecodingErrorLocation::Key(x) => write!(f, "key '{x}'"),
            DecodingErrorLocation::FrameLengthField => write!(f, "frame length field"),
            DecodingErrorLocation::Other(x) => write!(f, "{x}"),
        }
    }
}

impl DecodingErrorLocation {
    fn display_in_error(value: &Option<Self>) -> String {
        if let Some(x) = value {
            format!(" (in {x})")
        } else {
            String::new()
        }
    }
}

/// Helper type to return errors from decoder.
#[derive(Clone)]
pub(crate) enum CodecDecodeError {
    Io(Arc<tokio::io::Error>),
    Closed,
    Decode(DecodingError),
}

impl From<tokio::io::Error> for CodecDecodeError {
    fn from(v: tokio::io::Error) -> Self {
        Self::Io(Arc::new(v))
    }
}

/// Helper type to return errors from encoder.
#[derive(Debug)]
pub(crate) enum CodecEncodeError {
    Io(tokio::io::Error),
    Encode(EncodingError),
}

impl From<tokio::io::Error> for CodecEncodeError {
    fn from(v: tokio::io::Error) -> Self {
        Self::Io(v)
    }
}

// #[derive(Debug, thiserror::Error)]
// pub enum Error {
//     #[error("Error while encoding response body into MessagePack: {0}")]
//     RequestBodyEncode(#[source] anyhow::Error),
//     #[error("Error response: {0}")]
//     Response(#[from] ErrorResponse),
//     #[error("Error while decoding response body: {0}")]
//     ResponseBodyDecode(#[source] anyhow::Error),
//     #[error("Serde deserialization error: {0}")]
//     SerdeDeserialize(#[from] rmpv::ext::Error),
//     #[error("Transport error: {0}")]
//     Transport(#[from] Arc<TransportError>),

//     #[error("Space not found")]
//     SpaceNotFound,
//     #[error("Failed to metadata: {0}")]
//     MetadataLoad(#[source] anyhow::Error),
// }

// impl From<TransportError> for Error {
//     fn from(value: TransportError) -> Self {
//         Error::Transport(Arc::new(value))
//     }
// }

// /// Errors related to low-level interaction with Tarantool (TCP or MessagePack).
// #[derive(Debug, thiserror::Error)]
// pub enum TransportError {
//     #[error("Duplicated sync '{0}'")]
//     DuplicatedSync(u32),
//     #[error("Underlying connection error: {0}")]
//     Connection(#[from] tokio::io::Error),
//     #[error("MessagePack encoding error: {0}")]
//     MessagePackEncode(#[source] anyhow::Error),
//     #[error("MessagePack decoding error: {0}")]
//     MessagePackDecode(#[source] anyhow::Error),
//     #[error("Underlying connection closed")]
//     ConnectionClosed,
// }

// impl<E> From<ValueWriteError<E>> for TransportError
// where
//     E: RmpWriteErr + Send + Sync,
// {
//     fn from(v: ValueWriteError<E>) -> Self {
//         Self::MessagePackEncode(v.into())
//     }
// }

// impl From<ValueReadError> for TransportError {
//     fn from(v: ValueReadError) -> Self {
//         Self::MessagePackDecode(v.into())
//     }
// }

// impl From<rmpv::decode::Error> for TransportError {
//     fn from(v: rmpv::decode::Error) -> Self {
//         Self::MessagePackDecode(v.into())
//     }
// }

// impl From<NumValueReadError> for TransportError {
//     fn from(v: NumValueReadError) -> Self {
//         Self::MessagePackDecode(v.into())
//     }
// }

// // impl From<DecodeStringError<'_>> for TransportError {
// //     fn from(v: DecodeStringError<'_>) -> Self {
// //         Self::MessagePackDecode(anyhow!("{}", v))
// //     }
// // }

// impl From<MarkerReadError> for TransportError {
//     fn from(v: MarkerReadError) -> Self {
//         Self::MessagePackDecode(v.0.into())
//     }
// }