sonic_channel2/
result.rs

1use crate::channels::ChannelMode;
2
3/// Sugar if you expect only sonic-channel error type in result
4pub type Result<T> = std::result::Result<T, Error>;
5
6/// Wrap for sonic channel error kind. This type has std::error::Error
7/// implementation and you can use boxed trait for catch other errors
8/// like this.
9
10/// All error kinds that you can see in sonic-channel crate.
11#[derive(Debug)]
12pub enum Error {
13    /// Cannot connect to the sonic search backend.
14    ConnectToServer,
15
16    /// Cannot write message to stream.
17    WriteToStream,
18
19    /// Cannot read message in stream.
20    ReadStream,
21
22    /// Cannot switch channel mode from uninitialized.
23    SwitchMode,
24
25    /// Cannot run command in current mode.
26    RunCommand,
27
28    /// Error in query response with additional message.
29    QueryResponse(&'static str),
30
31    /// Response from sonic server are wrong! Actually it may happen if you use
32    /// unsupported sonic backend version. Please write issue to the github repo.
33    WrongResponse,
34
35    /// You cannot run the command in current channel.
36    UnsupportedCommand((&'static str, Option<ChannelMode>)),
37
38    /// This error appears if the error occurred on the server side
39    SonicServer(String),
40}
41
42impl std::fmt::Display for Error {
43    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44        use Error::*;
45        match self {
46            ConnectToServer => f.write_str("Cannot connect to server"),
47            WriteToStream => f.write_str("Cannot write data to stream"),
48            ReadStream => f.write_str("Cannot read sonic response from stream"),
49            SwitchMode => f.write_str("Cannot switch channel mode"),
50            RunCommand => f.write_str("Cannot run command in current mode"),
51            QueryResponse(message) => {
52                write!(f, "Error in query response: {}", message)
53            }
54            WrongResponse => {
55                write!(f, "Client cannot parse response from sonic server. Please write an issue to github (https://github.com/pleshevskiy/sonic-channel).")
56            }
57            UnsupportedCommand((command_name, channel_mode)) => {
58                if let Some(channel_mode) = channel_mode {
59                    write!(
60                        f,
61                        "You cannot use `{}` command in {} sonic channel mode",
62                        command_name, channel_mode
63                    )
64                } else {
65                    write!(
66                        f,
67                        "You need to connect to sonic channel before use {} command",
68                        command_name
69                    )
70                }
71            }
72            SonicServer(message) => write!(f, "Sonic Server-side error: {}", message),
73        }
74    }
75}
76
77impl std::error::Error for Error {}