1
//! Define an error type for the tor-cell crate.
2
use thiserror::Error;
3
use tor_error::{ErrorKind, HasKind};
4

            
5
/// An error type for the tor-cell crate.
6
///
7
/// This type should probably be split into several.  There's more
8
/// than one kind of error that can occur while doing something with
9
/// tor cells.
10
88
#[derive(Error, Debug, Clone)]
11
#[non_exhaustive]
12
pub enum Error {
13
    /// An error that occurred in the tor_bytes crate while decoding an
14
    /// object.
15
    #[error("parsing error: {0}")]
16
    BytesErr(#[from] tor_bytes::Error),
17
    /// There was a programming error somewhere in the code.
18
    #[error("Internal programming error: {0}")]
19
    Internal(tor_error::Bug),
20
    /// Protocol violation at the channel level
21
    #[error("channel protocol violation: {0}")]
22
    ChanProto(String),
23
    /// Tried to make or use a stream to an invalid destination address.
24
    #[error("invalid stream target address")]
25
    BadStreamAddress,
26
    /// Tried to construct a message that Tor can't represent.
27
    #[error("Message can't be represented in a Tor cell.")]
28
    CantEncode,
29
}
30

            
31
impl HasKind for Error {
32
    fn kind(&self) -> ErrorKind {
33
        use tor_bytes::Error as ByE;
34
        use Error as E;
35
        use ErrorKind as EK;
36
        match self {
37
            E::BytesErr(ByE::Truncated) => EK::Internal,
38
            E::BytesErr(_) => EK::TorProtocolViolation,
39
            E::Internal(_) => EK::Internal,
40
            E::ChanProto(_) => EK::TorProtocolViolation,
41
            E::BadStreamAddress => EK::BadApiUsage,
42
            E::CantEncode => EK::Internal,
43
        }
44
    }
45
}