Skip to main content

moirai_core/ipc/
error.rs

1use core::fmt;
2
3/// IPC-specific error type to minimize dependencies
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub enum IpcError {
6    /// System error with error code
7    SystemError(i32),
8    /// Invalid argument
9    InvalidArgument,
10    /// The name is already taken by a live segment
11    AlreadyExists,
12    /// Another handle already holds this endpoint of the queue
13    EndpointInUse,
14    /// Resource not found
15    NotFound,
16    /// Permission denied
17    PermissionDenied,
18}
19
20impl fmt::Display for IpcError {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        match self {
23            IpcError::SystemError(code) => write!(f, "System error: {code}"),
24            IpcError::InvalidArgument => write!(f, "Invalid argument"),
25            IpcError::AlreadyExists => write!(f, "Segment already exists"),
26            IpcError::EndpointInUse => write!(f, "Queue endpoint already in use"),
27            IpcError::NotFound => write!(f, "Resource not found"),
28            IpcError::PermissionDenied => write!(f, "Permission denied"),
29        }
30    }
31}
32
33impl core::error::Error for IpcError {}
34
35/// Convert the calling thread's last operating-system error to `IpcError`.
36#[cfg(any(unix, windows))]
37pub fn last_os_error() -> IpcError {
38    let code = std::io::Error::last_os_error()
39        .raw_os_error()
40        .expect("invariant: last_os_error captures a raw operating-system error code");
41    IpcError::SystemError(code)
42}