use super::line_parser::error::{ParseError, ParseErrorKind};
use crate::BoxError;
use derive_more::Display;
use thiserror::Error;
#[derive(Debug, Error)]
#[error("control channel error: {kind}")]
pub struct ControlChanError {
kind: ControlChanErrorKind,
#[source]
source: Option<BoxError>,
}
#[derive(Eq, PartialEq, Debug, Display)]
#[allow(dead_code)]
pub enum ControlChanErrorKind {
#[display("Failed to perform IO")]
IoError,
#[display("Failed to parse command")]
ParseError,
#[display("Internal Server Error")]
InternalServerError,
#[display("Something went wrong when trying to authenticate")]
AuthenticationError,
#[display("Failed to map event from data channel")]
InternalMsgError,
#[display("Non-UTF8 character in command")]
Utf8Error,
#[display("Unknown command: {}", command)]
UnknownCommand {
command: String,
},
#[display("Invalid command (invalid parameter)")]
InvalidCommand,
#[display("Encountered read timeout on the control channel")]
ControlChannelTimeout,
#[display("Control channel in illegal state")]
IllegalState,
}
impl ControlChanError {
pub fn new(kind: ControlChanErrorKind) -> Self {
ControlChanError { kind, source: None }
}
#[allow(unused)]
pub fn kind(&self) -> &ControlChanErrorKind {
&self.kind
}
}
impl From<ControlChanErrorKind> for ControlChanError {
fn from(kind: ControlChanErrorKind) -> ControlChanError {
ControlChanError { kind, source: None }
}
}
impl From<std::io::Error> for ControlChanError {
fn from(err: std::io::Error) -> ControlChanError {
ControlChanError {
kind: ControlChanErrorKind::IoError,
source: Some(Box::new(err)),
}
}
}
impl From<std::str::Utf8Error> for ControlChanError {
fn from(err: std::str::Utf8Error) -> ControlChanError {
ControlChanError {
kind: ControlChanErrorKind::Utf8Error,
source: Some(Box::new(err)),
}
}
}
impl From<ParseError> for ControlChanError {
fn from(err: ParseError) -> ControlChanError {
let kind: ControlChanErrorKind = match err.kind().clone() {
ParseErrorKind::InvalidUtf8 => ControlChanErrorKind::Utf8Error,
ParseErrorKind::InvalidCommand => ControlChanErrorKind::InvalidCommand,
_ => ControlChanErrorKind::InvalidCommand,
};
ControlChanError {
kind,
source: Some(Box::new(err)),
}
}
}