1use crate::channels::ChannelMode;
2
3pub type Result<T> = std::result::Result<T, Error>;
5
6#[derive(Debug)]
12pub enum Error {
13 ConnectToServer,
15
16 WriteToStream,
18
19 ReadStream,
21
22 SwitchMode,
24
25 RunCommand,
27
28 QueryResponse(&'static str),
30
31 WrongResponse,
34
35 UnsupportedCommand((&'static str, Option<ChannelMode>)),
37
38 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 {}