storefd/src/notify/error.rs
Mathieu Trossevin 776be0c96a
Proper NOTIFY_SOCKET support
Still need to implement watchdog detection.
2023-12-08 16:21:00 +01:00

122 lines
3.8 KiB
Rust

//! Contains the error types used by the [`notify`](super) module.
#![allow(clippy::module_name_repetitions)]
use core::fmt::Display;
use std::error::Error;
use rustix::io::Errno;
#[derive(Debug)]
pub enum NewNotifierError {
InvalidAbstractSocket(Errno),
InvalidSocketPath(Errno),
CouldntOpenSocket(std::io::Error),
}
impl Display for NewNotifierError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
NewNotifierError::InvalidAbstractSocket(error) => {
write!(f, "couldn't open notify abstract socket: {error}")
}
NewNotifierError::InvalidSocketPath(error) => {
write!(f, "couldn't open notify socket from path: {error}")
}
NewNotifierError::CouldntOpenSocket(error) => {
write!(f, "couldn't open unix socket: {error}")
}
}
}
}
impl Error for NewNotifierError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
NewNotifierError::InvalidAbstractSocket(error)
| NewNotifierError::InvalidSocketPath(error) => Some(error),
NewNotifierError::CouldntOpenSocket(error) => Some(error),
}
}
}
#[derive(Debug)]
pub enum NotifyError {
SanityCheck(SanityCheckError),
SendMsg(Errno),
PushAncillaryMessage,
PartialSend,
}
impl From<SanityCheckError> for NotifyError {
fn from(value: SanityCheckError) -> Self {
Self::SanityCheck(value)
}
}
impl Display for NotifyError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
NotifyError::SanityCheck(error) => {
write!(f, "NotifyState sanity check failed: {error}")
}
NotifyError::SendMsg(error) => write!(f, "couldn't send the message: {error}"),
NotifyError::PushAncillaryMessage => {
f.write_str("couldn't push necessary ancillary message for fd passing")
}
NotifyError::PartialSend => f.write_str("only some of the message could be sent"),
}
}
}
impl Error for NotifyError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
NotifyError::SanityCheck(error) => Some(error),
NotifyError::SendMsg(error) => Some(error),
NotifyError::PushAncillaryMessage | NotifyError::PartialSend => None,
}
}
}
#[derive(Debug)]
pub enum SanityCheckError {
InvalidFdName(FdNameError),
}
impl Display for SanityCheckError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SanityCheckError::InvalidFdName(error) => {
write!(f, "the value of FDNAME was invalid : {error}")
}
}
}
}
impl Error for SanityCheckError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
SanityCheckError::InvalidFdName(error) => Some(error),
}
}
}
#[derive(Debug)]
pub enum FdNameError {
TooLong { length: usize, name: String },
NotAsciiNonControl { disallowed_char: char, name: String },
ContainColon(String),
}
impl Display for FdNameError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
FdNameError::TooLong { length, name } => write!(f, "the file descriptor name {name:?} is too long (is {length} characters and should be less than 255)"),
FdNameError::NotAsciiNonControl { disallowed_char, name } => write!(f, "the file descriptor name {name:?} contains invalid character '{disallowed_char}' (only ASCII allowed)"),
FdNameError::ContainColon(name) => write!(f, "the file descriptor name {name:?} contains a colon (':') which isn't allowed"),
}
}
}
impl Error for FdNameError {}