Skip to main content

arcbox_container/
error.rs

1//! Error types for container operations.
2
3use arcbox_error::CommonError;
4use thiserror::Error;
5
6/// Result type alias for container operations.
7pub type Result<T> = std::result::Result<T, ContainerError>;
8
9/// Errors that can occur during container operations.
10#[derive(Debug, Error)]
11pub enum ContainerError {
12    /// Common errors shared across `ArcBox` crates.
13    #[error(transparent)]
14    Common(#[from] CommonError),
15
16    /// Image error.
17    #[error("image error: {0}")]
18    Image(String),
19
20    /// Volume error.
21    #[error("volume error: {0}")]
22    Volume(String),
23
24    /// Runtime error.
25    #[error("runtime error: {0}")]
26    Runtime(String),
27
28    /// Internal lock poisoned.
29    #[error("internal lock poisoned")]
30    LockPoisoned,
31}
32
33impl From<std::io::Error> for ContainerError {
34    fn from(err: std::io::Error) -> Self {
35        Self::Common(CommonError::from(err))
36    }
37}
38
39impl ContainerError {
40    /// Creates a new not found error.
41    #[must_use]
42    pub fn not_found(resource: impl Into<String>) -> Self {
43        Self::Common(CommonError::not_found(resource))
44    }
45
46    /// Creates a new invalid state error.
47    #[must_use]
48    pub fn invalid_state(msg: impl Into<String>) -> Self {
49        Self::Common(CommonError::invalid_state(msg))
50    }
51
52    /// Creates a new config error.
53    #[must_use]
54    pub fn config(msg: impl Into<String>) -> Self {
55        Self::Common(CommonError::config(msg))
56    }
57
58    /// Creates a new already exists error.
59    #[must_use]
60    pub fn already_exists(resource: impl Into<String>) -> Self {
61        Self::Common(CommonError::already_exists(resource))
62    }
63
64    /// Returns true if this is a not found error.
65    #[must_use]
66    pub const fn is_not_found(&self) -> bool {
67        matches!(self, Self::Common(CommonError::NotFound(_)))
68    }
69
70    /// Returns true if this is an invalid state error.
71    #[must_use]
72    pub const fn is_invalid_state(&self) -> bool {
73        matches!(self, Self::Common(CommonError::InvalidState(_)))
74    }
75}