storefd/src/error.rs

101 lines
3.3 KiB
Rust
Raw Normal View History

2023-12-07 18:02:35 +01:00
use core::fmt::Display;
use core::num::ParseIntError;
use std::error::Error;
use std::ffi::OsString;
use crate::{FD_NAMES_VAR, FD_NUMBER_VAR, PID_VAR};
#[derive(Debug)]
pub enum ReceiveError {
NoListenPID,
NotUnicodeListenPID(OsString),
ListenPIDParse(ParseIntError),
NoListenFD,
NotUnicodeListenFD(OsString),
ListenFDParse(ParseIntError),
2023-12-07 21:13:06 +01:00
PidMismatch { expected: u32, found: u32 },
2023-12-07 18:02:35 +01:00
GetFds(GetFdsError),
}
impl Display for ReceiveError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Couldn't receive file descriptors :")?;
match self {
ReceiveError::NoListenPID => write!(f, "The variable {PID_VAR} doesn't exists."),
ReceiveError::NotUnicodeListenPID(var) => write!(f, "The variable {PID_VAR} isn't unicode (this should never happen): it is {var:?}"),
ReceiveError::ListenPIDParse(error) => write!(f, "Couldn't parse {PID_VAR} as a `u32`: {error}"),
ReceiveError::NoListenFD => write!(f, "The variable {FD_NUMBER_VAR} doesn't exists."),
ReceiveError::NotUnicodeListenFD(var) => write!(f, "The variable {FD_NUMBER_VAR} isn't unicode (this should never happen): it is {var:?}"),
ReceiveError::ListenFDParse(error) => write!(f, "Couldn't parse {FD_NUMBER_VAR} as a `u32`: {error}"),
ReceiveError::PidMismatch{expected, found} => write!(f, "PID mismatch! Was {found} but should have been {expected}."),
ReceiveError::GetFds(error) => Display::fmt(error, f),
}
}
}
impl Error for ReceiveError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
ReceiveError::NoListenPID => None,
ReceiveError::NotUnicodeListenPID(_) => None,
ReceiveError::ListenPIDParse(error) => Some(error),
ReceiveError::NoListenFD => None,
ReceiveError::NotUnicodeListenFD(_) => None,
ReceiveError::ListenFDParse(error) => Some(error),
2023-12-07 21:13:06 +01:00
ReceiveError::PidMismatch {
expected: _,
found: _,
} => None,
2023-12-07 18:02:35 +01:00
ReceiveError::GetFds(error) => Some(error),
}
}
}
#[derive(Debug)]
pub enum ReceiveNameError {
NoListenFDName,
Receive(ReceiveError),
}
impl From<ReceiveError> for ReceiveNameError {
fn from(value: ReceiveError) -> Self {
Self::Receive(value)
}
}
impl Display for ReceiveNameError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
2023-12-07 21:13:06 +01:00
ReceiveNameError::NoListenFDName => write!(
f,
"Couldn't find FDs name : the variable {FD_NAMES_VAR} doesn't exists."
),
2023-12-07 18:02:35 +01:00
ReceiveNameError::Receive(error) => Display::fmt(error, f),
}
}
}
impl Error for ReceiveNameError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
ReceiveNameError::NoListenFDName => None,
ReceiveNameError::Receive(error) => Some(error),
}
}
}
#[derive(Debug)]
pub enum GetFdsError {
TooManyFDs(usize),
}
impl Display for GetFdsError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
GetFdsError::TooManyFDs(size) => write!(f, "Too many file descriptors ({size})"),
}
}
}
2023-12-07 21:13:06 +01:00
impl Error for GetFdsError {}