Skip to main content

boxlite_shared/
errors.rs

1//! Error types used across the Boxlite runtime.
2
3use thiserror::Error;
4
5/// Result type for Boxlite operations.
6pub type BoxliteResult<T> = Result<T, BoxliteError>;
7
8#[derive(Debug, Error)]
9pub enum BoxliteError {
10    #[error("unsupported engine kind")]
11    UnsupportedEngine,
12
13    #[error("engine reported an error: {0}")]
14    Engine(String),
15
16    #[error("configuration error: {0}")]
17    Config(String),
18
19    #[error("storage error: {0}")]
20    Storage(String),
21
22    #[error("images error: {0}")]
23    Image(String),
24
25    #[error("portal error: {0}")]
26    Portal(String),
27
28    #[error("network error: {0}")]
29    Network(String),
30
31    #[error("gRPC/tonic error: {0}")]
32    Rpc(String),
33
34    #[error("gRPC transport error: {0}")]
35    RpcTransport(String),
36
37    #[error("internal error: {0}")]
38    Internal(String),
39
40    #[error("Execution error: {0}")]
41    Execution(String),
42
43    #[error("unsupported: {0}")]
44    Unsupported(String),
45
46    /// Box not found in registry or database.
47    #[error("box not found: {0}")]
48    NotFound(String),
49
50    /// Box or resource already exists.
51    #[error("already exists: {0}")]
52    AlreadyExists(String),
53
54    /// Box is in wrong state for the requested operation.
55    #[error("invalid state: {0}")]
56    InvalidState(String),
57
58    /// Database operation failed.
59    #[error("database error: {0}")]
60    Database(String),
61
62    /// Metadata corruption or parsing error.
63    #[error("metadata error: {0}")]
64    MetadataError(String),
65
66    /// Invalid argument provided.
67    #[error("invalid argument: {0}")]
68    InvalidArgument(String),
69
70    /// Resource (box or runtime) has been stopped/shutdown.
71    #[error("stopped: {0}")]
72    Stopped(String),
73}
74
75// Implement From for common error types to enable `?` operator
76impl From<std::io::Error> for BoxliteError {
77    fn from(err: std::io::Error) -> Self {
78        BoxliteError::Internal(format!("I/O error: {}", err))
79    }
80}
81
82impl From<serde_json::Error> for BoxliteError {
83    fn from(err: serde_json::Error) -> Self {
84        BoxliteError::Internal(format!("JSON error: {}", err))
85    }
86}
87
88impl From<String> for BoxliteError {
89    fn from(err: String) -> Self {
90        BoxliteError::Internal(err)
91    }
92}
93
94impl From<&str> for BoxliteError {
95    fn from(err: &str) -> Self {
96        BoxliteError::Internal(err.to_string())
97    }
98}
99
100impl From<tonic::Status> for BoxliteError {
101    fn from(err: tonic::Status) -> Self {
102        BoxliteError::Rpc(err.to_string())
103    }
104}
105
106impl From<tonic::transport::Error> for BoxliteError {
107    fn from(err: tonic::transport::Error) -> Self {
108        BoxliteError::RpcTransport(err.to_string())
109    }
110}