2020-07-19 10:17:53 +03:00
|
|
|
use std::error::Error as StdError;
|
|
|
|
use std::fmt;
|
|
|
|
|
2020-07-26 23:36:07 +03:00
|
|
|
/// Sugar if you expect only sonic-channel error type in result
|
2020-07-19 10:17:53 +03:00
|
|
|
pub type Result<T> = std::result::Result<T, Error>;
|
|
|
|
|
2020-07-26 23:36:07 +03:00
|
|
|
/// Wrap for sonic channel error kind. This type has std::error::Error
|
|
|
|
/// implementation and you can use boxed trait for catch other errors
|
|
|
|
/// like this.
|
2020-07-19 10:17:53 +03:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Error {
|
|
|
|
kind: ErrorKind,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl StdError for Error {}
|
|
|
|
|
|
|
|
impl Error {
|
2020-07-26 23:36:07 +03:00
|
|
|
/// Creates new Error with sonic channel error kind
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use sonic_channel::result::*;
|
|
|
|
///
|
|
|
|
/// let err = Error::new(ErrorKind::ConnectToServer);
|
|
|
|
/// ```
|
2020-07-19 10:17:53 +03:00
|
|
|
pub fn new(kind: ErrorKind) -> Self {
|
|
|
|
Error { kind }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-26 23:36:07 +03:00
|
|
|
/// All error kinds that you can see in sonic-channel crate.
|
2020-07-19 10:17:53 +03:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ErrorKind {
|
2020-07-26 23:36:07 +03:00
|
|
|
/// Cannot connect to the sonic search backend.
|
2020-07-19 10:17:53 +03:00
|
|
|
ConnectToServer,
|
2020-07-26 23:36:07 +03:00
|
|
|
|
|
|
|
/// Cannot write message to stream.
|
2020-07-19 10:17:53 +03:00
|
|
|
WriteToStream,
|
2020-07-26 23:36:07 +03:00
|
|
|
|
|
|
|
/// Cannot read message in stream.
|
2020-07-19 10:17:53 +03:00
|
|
|
ReadStream,
|
2020-07-26 23:36:07 +03:00
|
|
|
|
|
|
|
/// Cannot switch channel mode from uninitialized.
|
2020-07-19 10:17:53 +03:00
|
|
|
SwitchMode,
|
2020-07-26 23:36:07 +03:00
|
|
|
|
|
|
|
/// Cannot run command in current mode.
|
2020-07-19 10:17:53 +03:00
|
|
|
RunCommand,
|
2020-07-26 23:36:07 +03:00
|
|
|
|
|
|
|
/// Error in query response with additional message.
|
2020-07-19 10:17:53 +03:00
|
|
|
QueryResponseError(&'static str),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::result::Result<(), fmt::Error> {
|
|
|
|
match self.kind {
|
|
|
|
ErrorKind::ConnectToServer => write!(f, "Cannot connect to server"),
|
|
|
|
ErrorKind::WriteToStream => write!(f, "Cannot write data to stream"),
|
|
|
|
ErrorKind::ReadStream => write!(f, "Cannot read sonic response from stream"),
|
|
|
|
ErrorKind::SwitchMode => write!(f, "Cannot switch channel mode"),
|
|
|
|
ErrorKind::RunCommand => write!(f, "Cannot run command in current mode"),
|
|
|
|
ErrorKind::QueryResponseError(message) => {
|
|
|
|
write!(f, "Error in query response: {}", message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|