use crate::channels::ChannelMode;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
ConnectToServer,
WriteToStream,
ReadStream,
SwitchMode,
RunCommand,
QueryResponse(&'static str),
WrongResponse,
UnsupportedCommand((&'static str, Option<ChannelMode>)),
SonicServer(String),
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use Error::*;
match self {
ConnectToServer => f.write_str("Cannot connect to server"),
WriteToStream => f.write_str("Cannot write data to stream"),
ReadStream => f.write_str("Cannot read sonic response from stream"),
SwitchMode => f.write_str("Cannot switch channel mode"),
RunCommand => f.write_str("Cannot run command in current mode"),
QueryResponse(message) => {
write!(f, "Error in query response: {}", message)
}
WrongResponse => {
write!(f, "Client cannot parse response from sonic server. Please write an issue to github (https://github.com/pleshevskiy/sonic-channel).")
}
UnsupportedCommand((command_name, channel_mode)) => {
if let Some(channel_mode) = channel_mode {
write!(
f,
"You cannot use `{}` command in {} sonic channel mode",
command_name, channel_mode
)
} else {
write!(
f,
"You need to connect to sonic channel before use {} command",
command_name
)
}
}
SonicServer(message) => write!(f, "Sonic Server-side error: {}", message),
}
}
}
impl std::error::Error for Error {}