Skip to main content

str0m_proto/crypto/
error.rs

1use std::error::Error;
2use std::fmt;
3use std::io;
4
5use crate::crypto::dtls::DtlsImplError;
6
7/// Errors that can arise in DTLS.
8#[derive(Debug)]
9pub enum CryptoError {
10    /// Some error from OpenSSL layer (used for DTLS).
11    #[cfg(feature = "openssl")]
12    OpenSsl(openssl::error::ErrorStack),
13
14    /// Error from DTLS implementation layer.
15    DtlsImpl(DtlsImplError),
16
17    /// Other IO errors.
18    Io(io::Error),
19
20    /// Other errors.
21    Other(String),
22}
23
24impl fmt::Display for CryptoError {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        match self {
27            #[cfg(feature = "openssl")]
28            CryptoError::OpenSsl(err) => write!(f, "{}", err),
29            CryptoError::Io(err) => write!(f, "{}", err),
30            CryptoError::DtlsImpl(err) => write!(f, "{}", err),
31            CryptoError::Other(err) => write!(f, "{}", err),
32        }
33    }
34}
35
36impl Error for CryptoError {
37    fn source(&self) -> Option<&(dyn Error + 'static)> {
38        match self {
39            #[cfg(feature = "openssl")]
40            CryptoError::OpenSsl(err) => Some(err),
41            CryptoError::Io(err) => Some(err),
42            CryptoError::DtlsImpl(err) => Some(err),
43            CryptoError::Other(_) => None,
44        }
45    }
46}
47
48#[cfg(feature = "openssl")]
49impl From<openssl::error::ErrorStack> for CryptoError {
50    fn from(err: openssl::error::ErrorStack) -> Self {
51        CryptoError::OpenSsl(err)
52    }
53}
54
55impl From<io::Error> for CryptoError {
56    fn from(err: io::Error) -> Self {
57        CryptoError::Io(err)
58    }
59}
60
61impl From<DtlsImplError> for CryptoError {
62    fn from(err: DtlsImplError) -> Self {
63        CryptoError::DtlsImpl(err)
64    }
65}
66
67/// Errors that can arise in DTLS.
68#[derive(Debug)]
69pub enum DtlsError {
70    /// Error arising in the crypto
71    CryptoError(CryptoError),
72
73    /// Other IO errors.
74    Io(io::Error),
75}
76
77impl fmt::Display for DtlsError {
78    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
79        match self {
80            DtlsError::CryptoError(err) => write!(f, "{}", err),
81            DtlsError::Io(err) => write!(f, "{}", err),
82        }
83    }
84}
85
86impl Error for DtlsError {
87    fn source(&self) -> Option<&(dyn Error + 'static)> {
88        match self {
89            DtlsError::CryptoError(err) => Some(err),
90            DtlsError::Io(err) => Some(err),
91        }
92    }
93}
94
95impl From<io::Error> for DtlsError {
96    fn from(err: io::Error) -> Self {
97        DtlsError::Io(err)
98    }
99}
100
101impl From<CryptoError> for DtlsError {
102    fn from(err: CryptoError) -> Self {
103        DtlsError::CryptoError(err)
104    }
105}