use core::fmt;
use std::path::PathBuf;
use crate::snapshot::SnapshotError;
#[non_exhaustive]
#[derive(Debug)]
pub enum BootError {
Adopt(Box<dyn core::error::Error + Send + Sync>),
Io {
path: PathBuf,
source: std::io::Error,
},
AlreadyRunning {
pid: Option<u32>,
},
Snapshot(SnapshotError),
SocketPathTooLong {
path: PathBuf,
len: usize,
limit: usize,
},
ReadyWrite(std::io::Error),
}
impl fmt::Display for BootError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Io { path, source } => {
write!(f, "boot step failed for `{}`: {source}", path.display())
}
Self::AlreadyRunning { pid: Some(pid) } => {
write!(f, "a shep daemon is already running (pid {pid})")
}
Self::AlreadyRunning { pid: None } => write!(f, "a shep daemon is already running"),
Self::Snapshot(err) => write!(f, "muster roll restore failed: {err}"),
Self::SocketPathTooLong { path, len, limit } => write!(
f,
"the control socket path is {len} bytes and this platform allows {limit}: `{}`. \
A unix socket path is bounded by the kernel, not by shep, so a shorter \
$SHEP_HOME is the only fix.",
path.display()
),
Self::ReadyWrite(err) => write!(f, "writing the readiness line failed: {err}"),
Self::Adopt(source) => write!(
f,
"this shepherd was handed a flock it could not take over: {source}. The flock is \
still running and nothing is supervising it. `shep daemon reload` is not the \
way back: it needs a live shepherd to ask and to signal, and this process is \
about to exit without ever serving, and boots nothing beside the flock \
first. The home is claimable straight afterwards and `shep muster` starts a \
shepherd from the roll"
),
}
}
}
impl core::error::Error for BootError {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
match self {
Self::Io { source, .. } => Some(source),
Self::AlreadyRunning { .. } => None,
Self::SocketPathTooLong { .. } => None,
Self::Snapshot(err) => Some(err),
Self::ReadyWrite(err) => Some(err),
Self::Adopt(source) => Some(source.as_ref()),
}
}
}
impl From<SnapshotError> for BootError {
fn from(source: SnapshotError) -> Self {
Self::Snapshot(source)
}
}
#[cfg(test)]
mod tests {
use super::*;
use core::error::Error as _;
#[test]
fn an_adopt_failure_reports_the_underlying_error_as_its_source() {
let err = BootError::Adopt(Box::new(std::io::Error::new(
std::io::ErrorKind::NotFound,
"fd 7 names no open descriptor",
)));
let source = err.source().expect("Adopt must report a source");
assert_eq!(source.to_string(), "fd 7 names no open descriptor");
assert!(
err.to_string().contains("fd 7 names no open descriptor"),
"the message must still name what failed: {err}"
);
}
}