use crate::protocol::Error;
#[derive(Debug)]
pub enum Failure {
Refused(Error),
Broken(anyhow::Error),
}
impl Failure {
pub fn code(&self) -> Option<i64> {
match self {
Failure::Refused(error) => Some(error.code),
Failure::Broken(_) => None,
}
}
pub(crate) fn broken(what: impl Into<String>) -> Failure {
Failure::Broken(anyhow::Error::msg(what.into()))
}
}
impl std::fmt::Display for Failure {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Failure::Refused(error) => write!(f, "the console server refused: {error}"),
Failure::Broken(e) => write!(f, "the console channel broke: {e}"),
}
}
}
impl std::error::Error for Failure {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Failure::Refused(error) => Some(error),
Failure::Broken(e) => Some(e.as_ref()),
}
}
}
impl From<Error> for Failure {
fn from(error: Error) -> Failure {
Failure::Refused(error)
}
}