Skip to main content

nexus_net/tls/
error.rs

1use std::fmt;
2
3/// TLS operation error.
4#[derive(Debug)]
5pub enum TlsError {
6    /// rustls protocol error (handshake failure, certificate error, etc.)
7    Rustls(rustls::Error),
8    /// I/O error during TLS operations.
9    Io(std::io::Error),
10    /// Invalid hostname for SNI.
11    InvalidHostname(String),
12    /// No system root certificates found.
13    NoRootCerts,
14}
15
16impl fmt::Display for TlsError {
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        match self {
19            Self::Rustls(e) => write!(f, "TLS error: {e}"),
20            Self::Io(e) => write!(f, "TLS I/O error: {e}"),
21            Self::InvalidHostname(h) => write!(f, "invalid TLS hostname: {h}"),
22            Self::NoRootCerts => write!(f, "no system root certificates found"),
23        }
24    }
25}
26
27impl std::error::Error for TlsError {
28    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
29        match self {
30            Self::Rustls(e) => Some(e),
31            Self::Io(e) => Some(e),
32            _ => None,
33        }
34    }
35}
36
37impl From<rustls::Error> for TlsError {
38    fn from(e: rustls::Error) -> Self {
39        Self::Rustls(e)
40    }
41}
42
43impl From<std::io::Error> for TlsError {
44    fn from(e: std::io::Error) -> Self {
45        Self::Io(e)
46    }
47}