use std::fmt;
#[derive(Debug)]
pub enum PodmanError {
Connect(std::io::Error),
Hyper(hyper::Error),
Json(serde_json::Error),
Api { status: u16, message: String },
IncompatibleApiVersion { reported: String },
}
impl fmt::Display for PodmanError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Connect(e) => write!(f, "podman socket connection error: {e}"),
Self::Hyper(e) => write!(f, "http error: {e}"),
Self::Json(e) => write!(f, "json error: {e}"),
Self::Api { status, message } => match conflict_hint(message) {
Some(hint) => write!(f, "{hint} (podman: {message})"),
None => write!(f, "podman API error (HTTP {status}): {message}"),
},
Self::IncompatibleApiVersion { reported } => {
let reported = if reported.is_empty() {
"an unknown version"
} else {
reported.as_str()
};
write!(
f,
"podup requires Podman >= 5.0; this server reports libpod API version {reported}"
)
}
}
}
}
fn conflict_hint(message: &str) -> Option<&'static str> {
let m = message.to_ascii_lowercase();
if m.contains("without force") || (m.contains("cannot remove") && m.contains("running")) {
if m.contains("is paused") {
Some("the container is paused — unpause it first, or pass `-f` to force removal")
} else {
Some("the container is running — stop it first, or pass `-f` to force removal")
}
} else if m.contains("already paused") {
Some("the container is already paused")
} else if m.contains("not paused") {
Some("the container is not paused")
} else if m.contains("not running")
|| m.contains("can only kill running containers")
|| m.contains("can only create exec sessions on running containers")
{
Some("the container is not running")
} else if m.contains("already running") {
Some("the container is already running")
} else if m.contains("must be in created or stopped state")
|| (m.contains("unable to start") && m.contains("state"))
{
Some("the container cannot be started in its current state")
} else if m.contains("container state improper") {
Some("the container is not in a valid state for this operation")
} else {
None
}
}
impl std::error::Error for PodmanError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Connect(e) => Some(e),
Self::Hyper(e) => Some(e),
Self::Json(e) => Some(e),
Self::Api { .. } | Self::IncompatibleApiVersion { .. } => None,
}
}
}
impl From<std::io::Error> for PodmanError {
fn from(e: std::io::Error) -> Self {
Self::Connect(e)
}
}
impl From<hyper::Error> for PodmanError {
fn from(e: hyper::Error) -> Self {
Self::Hyper(e)
}
}
impl From<serde_json::Error> for PodmanError {
fn from(e: serde_json::Error) -> Self {
Self::Json(e)
}
}
impl PodmanError {
pub fn is_status(&self, code: u16) -> bool {
matches!(self, Self::Api { status, .. } if *status == code)
}
pub(crate) fn is_timeout(&self) -> bool {
matches!(self, Self::Api { status: 0, message } if message.contains("timed out"))
}
pub(crate) fn is_kill_of_stopped(&self) -> bool {
matches!(
self,
Self::Api { status: 409, message }
if message.to_ascii_lowercase().contains("can only kill running")
)
}
pub(crate) fn is_already_exists(&self) -> bool {
match self {
Self::Api { status: 409, .. } => true,
Self::Api {
status: 500,
message,
} => message.contains("already exists"),
_ => false,
}
}
pub(crate) fn is_image_in_use(&self) -> bool {
match self {
Self::Api { status: 409, .. } => true,
Self::Api {
status: 500,
message,
} => {
let m = message.to_ascii_lowercase();
m.contains("in use") || m.contains("being used") || m.contains("used by")
}
_ => false,
}
}
pub(crate) fn is_state_conflict(&self) -> bool {
match self {
Self::Api { status, message } if *status == 409 || *status == 500 => {
let m = message.to_ascii_lowercase();
m.contains("state improper")
|| m.contains("already paused")
|| m.contains("not paused")
|| m.contains("not running")
}
_ => false,
}
}
}
#[cfg(test)]
mod tests {
use super::{conflict_hint, PodmanError};
#[test]
fn conflict_hint_recognises_common_state_errors() {
let rm = "cannot remove container abc as it is running - running or paused containers \
cannot be removed without force: container state improper";
assert!(conflict_hint(rm).unwrap().contains("-f"));
assert!(conflict_hint("container abc is already paused")
.unwrap()
.contains("already paused"));
assert!(conflict_hint("container abc is not paused")
.unwrap()
.contains("not paused"));
assert!(
conflict_hint("cannot kill container abc: container abc is not running")
.unwrap()
.contains("not running")
);
assert!(conflict_hint(
"can only kill running containers. abc is in state exited: container state improper"
)
.unwrap()
.contains("not running"));
}
#[test]
fn is_kill_of_stopped_matches_only_the_kill_409() {
let stopped = PodmanError::Api {
status: 409,
message: "can only kill running containers. abc is in state exited: \
container state improper"
.into(),
};
assert!(stopped.is_kill_of_stopped());
let paused = PodmanError::Api {
status: 409,
message: "container abc is already paused".into(),
};
assert!(!paused.is_kill_of_stopped());
let other = PodmanError::Api {
status: 500,
message: "can only kill running containers".into(),
};
assert!(!other.is_kill_of_stopped());
assert!(
!PodmanError::Json(serde_json::from_str::<u8>("bad").unwrap_err()).is_kill_of_stopped()
);
}
#[test]
fn conflict_hint_paused_rm_is_not_labelled_running() {
let paused = "cannot remove container abc as it is paused - running or paused containers \
cannot be removed without force: container state improper";
let hint = conflict_hint(paused).unwrap();
assert!(hint.contains("paused"), "got {hint:?}");
assert!(!hint.contains("running"), "must not say running: {hint:?}");
assert!(hint.contains("-f"));
}
#[test]
fn conflict_hint_covers_kill_exec_and_start() {
assert!(
conflict_hint("can only kill running containers. abc is in state exited")
.unwrap()
.contains("not running")
);
assert!(conflict_hint(
"can only create exec sessions on running containers: container state improper"
)
.unwrap()
.contains("not running"));
assert!(conflict_hint(
"unable to start container abc: container must be in Created or Stopped state to be \
started"
)
.unwrap()
.contains("cannot be started"));
}
#[test]
fn conflict_hint_none_for_unrecognised() {
assert!(conflict_hint("some unrelated error").is_none());
assert!(conflict_hint("no such container: abc").is_none());
}
#[test]
fn api_error_display_leads_with_hint_and_keeps_message() {
let e = PodmanError::Api {
status: 409,
message: "container abc is already paused".into(),
};
let s = e.to_string();
assert!(s.starts_with("the container is already paused"));
assert!(s.contains("podman: container abc is already paused"));
}
#[test]
fn api_error_display_raw_when_no_hint() {
let e = PodmanError::Api {
status: 500,
message: "boom".into(),
};
assert_eq!(e.to_string(), "podman API error (HTTP 500): boom");
}
#[test]
fn is_status_matches_code() {
let e = PodmanError::Api {
status: 404,
message: "not found".into(),
};
assert!(e.is_status(404));
assert!(!e.is_status(200));
assert!(!e.is_status(500));
}
#[test]
fn is_timeout_matches_synthetic_timeout_error() {
assert!(PodmanError::Api {
status: 0,
message: "timed out after 40s waiting for the Podman socket to respond".into(),
}
.is_timeout());
assert!(!PodmanError::Api {
status: 500,
message: "boom".into(),
}
.is_timeout());
assert!(!PodmanError::Api {
status: 0,
message: "invalid API path".into(),
}
.is_timeout());
}
#[test]
fn is_status_false_for_non_api() {
let e = PodmanError::Json(serde_json::from_str::<u8>("bad").unwrap_err());
assert!(!e.is_status(404));
}
#[test]
fn already_exists_accepts_409_and_500_with_message() {
assert!(PodmanError::Api {
status: 409,
message: "network already used".into(),
}
.is_already_exists());
assert!(PodmanError::Api {
status: 500,
message: "volume with name p_v already exists: volume already exists".into(),
}
.is_already_exists());
}
#[test]
fn image_in_use_accepts_409_and_500_with_message() {
assert!(PodmanError::Api {
status: 409,
message: "image is in use by 1 container".into(),
}
.is_image_in_use());
assert!(PodmanError::Api {
status: 500,
message: "image used by a container: image in use".into(),
}
.is_image_in_use());
assert!(!PodmanError::Api {
status: 500,
message: "internal error".into(),
}
.is_image_in_use());
assert!(!PodmanError::Api {
status: 404,
message: "no such image".into(),
}
.is_image_in_use());
}
#[test]
fn state_conflict_recognises_pause_unpause_mismatches() {
for msg in [
"container abc is already paused: container state improper",
"container abc is not paused: container state improper",
"cannot pause container abc: container abc is not running",
"unpausing container: container state improper",
] {
assert!(
PodmanError::Api {
status: 500,
message: msg.into(),
}
.is_state_conflict(),
"should treat {msg:?} as a state conflict"
);
}
assert!(!PodmanError::Api {
status: 500,
message: "internal error".into(),
}
.is_state_conflict());
assert!(!PodmanError::Api {
status: 404,
message: "no such container".into(),
}
.is_state_conflict());
}
#[test]
fn already_exists_false_for_other_errors() {
assert!(!PodmanError::Api {
status: 500,
message: "internal error".into(),
}
.is_already_exists());
assert!(!PodmanError::Api {
status: 404,
message: "no such volume".into(),
}
.is_already_exists());
assert!(
!PodmanError::Json(serde_json::from_str::<u8>("bad").unwrap_err()).is_already_exists()
);
}
#[test]
fn display_api_error() {
let e = PodmanError::Api {
status: 500,
message: "internal error".into(),
};
assert_eq!(e.to_string(), "podman API error (HTTP 500): internal error");
}
#[test]
fn display_json_error() {
let e = PodmanError::Json(serde_json::from_str::<u8>("bad").unwrap_err());
assert!(e.to_string().contains("json error"));
}
#[test]
fn display_connect_error() {
let e = PodmanError::Connect(std::io::Error::new(
std::io::ErrorKind::NotFound,
"no socket",
));
assert!(e.to_string().contains("podman socket connection error"));
}
#[test]
fn display_incompatible_api_version_reports_version() {
let e = PodmanError::IncompatibleApiVersion {
reported: "4.9.3".into(),
};
let msg = e.to_string();
assert!(msg.contains("Podman >= 5.0"));
assert!(msg.contains("4.9.3"));
}
#[test]
fn display_incompatible_api_version_handles_missing_header() {
let e = PodmanError::IncompatibleApiVersion {
reported: String::new(),
};
let msg = e.to_string();
assert!(msg.contains("an unknown version"));
}
#[test]
fn source_present_for_wrapped_errors_absent_for_owned() {
use std::error::Error;
let connect = PodmanError::Connect(std::io::Error::new(
std::io::ErrorKind::NotFound,
"no socket",
));
assert!(connect.source().is_some());
let json = PodmanError::Json(serde_json::from_str::<u8>("bad").unwrap_err());
assert!(json.source().is_some());
assert!(PodmanError::Api {
status: 500,
message: "x".into(),
}
.source()
.is_none());
assert!(PodmanError::IncompatibleApiVersion {
reported: "4.0.0".into(),
}
.source()
.is_none());
}
#[test]
fn from_io_error_becomes_connect() {
let io = std::io::Error::new(std::io::ErrorKind::ConnectionRefused, "refused");
let e: PodmanError = io.into();
assert!(matches!(e, PodmanError::Connect(_)));
assert!(e.to_string().contains("podman socket connection error"));
}
#[test]
fn from_json_error_becomes_json() {
let json_err = serde_json::from_str::<u8>("not-json").unwrap_err();
let e: PodmanError = json_err.into();
assert!(matches!(e, PodmanError::Json(_)));
assert!(e.to_string().contains("json error"));
}
}