use thiserror::Error;
use tor_error::{ErrorKind, HasKind};
#[derive(Error, Debug, Clone)]
#[non_exhaustive]
pub enum Error {
#[error("Error while parsing {parsed}: {err}")]
BytesErr {
#[source]
err: tor_bytes::Error,
parsed: &'static str,
},
#[error("Error while encoding message")]
EncodeErr(#[from] tor_bytes::EncodeError),
#[error("Internal programming error")]
Internal(tor_error::Bug),
#[error("Channel protocol violation: {0}")]
ChanProto(String),
#[error("Circuit protocol violation: {0}")]
CircProto(String),
#[error("Invalid stream target address")]
BadStreamAddress,
#[error("Message can't be represented in a Tor cell: {0}")]
CantEncode(&'static str),
}
impl HasKind for Error {
fn kind(&self) -> ErrorKind {
use Error as E;
use ErrorKind as EK;
match self {
E::EncodeErr(..) => EK::BadApiUsage,
E::BytesErr { .. } => EK::TorProtocolViolation,
E::Internal(_) => EK::Internal,
E::ChanProto(_) => EK::TorProtocolViolation,
E::CircProto(_) => EK::TorProtocolViolation,
E::BadStreamAddress => EK::BadApiUsage,
E::CantEncode(_) => EK::Internal,
}
}
}