Skip to main content

ax_net/
error.rs

1use ax_io::IoError;
2
3/// Errors owned by the network and socket domain.
4#[derive(Clone, Copy, Debug, Eq, PartialEq, thiserror::Error)]
5pub enum NetError {
6    /// The requested local address is already bound.
7    #[error("network address is already in use")]
8    AddrInUse,
9    /// The socket already has an established peer.
10    #[error("socket is already connected")]
11    AlreadyConnected,
12    /// The requested network object already exists.
13    #[error("network object already exists")]
14    AlreadyExists,
15    /// The network object cannot perform the operation in its current state.
16    #[error("network object is in an invalid state")]
17    BadState,
18    /// A user or filesystem address used by the socket cannot be accessed.
19    #[error("bad network address")]
20    BadAddress,
21    /// A descriptor used by the network operation is invalid.
22    #[error("bad network file descriptor")]
23    BadFileDescriptor,
24    /// The peer has closed the write side of a stream.
25    #[error("network stream is broken")]
26    BrokenPipe,
27    /// The destination rejected the connection attempt.
28    #[error("connection was refused")]
29    ConnectionRefused,
30    /// The peer reset an established connection.
31    #[error("connection was reset")]
32    ConnectionReset,
33    /// A path-backed socket operation would cross filesystem devices.
34    #[error("socket path operation crosses devices")]
35    CrossesDevices,
36    /// A directory involved in a socket path operation is not empty.
37    #[error("socket path directory is not empty")]
38    DirectoryNotEmpty,
39    /// A datagram operation requires a destination address.
40    #[error("destination address is required")]
41    DestAddrRequired,
42    /// Socket-path traversal encountered a filesystem loop.
43    #[error("socket path traversal loop detected")]
44    FilesystemLoop,
45    /// A path-backed socket object exceeds the supported filesystem limit.
46    #[error("socket path object is too large")]
47    FileTooLarge,
48    /// A nonblocking connection attempt is still in progress.
49    #[error("connection is in progress")]
50    InProgress,
51    /// The network operation was interrupted before completion.
52    #[error("network operation was interrupted")]
53    Interrupted,
54    /// A socket address, option, or operation argument is invalid.
55    #[error("invalid network input")]
56    InvalidInput,
57    /// A path-backed socket object contains invalid filesystem data.
58    #[error("socket path data is invalid")]
59    InvalidData,
60    /// A socket path unexpectedly refers to a directory.
61    #[error("socket path is a directory")]
62    IsADirectory,
63    /// The packet or message exceeds the transport limit.
64    #[error("network message is too long")]
65    MessageTooLong,
66    /// A socket path component exceeds the filesystem name limit.
67    #[error("socket path name is too long")]
68    NameTooLong,
69    /// Memory required by the network operation could not be allocated.
70    #[error("network allocation failed")]
71    NoMemory,
72    /// The requested interface does not exist.
73    #[error("network device does not exist")]
74    NoSuchDevice,
75    /// No interface owns the address, or no route reaches the destination.
76    #[error("network device or address does not exist")]
77    NoSuchDeviceOrAddress,
78    /// The socket has no connected peer.
79    #[error("socket is not connected")]
80    NotConnected,
81    /// The requested network object was not found.
82    #[error("network object was not found")]
83    NotFound,
84    /// A socket path component expected to be a directory is not one.
85    #[error("socket path component is not a directory")]
86    NotADirectory,
87    /// The selected object is not a socket.
88    #[error("object is not a socket")]
89    NotASocket,
90    /// The selected socket object is not a terminal.
91    #[error("socket object is not a tty")]
92    NotATty,
93    /// The caller is not permitted to change the requested network state.
94    #[error("network operation is not permitted")]
95    OperationNotPermitted,
96    /// The socket or transport does not implement the requested operation.
97    #[error("network operation is not supported")]
98    OperationNotSupported,
99    /// The address belongs to a family unsupported by this socket.
100    #[error("socket address family is not supported")]
101    AddressFamilyUnsupported,
102    /// The socket does not recognize the requested protocol option.
103    #[error("socket protocol option is not supported")]
104    ProtocolOptionUnsupported,
105    /// Filesystem permissions deny access to a path-backed socket.
106    #[error("socket path permission denied")]
107    PermissionDenied,
108    /// A path-backed socket operation attempted to modify a read-only filesystem.
109    #[error("socket path filesystem is read-only")]
110    ReadOnlyFilesystem,
111    /// A queue or network resource is currently busy.
112    #[error("network resource is busy")]
113    ResourceBusy,
114    /// Storage used by a path-backed socket has no free space.
115    #[error("socket path storage is full")]
116    StorageFull,
117    /// The network operation did not complete before its deadline.
118    #[error("network operation timed out")]
119    TimedOut,
120    /// The requested network capability is unavailable in this build.
121    #[error("network capability is unavailable")]
122    Unsupported,
123    /// A secured wireless connection cannot obtain trusted runtime entropy.
124    #[error("trusted wireless connection entropy is unavailable")]
125    EntropyUnavailable,
126    /// The operation cannot complete without blocking.
127    #[error("network operation would block")]
128    WouldBlock,
129    /// A caller-provided I/O buffer failed while transferring packet data.
130    #[error(transparent)]
131    Io(#[from] IoError),
132    /// A network backend or path-backed namespace reported an I/O failure.
133    #[error("network backend I/O failed")]
134    BackendIo,
135}
136
137/// A result returned by the network and socket domain.
138pub type NetResult<T = ()> = Result<T, NetError>;
139
140impl From<NetError> for IoError {
141    fn from(error: NetError) -> Self {
142        match error {
143            NetError::AddrInUse => Self::AddrInUse,
144            NetError::AlreadyConnected => Self::AlreadyConnected,
145            NetError::AlreadyExists => Self::AlreadyExists,
146            NetError::BadState => Self::BadState,
147            NetError::BadAddress => Self::BadAddress,
148            NetError::BadFileDescriptor => Self::BadFileDescriptor,
149            NetError::BrokenPipe => Self::BrokenPipe,
150            NetError::ConnectionRefused => Self::ConnectionRefused,
151            NetError::ConnectionReset => Self::ConnectionReset,
152            NetError::CrossesDevices => Self::CrossesDevices,
153            NetError::DirectoryNotEmpty => Self::DirectoryNotEmpty,
154            NetError::DestAddrRequired => Self::DestAddrRequired,
155            NetError::FilesystemLoop => Self::FilesystemLoop,
156            NetError::FileTooLarge => Self::FileTooLarge,
157            NetError::InProgress => Self::InProgress,
158            NetError::Interrupted => Self::Interrupted,
159            NetError::InvalidInput => Self::InvalidInput,
160            NetError::InvalidData => Self::InvalidData,
161            NetError::IsADirectory => Self::IsADirectory,
162            NetError::MessageTooLong => Self::MessageTooLong,
163            NetError::NameTooLong => Self::NameTooLong,
164            NetError::NoMemory => Self::NoMemory,
165            NetError::NoSuchDevice => Self::NoSuchDevice,
166            NetError::NoSuchDeviceOrAddress => Self::NoSuchDeviceOrAddress,
167            NetError::NotConnected => Self::NotConnected,
168            NetError::NotFound => Self::NotFound,
169            NetError::NotADirectory => Self::NotADirectory,
170            NetError::NotASocket => Self::NotASocket,
171            NetError::NotATty => Self::NotATty,
172            NetError::OperationNotPermitted => Self::OperationNotPermitted,
173            NetError::OperationNotSupported => Self::OperationNotSupported,
174            NetError::AddressFamilyUnsupported => Self::AddressFamilyUnsupported,
175            NetError::ProtocolOptionUnsupported => Self::ProtocolOptionUnsupported,
176            NetError::PermissionDenied => Self::PermissionDenied,
177            NetError::ReadOnlyFilesystem => Self::ReadOnlyFilesystem,
178            NetError::ResourceBusy => Self::ResourceBusy,
179            NetError::StorageFull => Self::StorageFull,
180            NetError::TimedOut => Self::TimedOut,
181            NetError::Unsupported => Self::Unsupported,
182            NetError::EntropyUnavailable => Self::OperationNotSupported,
183            NetError::WouldBlock => Self::WouldBlock,
184            NetError::Io(error) => error,
185            NetError::BackendIo => Self::Io,
186        }
187    }
188}