use crate::listen::ListenError;
use core::fmt;
use shep_core::config::template::RenderError;
#[non_exhaustive]
#[derive(Debug)]
pub enum AssembleError {
Template {
field: String,
source: RenderError,
},
DogMissing {
dog: String,
},
CgroupUnavailable {
setting: &'static str,
reason: String,
},
Listen(ListenError),
}
impl AssembleError {
#[must_use]
pub fn is_retriable(&self) -> bool {
match self {
Self::Template { source, .. } => source.is_retriable(),
Self::DogMissing { .. } => false,
Self::CgroupUnavailable { .. } => false,
Self::Listen(_) => false,
}
}
}
impl fmt::Display for AssembleError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Template { field, source } => write!(f, "`{field}`: {source}"),
Self::DogMissing { dog } => write!(
f,
"the `{dog}` dog is not adopted or is disabled, and this sheep does not run without it"
),
Self::CgroupUnavailable { setting, reason } => write!(
f,
"`{setting}` needs a cgroup for this sheep, and this shepherd has none: {reason}"
),
Self::Listen(source) => source.fmt(f),
}
}
}
impl core::error::Error for AssembleError {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
match self {
Self::Template { source, .. } => Some(source),
Self::DogMissing { .. } | Self::CgroupUnavailable { .. } => None,
Self::Listen(source) => Some(source),
}
}
}
#[cfg(test)]
mod tests {
use super::super::spawn_spec::assemble;
use super::AssembleError;
use shep_core::secrets::SecretView;
use super::super::testing::*;
use shep_core::config::{AppConfig, normalize};
#[test]
fn a_missing_key_refuses_the_spawn_and_names_the_field() {
let mut config = AppConfig::minimal("web", "./srv");
config.env.insert("PW".into(), "{{secret:ABSENT}}".into());
let app = normalize(config).unwrap();
let err = assemble(
&app,
0,
&test_paths(),
None,
&SecretView::empty("production".into()),
)
.unwrap_err();
assert!(!err.is_retriable());
assert_eq!(
err.to_string(),
"`PW`: `{{secret:ABSENT}}` has no value in the `production` environment"
);
assert_eq!(
format!("{err:?}"),
"Template { field: \"PW\", source: Unresolved { reference: \"{{secret:ABSENT}}\", \
environment: \"production\" } }"
);
let rendered = err.to_string();
assert!(
!rendered.contains('\u{2014}') && !rendered.contains('\u{2013}'),
"no em or en dash in copy a user reads: {rendered}"
);
}
#[test]
fn a_missing_dog_is_named_and_never_worth_waiting_for() {
let err = AssembleError::DogMissing {
dog: "sandbox".into(),
};
assert!(!err.is_retriable());
assert_eq!(
err.to_string(),
"the `sandbox` dog is not adopted or is disabled, and this sheep does not run without it"
);
assert_eq!(format!("{err:?}"), "DogMissing { dog: \"sandbox\" }");
}
}