use core::fmt;
use std::path::PathBuf;
use crate::snapshot::SnapshotError;
#[non_exhaustive]
#[derive(Debug)]
pub enum BootError {
Adopt(String),
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(reason) => write!(
f,
"this shepherd was handed a flock it could not take over: {reason}. 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. It holds the pidfile until it does, so 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(_) => None,
}
}
}
impl From<SnapshotError> for BootError {
fn from(source: SnapshotError) -> Self {
Self::Snapshot(source)
}
}