psp_net/socket/
error.rs

1use alloc::string::String;
2use thiserror::Error;
3
4/// An error that can occur with a socket
5#[derive(Debug, Clone, PartialEq, Eq, Default, Error)]
6pub enum SocketError {
7    /// Unsupported address family
8    #[error("Unsupported address family")]
9    UnsupportedAddressFamily,
10    /// Socket error with errno
11    #[error("Errno: {0}")]
12    Errno(i32),
13    /// Socket error with errno and a description
14    #[error("Errno: {0} ({1})")]
15    ErrnoWithDescription(i32, String),
16    /// Other error
17    #[error("{0}")]
18    Other(String),
19    /// Unknown error
20    #[default]
21    #[error("Unknown error")]
22    Unknown,
23}
24
25impl SocketError {
26    /// Create a new [`SocketError::ErrnoWithDescription`]
27    #[must_use]
28    pub fn new_errno_with_description<S>(errno: i32, description: S) -> Self
29    where
30        S: Into<String>,
31    {
32        SocketError::ErrnoWithDescription(errno, description.into())
33    }
34}
35
36impl embedded_io::Error for SocketError {
37    fn kind(&self) -> embedded_io::ErrorKind {
38        match self {
39            SocketError::UnsupportedAddressFamily => embedded_io::ErrorKind::Unsupported,
40            _ => embedded_io::ErrorKind::Other,
41        }
42    }
43}
44
45/// An error that can occur with a TLS socket.
46///
47/// It can either be a [`TlsError`] or a [`SocketError`] from the
48/// underlying socket.
49#[derive(Debug, Clone, Error)]
50pub enum TlsSocketError {
51    /// TLS error
52    #[error("TLS error: {}", 0)]
53    TlsError(TlsError),
54    /// An error with the under
55    #[error("Socket error: {0}")]
56    SocketError(#[from] SocketError),
57}
58
59impl TlsSocketError {
60    /// Returns `true` if the error is a [`TlsError`].
61    ///
62    /// [`TlsError`]: TlsSocketError::TlsError
63    #[must_use]
64    pub fn is_tls_error(&self) -> bool {
65        matches!(self, Self::TlsError(..))
66    }
67
68    /// Returns `true` if the error is a [`SocketError`].
69    ///
70    /// [`SocketError`]: TlsSocketError::SocketError
71    #[must_use]
72    pub fn is_socket_error(&self) -> bool {
73        matches!(self, Self::SocketError(..))
74    }
75}
76
77impl From<TlsError> for TlsSocketError {
78    fn from(value: TlsError) -> Self {
79        TlsSocketError::TlsError(value)
80    }
81}
82
83impl embedded_io::Error for TlsSocketError {
84    fn kind(&self) -> embedded_io::ErrorKind {
85        match self {
86            TlsSocketError::TlsError(tls_error) => tls_error.kind(),
87            TlsSocketError::SocketError(socket_error) => socket_error.kind(),
88        }
89    }
90}
91
92// re-exports
93pub type TlsError = embedded_tls::TlsError;