use core::fmt;
use std::path::{Path, PathBuf};
use shep_core::dog_defaults::DogDefaultsError;
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),
DogDefaults {
path: PathBuf,
source: DogDefaultsError,
},
}
impl BootError {
pub(crate) fn io(path: &Path, source: std::io::Error) -> Self {
Self::Io {
path: path.to_path_buf(),
source,
}
}
}
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::DogDefaults { path, source } => write!(
f,
"`{}` could not be read: {source}. Nothing was started: fix or remove the file, \
because a shepherd without it would serve every dog its tables with no defaults \
layered in",
path.display()
),
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::DogDefaults { source, .. } => Some(source),
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 io_carries_the_path_and_the_source() {
let err = BootError::io(
Path::new("/srv/flock/run"),
std::io::Error::new(std::io::ErrorKind::PermissionDenied, "denied"),
);
assert_eq!(
err.to_string(),
"boot step failed for `/srv/flock/run`: denied",
"the message names the path the step operated on and what the OS said"
);
let source = err.source().expect("Io must report a source");
assert_eq!(source.to_string(), "denied");
}
#[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}"
);
}
}