Skip to main content

dicedb_rs/
errors.rs

1//! # Error Module
2//! This module contains the error types for the client and the server.
3//! The error types are used to handle errors that occur during the execution of the client and
4//! server.
5use crate::commands::ScalarValue;
6use prost::DecodeError;
7use std::io::Error;
8
9/// The errors that originates from handling commands.
10#[derive(Debug)]
11pub enum CommandError {
12    /// A server side error occured. This might be caused by a bug in the SDK, or uninteded usage.
13    ServerError(String),
14    /// The server returned an unexpected response, this can be caused by running on an
15    /// incompatible server version.
16    DecodeError(DecodeError),
17    /// The server returned an unexpected watch response, this can be caused by running on an
18    /// incompatible server version.
19    WatchValueExpectationError(String),
20}
21
22/// The errors that originates from the command stream.
23#[derive(Debug)]
24pub enum CommandStreamError {
25    /// An error occured while reading from the stream. This is caused by the underlying IO to the
26    /// server. Connection to server could be lost, or the server could have closed the connection.
27    ReadError(Error),
28    /// An error occured while decoding the response from the server. This can be caused by an
29    /// incompatible server version.
30    DecodeError(prost::DecodeError),
31    /// An unexpected value was received from the server during handshake. This can be caused by
32    /// incompatible server version.
33    HandshakeError(ScalarValue),
34    /// An error occured in the command stream, this can be caused by an unexpected response from
35    /// the server.
36    CommandError(String),
37}
38
39impl From<Error> for CommandStreamError {
40    fn from(error: Error) -> Self {
41        CommandStreamError::ReadError(error)
42    }
43}
44impl From<prost::DecodeError> for CommandStreamError {
45    fn from(error: prost::DecodeError) -> Self {
46        CommandStreamError::DecodeError(error)
47    }
48}
49
50/// The errors that originates from the Client.
51#[derive(Debug)]
52pub enum ClientError {
53    /// An error occured with the command stream
54    CommandStreamError(CommandStreamError),
55    /// An error occured with the watch stream
56    WatchStreamError(WatchStreamError),
57    /// An error occured in the clients stream
58    StreamError(StreamError),
59}
60
61impl From<CommandStreamError> for ClientError {
62    fn from(error: CommandStreamError) -> Self {
63        ClientError::CommandStreamError(error)
64    }
65}
66
67impl From<WatchStreamError> for ClientError {
68    fn from(error: WatchStreamError) -> Self {
69        ClientError::WatchStreamError(error)
70    }
71}
72
73impl From<StreamError> for ClientError {
74    fn from(error: StreamError) -> Self {
75        ClientError::StreamError(error)
76    }
77}
78
79/// The errors that originates from base functionality of a stream, either command stream or watch
80/// stream.
81#[derive(Debug)]
82pub enum StreamError {
83    /// An error occured with the IO, this could be caused by the underlying IO to the server.
84    /// Connection to server could be lost, or the server could have closed the connection.
85    IoError(Error),
86    /// An error occured while decoding the response from the server. This can be caused by an
87    /// incompatible server version.
88    DecodeError(DecodeError),
89    /// An error occured while handling a command.
90    /// This can be caused by an unexpected response from the server.
91    CommandError(CommandError),
92}
93
94impl From<Error> for StreamError {
95    fn from(error: Error) -> Self {
96        StreamError::IoError(error)
97    }
98}
99
100impl From<DecodeError> for StreamError {
101    fn from(error: DecodeError) -> Self {
102        StreamError::DecodeError(error)
103    }
104}
105impl From<CommandError> for StreamError {
106    fn from(error: CommandError) -> Self {
107        StreamError::CommandError(error)
108    }
109}
110
111/// The errors that originates from the watch stream.
112#[derive(Debug)]
113pub enum WatchStreamError {
114    /// An error occured with the IO, this could be caused by the underlying IO to the server.
115    /// Connection to server could be lost, or the server could have closed the connection.
116    IoError(Error),
117    /// An error occured while decoding the response from the server. This can be caused by an
118    /// incompatible server version.
119    UnexpectedResponse(ScalarValue),
120    /// An error occured while handling a command.
121    StreamError(StreamError),
122}
123
124impl From<Error> for WatchStreamError {
125    fn from(error: Error) -> Self {
126        WatchStreamError::IoError(error)
127    }
128}
129
130impl From<StreamError> for WatchStreamError {
131    fn from(error: StreamError) -> Self {
132        WatchStreamError::StreamError(error)
133    }
134}