use std::borrow::Cow;
use thiserror::Error;
use tor_error::{ErrorKind, HasKind};
#[derive(Clone, Error, Debug)]
#[non_exhaustive]
pub enum Error {
#[error("SOCKS protocol syntax violation")]
Syntax,
#[error("Error decoding SOCKS message")]
Decode(#[from] tor_bytes::Error),
#[error("Unrecognized SOCKS protocol version {0}")]
BadProtocol(u8),
#[error("SOCKS feature ({0}) not implemented")]
NotImplemented(Cow<'static, str>),
#[error("SOCKS handshake was finished; no need to call this again")]
AlreadyFinished(tor_error::Bug),
#[error("SOCKS Authentication failed")]
AuthRejected,
#[error("SOCKS protocol message size limit {limit} exceeded")]
MessageTooLong {
limit: usize,
},
#[error("peer closed connection during SOCKS handshake")]
UnexpectedEof,
#[error("SOCKS peer inappropriately pipelined (optimistically sent) payload data")]
ForbiddenPipelining,
#[error("Bug while handling SOCKS handshake")]
Bug(#[from] tor_error::Bug),
}
impl HasKind for Error {
fn kind(&self) -> ErrorKind {
use Error as E;
use ErrorKind as EK;
match self {
E::Decode(tor_bytes::Error::Incomplete { .. }) => {
EK::Internal
}
E::Syntax | E::Decode(_) | E::BadProtocol(_) => EK::LocalProtocolViolation,
E::NotImplemented(_) => EK::NotImplemented,
E::AuthRejected => EK::LocalProtocolViolation,
E::UnexpectedEof => EK::LocalProtocolViolation,
E::ForbiddenPipelining => EK::LocalProtocolViolation,
E::MessageTooLong { .. } => EK::Internal, E::AlreadyFinished(e) => e.kind(),
E::Bug(e) => e.kind(),
}
}
}