ipc_communication/
ipc_error.rs

1//! A wrapper about `IpcError` that implement `Error` and stuff.
2
3use ipc_channel::ipc::IpcError;
4use std::{error::Error, fmt};
5
6/// IPC error wrapper.
7pub struct IpcErrorWrapper(
8    /// Original IPC error.
9    pub IpcError,
10);
11
12impl IpcErrorWrapper {
13    /// Checks whether this error represents a "disconnected" error.
14    pub fn is_disconnected(&self) -> bool {
15        if let IpcError::Disconnected = self.0 {
16            true
17        } else {
18            false
19        }
20    }
21}
22
23impl From<IpcError> for IpcErrorWrapper {
24    fn from(e: IpcError) -> Self {
25        Self(e)
26    }
27}
28
29impl fmt::Display for IpcErrorWrapper {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        match &self.0 {
32            IpcError::Io(io) => write!(f, "I/O error in IPC: {}", io),
33            IpcError::Bincode(err) => write!(f, "Bincode error in IPC: {}", err),
34            IpcError::Disconnected => write!(f, "IPC endpoint disconnected"),
35        }
36    }
37}
38
39impl fmt::Debug for IpcErrorWrapper {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        write!(f, "{:?}", self)
42    }
43}
44
45impl Error for IpcErrorWrapper {
46    fn source(&self) -> Option<&(dyn Error + 'static)> {
47        match &self.0 {
48            IpcError::Io(err) => Some(err),
49            IpcError::Bincode(err) => Some(err),
50            IpcError::Disconnected => None,
51        }
52    }
53}